mirror of https://github.com/LemmyNet/lemmy
Move code to apub library (#1795)
* Remove dependency of apub_lib on LemmyContext * Move ApubObject trait to library * Reorganize files in apub lib * Move ActorType, signatures, activity_queue to apub librarypull/1825/head
parent
fe7c1b300b
commit
b96ce81f89
@ -1,66 +0,0 @@
|
||||
use crate::{check_is_apub_id_valid, ActorType, CommunityType};
|
||||
use itertools::Itertools;
|
||||
use lemmy_api_common::blocking;
|
||||
use lemmy_db_queries::DbPool;
|
||||
use lemmy_db_schema::source::community::Community;
|
||||
use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
|
||||
use lemmy_utils::{settings::structs::Settings, LemmyError};
|
||||
use url::Url;
|
||||
|
||||
impl ActorType for Community {
|
||||
fn is_local(&self) -> bool {
|
||||
self.local
|
||||
}
|
||||
fn actor_id(&self) -> Url {
|
||||
self.actor_id.to_owned().into()
|
||||
}
|
||||
fn name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
fn public_key(&self) -> Option<String> {
|
||||
self.public_key.to_owned()
|
||||
}
|
||||
fn private_key(&self) -> Option<String> {
|
||||
self.private_key.to_owned()
|
||||
}
|
||||
|
||||
fn get_shared_inbox_or_inbox_url(&self) -> Url {
|
||||
self
|
||||
.shared_inbox_url
|
||||
.clone()
|
||||
.unwrap_or_else(|| self.inbox_url.to_owned())
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl CommunityType for Community {
|
||||
fn followers_url(&self) -> Url {
|
||||
self.followers_url.clone().into()
|
||||
}
|
||||
|
||||
/// For a given community, returns the inboxes of all followers.
|
||||
async fn get_follower_inboxes(
|
||||
&self,
|
||||
pool: &DbPool,
|
||||
settings: &Settings,
|
||||
) -> Result<Vec<Url>, LemmyError> {
|
||||
let id = self.id;
|
||||
|
||||
let follows = blocking(pool, move |conn| {
|
||||
CommunityFollowerView::for_community(conn, id)
|
||||
})
|
||||
.await??;
|
||||
let inboxes = follows
|
||||
.into_iter()
|
||||
.filter(|f| !f.follower.local)
|
||||
.map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
|
||||
.map(|i| i.into_inner())
|
||||
.unique()
|
||||
// Don't send to blocked instances
|
||||
.filter(|inbox| check_is_apub_id_valid(inbox, false, settings).is_ok())
|
||||
.collect();
|
||||
|
||||
Ok(inboxes)
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
pub(crate) mod community;
|
||||
pub(crate) mod person;
|
@ -1,31 +0,0 @@
|
||||
use crate::ActorType;
|
||||
use lemmy_db_schema::source::person::Person;
|
||||
use url::Url;
|
||||
|
||||
impl ActorType for Person {
|
||||
fn is_local(&self) -> bool {
|
||||
self.local
|
||||
}
|
||||
fn actor_id(&self) -> Url {
|
||||
self.actor_id.to_owned().into_inner()
|
||||
}
|
||||
fn name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
fn public_key(&self) -> Option<String> {
|
||||
self.public_key.to_owned()
|
||||
}
|
||||
|
||||
fn private_key(&self) -> Option<String> {
|
||||
self.private_key.to_owned()
|
||||
}
|
||||
|
||||
fn get_shared_inbox_or_inbox_url(&self) -> Url {
|
||||
self
|
||||
.shared_inbox_url
|
||||
.clone()
|
||||
.unwrap_or_else(|| self.inbox_url.to_owned())
|
||||
.into()
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
pub mod context;
|
||||
pub mod signatures;
|
@ -1,107 +0,0 @@
|
||||
use crate::{
|
||||
fetcher::fetch::fetch_remote_object,
|
||||
objects::{comment::Note, post::Page, FromApub},
|
||||
PostOrComment,
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use diesel::result::Error::NotFound;
|
||||
use lemmy_api_common::blocking;
|
||||
use lemmy_db_queries::{ApubObject, Crud};
|
||||
use lemmy_db_schema::source::{comment::Comment, post::Post};
|
||||
use lemmy_utils::LemmyError;
|
||||
use lemmy_websocket::LemmyContext;
|
||||
use log::debug;
|
||||
use url::Url;
|
||||
|
||||
/// Gets a post by its apub ID. If it exists locally, it is returned directly. Otherwise it is
|
||||
/// pulled from its apub ID, inserted and returned.
|
||||
///
|
||||
/// The parent community is also pulled if necessary. Comments are not pulled.
|
||||
pub(crate) async fn get_or_fetch_and_insert_post(
|
||||
post_ap_id: &Url,
|
||||
context: &LemmyContext,
|
||||
recursion_counter: &mut i32,
|
||||
) -> Result<Post, LemmyError> {
|
||||
let post_ap_id_owned = post_ap_id.to_owned();
|
||||
let post = blocking(context.pool(), move |conn| {
|
||||
Post::read_from_apub_id(conn, &post_ap_id_owned.into())
|
||||
})
|
||||
.await?;
|
||||
|
||||
match post {
|
||||
Ok(p) => Ok(p),
|
||||
Err(NotFound {}) => {
|
||||
debug!("Fetching and creating remote post: {}", post_ap_id);
|
||||
let page = fetch_remote_object::<Page>(
|
||||
context.client(),
|
||||
&context.settings(),
|
||||
post_ap_id,
|
||||
recursion_counter,
|
||||
)
|
||||
.await?;
|
||||
let post = Post::from_apub(&page, context, post_ap_id, recursion_counter).await?;
|
||||
|
||||
Ok(post)
|
||||
}
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets a comment by its apub ID. If it exists locally, it is returned directly. Otherwise it is
|
||||
/// pulled from its apub ID, inserted and returned.
|
||||
///
|
||||
/// The parent community, post and comment are also pulled if necessary.
|
||||
pub(crate) async fn get_or_fetch_and_insert_comment(
|
||||
comment_ap_id: &Url,
|
||||
context: &LemmyContext,
|
||||
recursion_counter: &mut i32,
|
||||
) -> Result<Comment, LemmyError> {
|
||||
let comment_ap_id_owned = comment_ap_id.to_owned();
|
||||
let comment = blocking(context.pool(), move |conn| {
|
||||
Comment::read_from_apub_id(conn, &comment_ap_id_owned.into())
|
||||
})
|
||||
.await?;
|
||||
|
||||
match comment {
|
||||
Ok(p) => Ok(p),
|
||||
Err(NotFound {}) => {
|
||||
debug!(
|
||||
"Fetching and creating remote comment and its parents: {}",
|
||||
comment_ap_id
|
||||
);
|
||||
let comment = fetch_remote_object::<Note>(
|
||||
context.client(),
|
||||
&context.settings(),
|
||||
comment_ap_id,
|
||||
recursion_counter,
|
||||
)
|
||||
.await?;
|
||||
let comment = Comment::from_apub(&comment, context, comment_ap_id, recursion_counter).await?;
|
||||
|
||||
let post_id = comment.post_id;
|
||||
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
||||
if post.locked {
|
||||
return Err(anyhow!("Post is locked").into());
|
||||
}
|
||||
|
||||
Ok(comment)
|
||||
}
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn get_or_fetch_and_insert_post_or_comment(
|
||||
ap_id: &Url,
|
||||
context: &LemmyContext,
|
||||
recursion_counter: &mut i32,
|
||||
) -> Result<PostOrComment, LemmyError> {
|
||||
Ok(
|
||||
match get_or_fetch_and_insert_post(ap_id, context, recursion_counter).await {
|
||||
Ok(p) => PostOrComment::Post(Box::new(p)),
|
||||
Err(_) => {
|
||||
let c = get_or_fetch_and_insert_comment(ap_id, context, recursion_counter).await?;
|
||||
PostOrComment::Comment(Box::new(c))
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
use crate::{
|
||||
fetcher::{fetch::fetch_remote_object, is_deleted, should_refetch_actor},
|
||||
objects::{person::Person as ApubPerson, FromApub},
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use diesel::result::Error::NotFound;
|
||||
use lemmy_api_common::blocking;
|
||||
use lemmy_db_queries::{source::person::Person_, ApubObject};
|
||||
use lemmy_db_schema::source::person::Person;
|
||||
use lemmy_utils::LemmyError;
|
||||
use lemmy_websocket::LemmyContext;
|
||||
use log::debug;
|
||||
use url::Url;
|
||||
|
||||
/// Get a person from its apub ID.
|
||||
///
|
||||
/// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
|
||||
/// Otherwise it is fetched from the remote instance, stored and returned.
|
||||
pub(crate) async fn get_or_fetch_and_upsert_person(
|
||||
apub_id: &Url,
|
||||
context: &LemmyContext,
|
||||
recursion_counter: &mut i32,
|
||||
) -> Result<Person, LemmyError> {
|
||||
let apub_id_owned = apub_id.to_owned();
|
||||
let person = blocking(context.pool(), move |conn| {
|
||||
Person::read_from_apub_id(conn, &apub_id_owned.into())
|
||||
})
|
||||
.await?;
|
||||
|
||||
match person {
|
||||
// If its older than a day, re-fetch it
|
||||
Ok(u) if !u.local && should_refetch_actor(u.last_refreshed_at) => {
|
||||
debug!("Fetching and updating from remote person: {}", apub_id);
|
||||
let person = fetch_remote_object::<ApubPerson>(
|
||||
context.client(),
|
||||
&context.settings(),
|
||||
apub_id,
|
||||
recursion_counter,
|
||||
)
|
||||
.await;
|
||||
|
||||
if is_deleted(&person) {
|
||||
// TODO: use Person::update_deleted() once implemented
|
||||
blocking(context.pool(), move |conn| {
|
||||
Person::delete_account(conn, u.id)
|
||||
})
|
||||
.await??;
|
||||
return Err(anyhow!("Person was deleted by remote instance").into());
|
||||
} else if person.is_err() {
|
||||
return Ok(u);
|
||||
}
|
||||
|
||||
let person = Person::from_apub(&person?, context, apub_id, recursion_counter).await?;
|
||||
|
||||
let person_id = person.id;
|
||||
blocking(context.pool(), move |conn| {
|
||||
Person::mark_as_updated(conn, person_id)
|
||||
})
|
||||
.await??;
|
||||
|
||||
Ok(person)
|
||||
}
|
||||
Ok(u) => Ok(u),
|
||||
Err(NotFound {}) => {
|
||||
debug!("Fetching and creating remote person: {}", apub_id);
|
||||
let person = fetch_remote_object::<ApubPerson>(
|
||||
context.client(),
|
||||
&context.settings(),
|
||||
apub_id,
|
||||
recursion_counter,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let person = Person::from_apub(&person, context, apub_id, recursion_counter).await?;
|
||||
|
||||
Ok(person)
|
||||
}
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
use std::{ops::Deref, sync::Arc};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Data<T: ?Sized>(Arc<T>);
|
||||
|
||||
impl<T> Data<T> {
|
||||
/// Create new `Data` instance.
|
||||
pub fn new(state: T) -> Data<T> {
|
||||
Data(Arc::new(state))
|
||||
}
|
||||
|
||||
/// Get reference to inner app data.
|
||||
pub fn get_ref(&self) -> &T {
|
||||
self.0.as_ref()
|
||||
}
|
||||
|
||||
/// Convert to the internal Arc<T>
|
||||
pub fn into_inner(self) -> Arc<T> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Deref for Data<T> {
|
||||
type Target = Arc<T>;
|
||||
|
||||
fn deref(&self) -> &Arc<T> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Clone for Data<T> {
|
||||
fn clone(&self) -> Data<T> {
|
||||
Data(self.0.clone())
|
||||
}
|
||||
}
|
@ -1,44 +1,12 @@
|
||||
pub mod values;
|
||||
|
||||
use activitystreams::error::DomainError;
|
||||
pub use lemmy_apub_lib_derive::*;
|
||||
use lemmy_utils::LemmyError;
|
||||
use lemmy_websocket::LemmyContext;
|
||||
use url::Url;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
pub mod activity_queue;
|
||||
pub mod data;
|
||||
pub mod signatures;
|
||||
pub mod traits;
|
||||
pub mod values;
|
||||
pub mod verify;
|
||||
pub mod webfinger;
|
||||
|
||||
pub trait ActivityFields {
|
||||
fn id_unchecked(&self) -> &Url;
|
||||
fn actor(&self) -> &Url;
|
||||
fn cc(&self) -> Vec<Url>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
pub trait ActivityHandler {
|
||||
async fn verify(
|
||||
&self,
|
||||
context: &LemmyContext,
|
||||
request_counter: &mut i32,
|
||||
) -> Result<(), LemmyError>;
|
||||
|
||||
async fn receive(
|
||||
self,
|
||||
context: &LemmyContext,
|
||||
request_counter: &mut i32,
|
||||
) -> Result<(), LemmyError>;
|
||||
}
|
||||
|
||||
pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), LemmyError> {
|
||||
if a.domain() != b.domain() {
|
||||
return Err(DomainError.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn verify_urls_match(a: &Url, b: &Url) -> Result<(), LemmyError> {
|
||||
if a != b {
|
||||
return Err(DomainError.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";
|
||||
|
@ -0,0 +1,67 @@
|
||||
use crate::{data::Data, signatures::PublicKey};
|
||||
use activitystreams::chrono::NaiveDateTime;
|
||||
use anyhow::Context;
|
||||
pub use lemmy_apub_lib_derive::*;
|
||||
use lemmy_utils::{location_info, LemmyError};
|
||||
use url::Url;
|
||||
|
||||
pub trait ActivityFields {
|
||||
fn id_unchecked(&self) -> &Url;
|
||||
fn actor(&self) -> &Url;
|
||||
fn cc(&self) -> Vec<Url>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
pub trait ActivityHandler {
|
||||
type DataType;
|
||||
async fn verify(
|
||||
&self,
|
||||
data: &Data<Self::DataType>,
|
||||
request_counter: &mut i32,
|
||||
) -> Result<(), LemmyError>;
|
||||
|
||||
async fn receive(
|
||||
self,
|
||||
data: &Data<Self::DataType>,
|
||||
request_counter: &mut i32,
|
||||
) -> Result<(), LemmyError>;
|
||||
}
|
||||
|
||||
pub trait ApubObject {
|
||||
type DataType;
|
||||
/// If this object should be refetched after a certain interval, it should return the last refresh
|
||||
/// time here. This is mainly used to update remote actors.
|
||||
fn last_refreshed_at(&self) -> Option<NaiveDateTime>;
|
||||
/// Try to read the object with given ID from local database. Returns Ok(None) if it doesn't exist.
|
||||
fn read_from_apub_id(data: &Self::DataType, object_id: Url) -> Result<Option<Self>, LemmyError>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
/// Common methods provided by ActivityPub actors (community and person). Not all methods are
|
||||
/// implemented by all actors.
|
||||
pub trait ActorType {
|
||||
fn is_local(&self) -> bool;
|
||||
fn actor_id(&self) -> Url;
|
||||
fn name(&self) -> String;
|
||||
|
||||
// TODO: this should not be an option (needs db migration in lemmy)
|
||||
fn public_key(&self) -> Option<String>;
|
||||
fn private_key(&self) -> Option<String>;
|
||||
|
||||
fn inbox_url(&self) -> Url;
|
||||
|
||||
fn shared_inbox_url(&self) -> Option<Url>;
|
||||
|
||||
fn shared_inbox_or_inbox_url(&self) -> Url {
|
||||
self.shared_inbox_url().unwrap_or_else(|| self.inbox_url())
|
||||
}
|
||||
|
||||
fn get_public_key(&self) -> Result<PublicKey, LemmyError> {
|
||||
Ok(PublicKey {
|
||||
id: format!("{}#main-key", self.actor_id()),
|
||||
owner: self.actor_id(),
|
||||
public_key_pem: self.public_key().context(location_info!())?,
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
use activitystreams::error::DomainError;
|
||||
use lemmy_utils::LemmyError;
|
||||
use url::Url;
|
||||
|
||||
pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), LemmyError> {
|
||||
if a.domain() != b.domain() {
|
||||
return Err(DomainError.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn verify_urls_match(a: &Url, b: &Url) -> Result<(), LemmyError> {
|
||||
if a != b {
|
||||
return Err(DomainError.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue