Reorganize files in apub lib

This commit is contained in:
Felix Ableitner 2021-10-04 12:56:05 +02:00
parent 03cba11976
commit 455e0b5760
46 changed files with 213 additions and 329 deletions

View file

@ -17,11 +17,10 @@ use crate::{
use activitystreams::{base::AnyBase, link::Mention, primitives::OneOrMany, unparsed::Unparsed};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
verify_domains_match,
ActivityFields,
ActivityHandler,
Data,
verify::verify_domains_match,
};
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};

View file

@ -20,7 +20,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_queries::{source::community::CommunityModerator_, Joinable};
use lemmy_db_schema::source::{
community::{Community, CommunityModerator, CommunityModeratorForm},

View file

@ -31,7 +31,11 @@ use activitystreams::{
primitives::OneOrMany,
unparsed::Unparsed,
};
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_schema::source::community::Community;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;

View file

@ -18,7 +18,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_queries::{Bannable, Followable};
use lemmy_db_schema::source::{
community::{

View file

@ -21,7 +21,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_queries::Joinable;
use lemmy_db_schema::source::{
community::{Community, CommunityModerator, CommunityModeratorForm},

View file

@ -18,7 +18,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_queries::Bannable;
use lemmy_db_schema::source::{
community::{Community, CommunityPersonBan, CommunityPersonBanForm},

View file

@ -19,7 +19,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::{
community::{Community, CommunityForm},

View file

@ -23,7 +23,11 @@ use activitystreams::{
};
use anyhow::anyhow;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_queries::{
source::{comment::Comment_, community::Community_, post::Post_},
Crud,

View file

@ -9,7 +9,10 @@ use crate::{
};
use diesel::PgConnection;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{verify_domains_match, ActivityFields, ApubObject};
use lemmy_apub_lib::{
traits::{ActivityFields, ApubObject},
verify::verify_domains_match,
};
use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
use lemmy_utils::LemmyError;

View file

@ -24,7 +24,11 @@ use activitystreams::{
};
use anyhow::anyhow;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
use lemmy_utils::LemmyError;

View file

@ -17,7 +17,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
verify::verify_urls_match,
};
use lemmy_db_queries::Followable;
use lemmy_db_schema::source::{
community::{Community, CommunityFollower},

View file

@ -17,7 +17,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
verify::verify_urls_match,
};
use lemmy_db_queries::Followable;
use lemmy_db_schema::source::{
community::{Community, CommunityFollower, CommunityFollowerForm},

View file

@ -17,7 +17,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
verify::verify_urls_match,
};
use lemmy_db_queries::Followable;
use lemmy_db_schema::source::{
community::{Community, CommunityFollower, CommunityFollowerForm},

View file

@ -6,7 +6,7 @@ use crate::{
};
use anyhow::anyhow;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{verify_domains_match, ActivityFields};
use lemmy_apub_lib::{traits::ActivityFields, verify::verify_domains_match};
use lemmy_db_schema::source::{community::Community, person::Person};
use lemmy_db_views_actor::community_view::CommunityView;
use lemmy_utils::{settings::structs::Settings, LemmyError};

View file

@ -17,12 +17,10 @@ use activitystreams::{base::AnyBase, primitives::OneOrMany, unparsed::Unparsed};
use anyhow::anyhow;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
verify_domains_match,
verify_urls_match,
ActivityFields,
ActivityHandler,
Data,
verify::{verify_domains_match, verify_urls_match},
};
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::{community::Community, person::Person, post::Post};

View file

@ -8,7 +8,11 @@ use crate::{
};
use activitystreams::{base::AnyBase, primitives::OneOrMany, unparsed::Unparsed};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{verify_domains_match, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
verify::verify_domains_match,
};
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage};
use lemmy_utils::LemmyError;

View file

@ -12,7 +12,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{verify_domains_match, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
verify::verify_domains_match,
};
use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud};
use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage};
use lemmy_utils::LemmyError;

View file

@ -18,11 +18,9 @@ use activitystreams::{
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
verify_domains_match,
verify_urls_match,
ActivityFields,
ActivityHandler,
Data,
data::Data,
traits::{ActivityFields, ActivityHandler},
verify::{verify_domains_match, verify_urls_match},
};
use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud};
use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage};

View file

@ -12,7 +12,11 @@ use activitystreams::{
primitives::OneOrMany,
unparsed::Unparsed,
};
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_schema::source::{community::Community, person::Person};
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;

View file

@ -23,7 +23,12 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
verify::verify_urls_match,
};
use lemmy_db_queries::Crud;
use lemmy_db_schema::{
source::{community::Community, person::Person},

View file

@ -15,7 +15,11 @@ use crate::{
use activitystreams::{base::AnyBase, primitives::OneOrMany, unparsed::Unparsed};
use anyhow::anyhow;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_db_queries::Crud;
use lemmy_db_schema::{
source::{community::Community, person::Person},

View file

@ -6,7 +6,7 @@ use crate::{
use activitystreams::collection::{CollectionExt, OrderedCollection};
use anyhow::Context;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{ActivityHandler, Data};
use lemmy_apub_lib::{data::Data, traits::ActivityHandler};
use lemmy_db_queries::Joinable;
use lemmy_db_schema::source::{
community::{Community, CommunityModerator, CommunityModeratorForm},

View file

@ -6,7 +6,7 @@ use crate::{
use anyhow::anyhow;
use diesel::{NotFound, PgConnection};
use lemmy_api_common::blocking;
use lemmy_apub_lib::ApubObject;
use lemmy_apub_lib::traits::ApubObject;
use lemmy_db_queries::DbPool;
use lemmy_db_schema::DbUrl;
use lemmy_utils::{request::retry, settings::structs::Settings, LemmyError};

View file

@ -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))
}
},
)
}

View file

@ -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()),
}
}

View file

@ -1,7 +1,7 @@
use crate::objects::{comment::Note, post::Page, FromApub};
use activitystreams::chrono::NaiveDateTime;
use diesel::PgConnection;
use lemmy_apub_lib::ApubObject;
use lemmy_apub_lib::traits::ApubObject;
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
post::{Post, PostForm},

View file

@ -8,8 +8,8 @@ use diesel::PgConnection;
use itertools::Itertools;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
traits::ApubObject,
webfinger::{webfinger_resolve_actor, WebfingerType},
ApubObject,
};
use lemmy_db_queries::{
source::{community::Community_, person::Person_},

View file

@ -22,7 +22,7 @@ use activitystreams::{
};
use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{ActivityFields, ActivityHandler};
use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler};
use lemmy_db_queries::source::{activity::Activity_, community::Community_};
use lemmy_db_schema::source::{activity::Activity, community::Community};
use lemmy_db_views_actor::{

View file

@ -20,7 +20,10 @@ use anyhow::{anyhow, Context};
use futures::StreamExt;
use http::StatusCode;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{ActivityFields, ActivityHandler, Data};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
};
use lemmy_db_queries::{source::activity::Activity_, DbPool};
use lemmy_db_schema::source::activity::Activity;
use lemmy_utils::{location_info, LemmyError};

View file

@ -24,7 +24,7 @@ use activitystreams::{
};
use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{ActivityFields, ActivityHandler};
use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler};
use lemmy_db_queries::source::person::Person_;
use lemmy_db_schema::source::person::Person;
use lemmy_utils::LemmyError;

View file

@ -18,7 +18,7 @@ use chrono::{DateTime, FixedOffset};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
values::{MediaTypeHtml, MediaTypeMarkdown, PublicUrl},
verify_domains_match,
verify::verify_domains_match,
};
use lemmy_db_queries::{source::comment::Comment_, Crud, DbPool};
use lemmy_db_schema::{

View file

@ -16,7 +16,7 @@ use chrono::{DateTime, FixedOffset};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
values::{MediaTypeHtml, MediaTypeMarkdown},
verify_domains_match,
verify::verify_domains_match,
};
use lemmy_db_queries::{source::community::Community_, DbPool};
use lemmy_db_schema::{

View file

@ -15,7 +15,7 @@ use activitystreams::{
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
values::{MediaTypeHtml, MediaTypeMarkdown},
verify_domains_match,
verify::verify_domains_match,
};
use lemmy_db_queries::{source::person::Person_, DbPool};
use lemmy_db_schema::{

View file

@ -19,7 +19,7 @@ use chrono::{DateTime, FixedOffset};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
values::{MediaTypeHtml, MediaTypeMarkdown},
verify_domains_match,
verify::verify_domains_match,
};
use lemmy_db_queries::{source::post::Post_, Crud, DbPool};
use lemmy_db_schema::{

View file

@ -14,7 +14,7 @@ use chrono::{DateTime, FixedOffset};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
values::{MediaTypeHtml, MediaTypeMarkdown},
verify_domains_match,
verify::verify_domains_match,
};
use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud, DbPool};
use lemmy_db_schema::source::{

View file

@ -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())
}
}

View file

@ -1,90 +1,5 @@
pub mod data;
pub mod traits;
pub mod values;
use activitystreams::{chrono::NaiveDateTime, error::DomainError};
pub use lemmy_apub_lib_derive::*;
use lemmy_utils::LemmyError;
use std::{ops::Deref, sync::Arc};
use url::Url;
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 {
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;
}
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(())
}
#[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())
}
}

View file

@ -0,0 +1,38 @@
use crate::data::Data;
use activitystreams::chrono::NaiveDateTime;
pub use lemmy_apub_lib_derive::*;
use lemmy_utils::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;
}

View file

@ -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(())
}

View file

@ -91,11 +91,11 @@ pub fn derive_activity_handler(input: proc_macro::TokenStream) -> proc_macro::To
let expanded = quote! {
#[async_trait::async_trait(?Send)]
impl #impl_generics lemmy_apub_lib::ActivityHandler for #enum_name #ty_generics #where_clause {
impl #impl_generics lemmy_apub_lib::traits::ActivityHandler for #enum_name #ty_generics #where_clause {
type DataType = #attrs;
async fn verify(
&self,
context: &lemmy_apub_lib::Data<Self::DataType>,
context: &lemmy_apub_lib::data::Data<Self::DataType>,
request_counter: &mut i32,
) -> Result<(), lemmy_utils::LemmyError> {
match self {
@ -104,7 +104,7 @@ pub fn derive_activity_handler(input: proc_macro::TokenStream) -> proc_macro::To
}
async fn receive(
self,
context: &lemmy_apub_lib::Data<Self::DataType>,
context: &lemmy_apub_lib::data::Data<Self::DataType>,
request_counter: &mut i32,
) -> Result<(), lemmy_utils::LemmyError> {
match self {
@ -149,7 +149,7 @@ pub fn derive_activity_fields(input: proc_macro::TokenStream) -> proc_macro::Tok
.iter()
.map(|v| generate_match_arm(&name, v, &quote! {a.cc()}));
quote! {
impl #impl_generics lemmy_apub_lib::ActivityFields for #name #ty_generics #where_clause {
impl #impl_generics lemmy_apub_lib::traits::ActivityFields for #name #ty_generics #where_clause {
fn id_unchecked(&self) -> &url::Url { match self { #(#impl_id)* } }
fn actor(&self) -> &url::Url { match self { #(#impl_actor)* } }
fn cc(&self) -> Vec<url::Url> { match self { #(#impl_cc)* } }
@ -171,7 +171,7 @@ pub fn derive_activity_fields(input: proc_macro::TokenStream) -> proc_macro::Tok
quote! {vec![]}
};
quote! {
impl #impl_generics lemmy_apub_lib::ActivityFields for #name #ty_generics #where_clause {
impl #impl_generics lemmy_apub_lib::traits::ActivityFields for #name #ty_generics #where_clause {
fn id_unchecked(&self) -> &url::Url { &self.id }
fn actor(&self) -> &url::Url { &self.actor.inner() }
fn cc(&self) -> Vec<url::Url> { #cc_impl }

View file

@ -8,7 +8,7 @@ use crate::{
};
use chrono::NaiveDateTime;
use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
use lemmy_apub_lib::ApubObject;
use lemmy_apub_lib::traits::ApubObject;
use lemmy_utils::LemmyError;
use serde::Serialize;
use url::Url;

View file

@ -6,7 +6,7 @@ use crate::{
};
use chrono::NaiveDateTime;
use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
use lemmy_apub_lib::ApubObject;
use lemmy_apub_lib::traits::ApubObject;
use lemmy_utils::LemmyError;
use serde::Serialize;
use url::Url;

View file

@ -5,7 +5,7 @@ use crate::{
};
use chrono::NaiveDateTime;
use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
use lemmy_apub_lib::ApubObject;
use lemmy_apub_lib::traits::ApubObject;
use lemmy_utils::LemmyError;
use serde::Serialize;
use url::Url;

View file

@ -7,7 +7,7 @@ use crate::{
};
use chrono::NaiveDateTime;
use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
use lemmy_apub_lib::ApubObject;
use lemmy_apub_lib::traits::ApubObject;
use lemmy_utils::LemmyError;
use serde::Serialize;
use url::Url;

View file

@ -1,7 +1,7 @@
use crate::{schema::private_message, DbUrl, PersonId, PrivateMessageId};
use chrono::NaiveDateTime;
use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
use lemmy_apub_lib::ApubObject;
use lemmy_apub_lib::traits::ApubObject;
use lemmy_utils::LemmyError;
use serde::Serialize;
use url::Url;