From dfce4b67ca7855c48206e9a539a131e247d0d3fb Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 24 Jun 2021 03:50:41 +0200 Subject: [PATCH] move library code to separate crate (most of it) --- Cargo.lock | 14 ++++ Cargo.toml | 1 + crates/apub_lib/Cargo.toml | 13 +++ crates/apub_lib/src/lib.rs | 66 ++++++++++++++++ crates/apub_receive/Cargo.toml | 1 + .../src/activities_new/comment.rs | 3 +- .../apub_receive/src/activities_new/follow.rs | 5 +- .../src/activities_new/private_message.rs | 3 +- .../src/inbox/new_inbox_routing.rs | 79 ++----------------- crates/apub_receive/src/inbox/person_inbox.rs | 3 +- 10 files changed, 110 insertions(+), 78 deletions(-) create mode 100644 crates/apub_lib/Cargo.toml create mode 100644 crates/apub_lib/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index a9e8dd0de..fe8cb2af7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1880,6 +1880,19 @@ dependencies = [ "uuid", ] +[[package]] +name = "lemmy_apub_lib" +version = "0.1.0" +dependencies = [ + "activitystreams", + "activitystreams-ext", + "async-trait", + "lemmy_utils", + "lemmy_websocket", + "serde", + "url", +] + [[package]] name = "lemmy_apub_receive" version = "0.1.0" @@ -1904,6 +1917,7 @@ dependencies = [ "itertools", "lemmy_api_common", "lemmy_apub", + "lemmy_apub_lib", "lemmy_db_queries", "lemmy_db_schema", "lemmy_db_views", diff --git a/Cargo.toml b/Cargo.toml index 0fd838819..eec65009e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ members = [ "crates/api", "crates/api_crud", "crates/api_common", + "crates/apub_lib", "crates/apub", "crates/apub_receive", "crates/utils", diff --git a/crates/apub_lib/Cargo.toml b/crates/apub_lib/Cargo.toml new file mode 100644 index 000000000..4d85c73fa --- /dev/null +++ b/crates/apub_lib/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "lemmy_apub_lib" +version = "0.1.0" +edition = "2018" + +[dependencies] +lemmy_utils = { path = "../utils" } +lemmy_websocket = { path = "../websocket" } +activitystreams = "0.7.0-alpha.11" +activitystreams-ext = "0.1.0-alpha.2" +serde = { version = "1.0.123", features = ["derive"] } +async-trait = "0.1.42" +url = { version = "2.2.1", features = ["serde"] } diff --git a/crates/apub_lib/src/lib.rs b/crates/apub_lib/src/lib.rs new file mode 100644 index 000000000..993376ebb --- /dev/null +++ b/crates/apub_lib/src/lib.rs @@ -0,0 +1,66 @@ +use activitystreams::{ + error::DomainError, +}; +use lemmy_utils::LemmyError; +use lemmy_websocket::LemmyContext; +use std::marker::PhantomData; +use url::Url; + +// for now, limit it to activity routing only, no http sigs, parsing or any of that +// need to route in this order: +// 1. recipient actor +// 2. activity type +// 3. inner object (recursively until object is empty or an url) + +// TODO: turn this into a trait in which app has to implement the following functions: +// .checkIdValid() - for unique, instance block etc +// .checkHttpSig::() +// .fetchObject() - for custom http client +// .checkActivity() - for common validity checks +pub struct InboxConfig { + //actors: Vec, +} + +impl InboxConfig { + pub fn shared_inbox_handler() { + todo!() + } +} + +#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)] +pub enum PublicUrl { + #[serde(rename = "https://www.w3.org/ns/activitystreams#Public")] + Public, +} + +pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), LemmyError> { + if a.domain() != b.domain() { + return Err(DomainError.into()); + } + Ok(()) +} + +// todo: later add a similar trait SendActivity +// todo: maybe add a separate method verify() +#[async_trait::async_trait(?Send)] +pub trait ReceiveActivity { + // todo: later handle request_counter completely inside library + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError>; +} + +// todo: instead of phantomdata, might use option to cache the fetched object (or just fetch on construction) +pub struct ObjectId<'a, Kind>(Url, &'a PhantomData); + +impl ObjectId<'_, Kind> { + pub fn url(self) -> Url { + self.0 + } + pub fn dereference(self) -> Result { + // todo: fetch object from http or database + todo!() + } +} diff --git a/crates/apub_receive/Cargo.toml b/crates/apub_receive/Cargo.toml index 3141d19da..3f0b1ebc5 100644 --- a/crates/apub_receive/Cargo.toml +++ b/crates/apub_receive/Cargo.toml @@ -5,6 +5,7 @@ edition = "2018" [dependencies] lemmy_utils = { path = "../utils" } +lemmy_apub_lib = { path = "../apub_lib" } lemmy_apub = { path = "../apub" } lemmy_db_queries = { path = "../db_queries" } lemmy_db_schema = { path = "../db_schema" } diff --git a/crates/apub_receive/src/activities_new/comment.rs b/crates/apub_receive/src/activities_new/comment.rs index 2c552f762..54369da46 100644 --- a/crates/apub_receive/src/activities_new/comment.rs +++ b/crates/apub_receive/src/activities_new/comment.rs @@ -1,4 +1,3 @@ -use crate::inbox::new_inbox_routing::{ReceiveActivity, Activity, PublicUrl}; use url::Url; use lemmy_apub::NoteExt; use activitystreams:: @@ -15,6 +14,8 @@ use lemmy_utils::utils::scrape_text_for_mentions; use lemmy_db_schema::source::post::Post; use lemmy_db_queries::Crud; use lemmy_apub::fetcher::person::get_or_fetch_and_upsert_person; +use lemmy_apub_lib::{PublicUrl, ReceiveActivity}; +use crate::inbox::new_inbox_routing::Activity; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] diff --git a/crates/apub_receive/src/activities_new/follow.rs b/crates/apub_receive/src/activities_new/follow.rs index 3fbca98e2..cfd088ef2 100644 --- a/crates/apub_receive/src/activities_new/follow.rs +++ b/crates/apub_receive/src/activities_new/follow.rs @@ -1,6 +1,3 @@ -use crate::{ - inbox::new_inbox_routing::{verify_domains_match, Activity, ReceiveActivity}, -}; use activitystreams::{ activity::kind::{AcceptType, FollowType}, }; @@ -14,6 +11,8 @@ use lemmy_db_schema::source::community::CommunityFollower; use lemmy_utils::{LemmyError}; use lemmy_websocket::LemmyContext; use url::Url; +use lemmy_apub_lib::{ReceiveActivity, verify_domains_match}; +use crate::inbox::new_inbox_routing::Activity; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] diff --git a/crates/apub_receive/src/activities_new/private_message.rs b/crates/apub_receive/src/activities_new/private_message.rs index 50a63046b..b8e449917 100644 --- a/crates/apub_receive/src/activities_new/private_message.rs +++ b/crates/apub_receive/src/activities_new/private_message.rs @@ -1,4 +1,3 @@ -use crate::inbox::new_inbox_routing::{Activity, ReceiveActivity}; use activitystreams::activity::kind::CreateType; use lemmy_api_common::{blocking, person::PrivateMessageResponse}; use lemmy_apub::{objects::FromApub, NoteExt}; @@ -7,6 +6,8 @@ use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::Priva use lemmy_utils::LemmyError; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud}; use url::Url; +use lemmy_apub_lib::{ReceiveActivity}; +use crate::inbox::new_inbox_routing::Activity; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] diff --git a/crates/apub_receive/src/inbox/new_inbox_routing.rs b/crates/apub_receive/src/inbox/new_inbox_routing.rs index 26a010f37..324433b16 100644 --- a/crates/apub_receive/src/inbox/new_inbox_routing.rs +++ b/crates/apub_receive/src/inbox/new_inbox_routing.rs @@ -1,79 +1,16 @@ use crate::activities_new::follow::Accept; -use activitystreams::{ - base::AnyBase, - error::DomainError, - primitives::OneOrMany, - unparsed::Unparsed, -}; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; -use std::marker::PhantomData; -use url::Url; use crate::activities_new::private_message::CreatePrivateMessage; use crate::activities_new::comment::CreateComment; +use lemmy_apub_lib::ReceiveActivity; +use activitystreams::primitives::OneOrMany; +use activitystreams::base::AnyBase; +use url::Url; +use activitystreams::unparsed::Unparsed; -// for now, limit it to activity routing only, no http sigs, parsing or any of that -// need to route in this order: -// 1. recipient actor -// 2. activity type -// 3. inner object (recursively until object is empty or an url) - -// library part -// todo: move this to separate crate - -// TODO: turn this into a trait in which app has to implement the following functions: -// .checkIdValid() - for unique, instance block etc -// .checkHttpSig::() -// .fetchObject() - for custom http client -// .checkActivity() - for common validity checks -pub struct InboxConfig { - //actors: Vec, -} - -impl InboxConfig { - pub fn shared_inbox_handler() { - todo!() - } -} - -#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)] -pub enum PublicUrl { - #[serde(rename = "https://www.w3.org/ns/activitystreams#Public")] - Public, -} - -pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), LemmyError> { - if a.domain() != b.domain() { - return Err(DomainError.into()); - } - Ok(()) -} - -// todo: later add a similar trait SendActivity -// todo: maybe add a separate method verify() -#[async_trait::async_trait(?Send)] -pub trait ReceiveActivity { - // todo: later handle request_counter completely inside library - async fn receive( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError>; -} - -// todo: instead of phantomdata, might use option to cache the fetched object (or just fetch on construction) -pub struct ObjectId<'a, Kind>(Url, &'a PhantomData); - -impl ObjectId<'_, Kind> { - pub fn url(self) -> Url { - self.0 - } - pub fn dereference(self) -> Result { - // todo: fetch object from http or database - todo!() - } -} - +// TODO: would be nice if we could move this to lemmy_apub_lib crate. doing that gives error: +// "only traits defined in the current crate can be implemented for arbitrary types" #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct Activity { @@ -97,8 +34,6 @@ impl Activity { } } -// application part - #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] pub enum PersonAcceptedActivitiesNew { Accept(Accept), diff --git a/crates/apub_receive/src/inbox/person_inbox.rs b/crates/apub_receive/src/inbox/person_inbox.rs index f79819564..7b5d31306 100644 --- a/crates/apub_receive/src/inbox/person_inbox.rs +++ b/crates/apub_receive/src/inbox/person_inbox.rs @@ -19,7 +19,6 @@ use crate::{ is_activity_already_known, is_addressed_to_community_followers, is_addressed_to_local_person, - new_inbox_routing::{Activity, PersonAcceptedActivitiesNew, ReceiveActivity}, receive_for_community::{ receive_add_for_community, receive_block_user_for_community, @@ -61,6 +60,8 @@ use serde::{Deserialize, Serialize}; use std::fmt::Debug; use strum_macros::EnumString; use url::Url; +use crate::inbox::new_inbox_routing::{PersonAcceptedActivitiesNew, Activity}; +use lemmy_apub_lib::ReceiveActivity; /// Allowed activities for person inbox. #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]