From 455e0b5760fa77e7d0fe5c6e5d8e88715ca383aa Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 4 Oct 2021 12:56:05 +0200 Subject: [PATCH] Reorganize files in apub lib --- .../activities/comment/create_or_update.rs | 7 +- .../apub/src/activities/community/add_mod.rs | 6 +- .../apub/src/activities/community/announce.rs | 6 +- .../src/activities/community/block_user.rs | 6 +- .../src/activities/community/remove_mod.rs | 6 +- .../activities/community/undo_block_user.rs | 6 +- .../apub/src/activities/community/update.rs | 6 +- crates/apub/src/activities/deletion/delete.rs | 6 +- crates/apub/src/activities/deletion/mod.rs | 5 +- .../src/activities/deletion/undo_delete.rs | 6 +- .../apub/src/activities/following/accept.rs | 6 +- .../apub/src/activities/following/follow.rs | 6 +- crates/apub/src/activities/following/undo.rs | 6 +- crates/apub/src/activities/mod.rs | 2 +- .../src/activities/post/create_or_update.rs | 8 +- .../private_message/create_or_update.rs | 6 +- .../src/activities/private_message/delete.rs | 6 +- .../activities/private_message/undo_delete.rs | 8 +- crates/apub/src/activities/undo_remove.rs | 6 +- .../apub/src/activities/voting/undo_vote.rs | 7 +- crates/apub/src/activities/voting/vote.rs | 6 +- crates/apub/src/fetcher/community.rs | 2 +- crates/apub/src/fetcher/object_id.rs | 2 +- crates/apub/src/fetcher/objects.rs | 107 ------------------ crates/apub/src/fetcher/person.rs | 80 ------------- crates/apub/src/fetcher/post_or_comment.rs | 2 +- crates/apub/src/fetcher/search.rs | 2 +- crates/apub/src/http/community.rs | 2 +- crates/apub/src/http/mod.rs | 5 +- crates/apub/src/http/person.rs | 2 +- crates/apub/src/objects/comment.rs | 2 +- crates/apub/src/objects/community.rs | 2 +- crates/apub/src/objects/person.rs | 2 +- crates/apub/src/objects/post.rs | 2 +- crates/apub/src/objects/private_message.rs | 2 +- crates/apub_lib/src/data.rs | 35 ++++++ crates/apub_lib/src/lib.rs | 91 +-------------- crates/apub_lib/src/traits.rs | 38 +++++++ .../apub_lib/src/{values/mod.rs => values.rs} | 0 crates/apub_lib/src/verify.rs | 17 +++ crates/apub_lib_derive/src/lib.rs | 10 +- crates/db_schema/src/source/comment.rs | 2 +- crates/db_schema/src/source/community.rs | 2 +- crates/db_schema/src/source/person.rs | 2 +- crates/db_schema/src/source/post.rs | 2 +- .../db_schema/src/source/private_message.rs | 2 +- 46 files changed, 213 insertions(+), 329 deletions(-) delete mode 100644 crates/apub/src/fetcher/objects.rs delete mode 100644 crates/apub/src/fetcher/person.rs create mode 100644 crates/apub_lib/src/data.rs create mode 100644 crates/apub_lib/src/traits.rs rename crates/apub_lib/src/{values/mod.rs => values.rs} (100%) create mode 100644 crates/apub_lib/src/verify.rs diff --git a/crates/apub/src/activities/comment/create_or_update.rs b/crates/apub/src/activities/comment/create_or_update.rs index e469e74b3..646a14f12 100644 --- a/crates/apub/src/activities/comment/create_or_update.rs +++ b/crates/apub/src/activities/comment/create_or_update.rs @@ -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}; diff --git a/crates/apub/src/activities/community/add_mod.rs b/crates/apub/src/activities/community/add_mod.rs index f4e75080e..d3785f4a4 100644 --- a/crates/apub/src/activities/community/add_mod.rs +++ b/crates/apub/src/activities/community/add_mod.rs @@ -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}, diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index 032797c81..93f015e74 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -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; diff --git a/crates/apub/src/activities/community/block_user.rs b/crates/apub/src/activities/community/block_user.rs index 35db21911..0009b0697 100644 --- a/crates/apub/src/activities/community/block_user.rs +++ b/crates/apub/src/activities/community/block_user.rs @@ -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::{ diff --git a/crates/apub/src/activities/community/remove_mod.rs b/crates/apub/src/activities/community/remove_mod.rs index 7e1d344d0..b58fdd2ca 100644 --- a/crates/apub/src/activities/community/remove_mod.rs +++ b/crates/apub/src/activities/community/remove_mod.rs @@ -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}, diff --git a/crates/apub/src/activities/community/undo_block_user.rs b/crates/apub/src/activities/community/undo_block_user.rs index d8fda9b11..a47a5b728 100644 --- a/crates/apub/src/activities/community/undo_block_user.rs +++ b/crates/apub/src/activities/community/undo_block_user.rs @@ -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}, diff --git a/crates/apub/src/activities/community/update.rs b/crates/apub/src/activities/community/update.rs index 3061fa1f7..2e412f326 100644 --- a/crates/apub/src/activities/community/update.rs +++ b/crates/apub/src/activities/community/update.rs @@ -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}, diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index c9821cb65..27338c390 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -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, diff --git a/crates/apub/src/activities/deletion/mod.rs b/crates/apub/src/activities/deletion/mod.rs index 2abe7ae7d..3dee47639 100644 --- a/crates/apub/src/activities/deletion/mod.rs +++ b/crates/apub/src/activities/deletion/mod.rs @@ -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; diff --git a/crates/apub/src/activities/deletion/undo_delete.rs b/crates/apub/src/activities/deletion/undo_delete.rs index f7f07297f..e2bf36b38 100644 --- a/crates/apub/src/activities/deletion/undo_delete.rs +++ b/crates/apub/src/activities/deletion/undo_delete.rs @@ -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; diff --git a/crates/apub/src/activities/following/accept.rs b/crates/apub/src/activities/following/accept.rs index be26e0c14..4ad3ea34e 100644 --- a/crates/apub/src/activities/following/accept.rs +++ b/crates/apub/src/activities/following/accept.rs @@ -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}, diff --git a/crates/apub/src/activities/following/follow.rs b/crates/apub/src/activities/following/follow.rs index e9e1d2b83..35c3307c9 100644 --- a/crates/apub/src/activities/following/follow.rs +++ b/crates/apub/src/activities/following/follow.rs @@ -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}, diff --git a/crates/apub/src/activities/following/undo.rs b/crates/apub/src/activities/following/undo.rs index ae6cc9bbc..8a3581c51 100644 --- a/crates/apub/src/activities/following/undo.rs +++ b/crates/apub/src/activities/following/undo.rs @@ -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}, diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index f477a66a4..0da5d8878 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -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}; diff --git a/crates/apub/src/activities/post/create_or_update.rs b/crates/apub/src/activities/post/create_or_update.rs index 72df0052d..a2dab8dd0 100644 --- a/crates/apub/src/activities/post/create_or_update.rs +++ b/crates/apub/src/activities/post/create_or_update.rs @@ -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}; diff --git a/crates/apub/src/activities/private_message/create_or_update.rs b/crates/apub/src/activities/private_message/create_or_update.rs index e2519dc28..8e212813c 100644 --- a/crates/apub/src/activities/private_message/create_or_update.rs +++ b/crates/apub/src/activities/private_message/create_or_update.rs @@ -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; diff --git a/crates/apub/src/activities/private_message/delete.rs b/crates/apub/src/activities/private_message/delete.rs index 499f0a938..aeb8b0ec3 100644 --- a/crates/apub/src/activities/private_message/delete.rs +++ b/crates/apub/src/activities/private_message/delete.rs @@ -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; diff --git a/crates/apub/src/activities/private_message/undo_delete.rs b/crates/apub/src/activities/private_message/undo_delete.rs index 486476298..8983d06da 100644 --- a/crates/apub/src/activities/private_message/undo_delete.rs +++ b/crates/apub/src/activities/private_message/undo_delete.rs @@ -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}; diff --git a/crates/apub/src/activities/undo_remove.rs b/crates/apub/src/activities/undo_remove.rs index f92938f5c..10305466c 100644 --- a/crates/apub/src/activities/undo_remove.rs +++ b/crates/apub/src/activities/undo_remove.rs @@ -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; diff --git a/crates/apub/src/activities/voting/undo_vote.rs b/crates/apub/src/activities/voting/undo_vote.rs index 06e73ae22..cbc523bd4 100644 --- a/crates/apub/src/activities/voting/undo_vote.rs +++ b/crates/apub/src/activities/voting/undo_vote.rs @@ -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}, diff --git a/crates/apub/src/activities/voting/vote.rs b/crates/apub/src/activities/voting/vote.rs index 23b728797..936826616 100644 --- a/crates/apub/src/activities/voting/vote.rs +++ b/crates/apub/src/activities/voting/vote.rs @@ -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}, diff --git a/crates/apub/src/fetcher/community.rs b/crates/apub/src/fetcher/community.rs index 329d4ad54..62749f6e3 100644 --- a/crates/apub/src/fetcher/community.rs +++ b/crates/apub/src/fetcher/community.rs @@ -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}, diff --git a/crates/apub/src/fetcher/object_id.rs b/crates/apub/src/fetcher/object_id.rs index 883ee8c16..6abdb48f4 100644 --- a/crates/apub/src/fetcher/object_id.rs +++ b/crates/apub/src/fetcher/object_id.rs @@ -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}; diff --git a/crates/apub/src/fetcher/objects.rs b/crates/apub/src/fetcher/objects.rs deleted file mode 100644 index c222776d5..000000000 --- a/crates/apub/src/fetcher/objects.rs +++ /dev/null @@ -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 { - 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::( - 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 { - 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::( - 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 { - 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)) - } - }, - ) -} diff --git a/crates/apub/src/fetcher/person.rs b/crates/apub/src/fetcher/person.rs deleted file mode 100644 index 54c3163a6..000000000 --- a/crates/apub/src/fetcher/person.rs +++ /dev/null @@ -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 { - 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::( - 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::( - 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()), - } -} diff --git a/crates/apub/src/fetcher/post_or_comment.rs b/crates/apub/src/fetcher/post_or_comment.rs index 940123aec..7971cedba 100644 --- a/crates/apub/src/fetcher/post_or_comment.rs +++ b/crates/apub/src/fetcher/post_or_comment.rs @@ -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}, diff --git a/crates/apub/src/fetcher/search.rs b/crates/apub/src/fetcher/search.rs index 0faca2da5..c8ac0d925 100644 --- a/crates/apub/src/fetcher/search.rs +++ b/crates/apub/src/fetcher/search.rs @@ -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_}, diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index 3eed569c3..dcf02c6ea 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -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::{ diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index 1471f02d7..9d0508341 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -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}; diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index d00e5aaa6..5c234c09c 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -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; diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index e27817d6f..fe3e73284 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -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::{ diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index c8ddd2972..cb61df5a0 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -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::{ diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index 5bf7a5897..a34a5c429 100644 --- a/crates/apub/src/objects/person.rs +++ b/crates/apub/src/objects/person.rs @@ -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::{ diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index c92213a62..457cb2163 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -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::{ diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index 9bc917a95..7fd0708aa 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -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::{ diff --git a/crates/apub_lib/src/data.rs b/crates/apub_lib/src/data.rs new file mode 100644 index 000000000..d366f0fbf --- /dev/null +++ b/crates/apub_lib/src/data.rs @@ -0,0 +1,35 @@ +use std::{ops::Deref, sync::Arc}; + +#[derive(Debug)] +pub struct Data(Arc); + +impl Data { + /// Create new `Data` instance. + pub fn new(state: T) -> Data { + 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 + pub fn into_inner(self) -> Arc { + self.0 + } +} + +impl Deref for Data { + type Target = Arc; + + fn deref(&self) -> &Arc { + &self.0 + } +} + +impl Clone for Data { + fn clone(&self) -> Data { + Data(self.0.clone()) + } +} diff --git a/crates/apub_lib/src/lib.rs b/crates/apub_lib/src/lib.rs index 361a64b4c..e20a056ae 100644 --- a/crates/apub_lib/src/lib.rs +++ b/crates/apub_lib/src/lib.rs @@ -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; -} - -#[async_trait::async_trait(?Send)] -pub trait ActivityHandler { - type DataType; - async fn verify( - &self, - data: &Data, - request_counter: &mut i32, - ) -> Result<(), LemmyError>; - - async fn receive( - self, - data: &Data, - 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; - /// 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, 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(Arc); - -impl Data { - /// Create new `Data` instance. - pub fn new(state: T) -> Data { - 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 - pub fn into_inner(self) -> Arc { - self.0 - } -} - -impl Deref for Data { - type Target = Arc; - - fn deref(&self) -> &Arc { - &self.0 - } -} - -impl Clone for Data { - fn clone(&self) -> Data { - Data(self.0.clone()) - } -} diff --git a/crates/apub_lib/src/traits.rs b/crates/apub_lib/src/traits.rs new file mode 100644 index 000000000..0577e16be --- /dev/null +++ b/crates/apub_lib/src/traits.rs @@ -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; +} + +#[async_trait::async_trait(?Send)] +pub trait ActivityHandler { + type DataType; + async fn verify( + &self, + data: &Data, + request_counter: &mut i32, + ) -> Result<(), LemmyError>; + + async fn receive( + self, + data: &Data, + 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; + /// 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, LemmyError> + where + Self: Sized; +} diff --git a/crates/apub_lib/src/values/mod.rs b/crates/apub_lib/src/values.rs similarity index 100% rename from crates/apub_lib/src/values/mod.rs rename to crates/apub_lib/src/values.rs diff --git a/crates/apub_lib/src/verify.rs b/crates/apub_lib/src/verify.rs new file mode 100644 index 000000000..426409b7c --- /dev/null +++ b/crates/apub_lib/src/verify.rs @@ -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(()) +} diff --git a/crates/apub_lib_derive/src/lib.rs b/crates/apub_lib_derive/src/lib.rs index a00644c65..44185d14e 100644 --- a/crates/apub_lib_derive/src/lib.rs +++ b/crates/apub_lib_derive/src/lib.rs @@ -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, + context: &lemmy_apub_lib::data::Data, 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, + context: &lemmy_apub_lib::data::Data, 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, "e! {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 { 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 { #cc_impl } diff --git a/crates/db_schema/src/source/comment.rs b/crates/db_schema/src/source/comment.rs index 2116a93c2..88c47d3a4 100644 --- a/crates/db_schema/src/source/comment.rs +++ b/crates/db_schema/src/source/comment.rs @@ -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; diff --git a/crates/db_schema/src/source/community.rs b/crates/db_schema/src/source/community.rs index 16ed7cd3c..6e9583550 100644 --- a/crates/db_schema/src/source/community.rs +++ b/crates/db_schema/src/source/community.rs @@ -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; diff --git a/crates/db_schema/src/source/person.rs b/crates/db_schema/src/source/person.rs index cdf60c959..ddc4b0379 100644 --- a/crates/db_schema/src/source/person.rs +++ b/crates/db_schema/src/source/person.rs @@ -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; diff --git a/crates/db_schema/src/source/post.rs b/crates/db_schema/src/source/post.rs index a6c8369c3..c0c42c830 100644 --- a/crates/db_schema/src/source/post.rs +++ b/crates/db_schema/src/source/post.rs @@ -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; diff --git a/crates/db_schema/src/source/private_message.rs b/crates/db_schema/src/source/private_message.rs index b4780affd..a0d1b9d10 100644 --- a/crates/db_schema/src/source/private_message.rs +++ b/crates/db_schema/src/source/private_message.rs @@ -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;