diff --git a/crates/apub_receive/src/activities_new/follow.rs b/crates/apub_receive/src/activities_new/follow.rs index 3c0c9209..75e83593 100644 --- a/crates/apub_receive/src/activities_new/follow.rs +++ b/crates/apub_receive/src/activities_new/follow.rs @@ -1,67 +1,78 @@ -use url::Url; -use crate::inbox::new_inbox_routing::{ReceiveActivity, Activity, verify_domains_match}; -use activitystreams::activity::kind::FollowType; -use activitystreams::activity::kind::AcceptType; -use crate::activities::receive::verify_activity_domains_valid; -use activitystreams::base::ExtendsExt; +use crate::{ + activities::receive::verify_activity_domains_valid, + inbox::new_inbox_routing::{verify_domains_match, Activity, ReceiveActivity}, +}; +use activitystreams::{ + activity::kind::{AcceptType, FollowType}, + base::ExtendsExt, +}; use anyhow::Context; -use lemmy_apub::fetcher::community::get_or_fetch_and_upsert_community; use lemmy_api_common::blocking; -use lemmy_db_schema::source::community::CommunityFollower; -use lemmy_websocket::LemmyContext; -use lemmy_utils::LemmyError; -use lemmy_utils::location_info; +use lemmy_apub::fetcher::{ + community::get_or_fetch_and_upsert_community, + person::get_or_fetch_and_upsert_person, +}; use lemmy_db_queries::Followable; -use lemmy_apub::fetcher::person::get_or_fetch_and_upsert_person; +use lemmy_db_schema::source::community::CommunityFollower; +use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::LemmyContext; +use url::Url; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct Follow { - // todo: implement newtypes PersonUrl, GroupUrl etc (with deref function) - actor: Url, - to: Url, - object: Url, - #[serde(rename = "type")] - kind: FollowType, + // todo: implement newtypes PersonUrl, GroupUrl etc (with deref function) + actor: Url, + to: Url, + object: Url, + #[serde(rename = "type")] + kind: FollowType, } #[async_trait::async_trait(?Send)] -impl ReceiveActivity for Follow { - type Kind = FollowType; - async fn receive(&self,activity: Activity, context: &LemmyContext, request_counter: &mut i32) -> Result<(), LemmyError> { - println!("receive follow"); - todo!() - } +impl ReceiveActivity for Activity { + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + println!("receive follow"); + todo!() + } } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct Accept { - // todo: implement newtypes PersonUrl, GroupUrl etc (with deref function) - actor: Url, - to: Url, - object: Activity, - #[serde(rename = "type")] - kind: AcceptType, + // todo: implement newtypes PersonUrl, GroupUrl etc (with deref function) + actor: Url, + to: Url, + object: Activity, + #[serde(rename = "type")] + kind: AcceptType, } /// Handle accepted follows #[async_trait::async_trait(?Send)] -impl ReceiveActivity for Accept { - type Kind = AcceptType; - async fn receive(&self, activity: Activity, context: &LemmyContext, request_counter: &mut i32) -> Result<(), LemmyError> { - verify_domains_match(&self.actor, &activity.id_unchecked())?; - verify_domains_match(&self.object.inner.actor, &self.object.id_unchecked())?; +impl ReceiveActivity for Activity { + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + verify_domains_match(&self.inner.actor, self.id_unchecked())?; + let follow = &self.inner.object; + verify_domains_match(&follow.inner.actor, &follow.id_unchecked())?; - let community = - get_or_fetch_and_upsert_community(&self.actor, context, request_counter).await?; - let person = get_or_fetch_and_upsert_person(&self.to, context, request_counter).await?; - // This will throw an error if no follow was requested - blocking(&context.pool(), move |conn| { - CommunityFollower::follow_accepted(conn, community.id, person.id) - }) - .await??; + let community = + get_or_fetch_and_upsert_community(&self.inner.actor, context, request_counter).await?; + let person = get_or_fetch_and_upsert_person(&self.inner.to, context, request_counter).await?; + // This will throw an error if no follow was requested + blocking(&context.pool(), move |conn| { + CommunityFollower::follow_accepted(conn, community.id, person.id) + }) + .await??; - Ok(()) - } -} \ No newline at end of file + Ok(()) + } +} diff --git a/crates/apub_receive/src/activities_new/mod.rs b/crates/apub_receive/src/activities_new/mod.rs index 42eb8930..62d6fa2b 100644 --- a/crates/apub_receive/src/activities_new/mod.rs +++ b/crates/apub_receive/src/activities_new/mod.rs @@ -1 +1 @@ -pub mod follow; \ No newline at end of file +pub mod follow; diff --git a/crates/apub_receive/src/inbox/new_inbox_routing.rs b/crates/apub_receive/src/inbox/new_inbox_routing.rs index 2907b9b7..0f64951d 100644 --- a/crates/apub_receive/src/inbox/new_inbox_routing.rs +++ b/crates/apub_receive/src/inbox/new_inbox_routing.rs @@ -1,12 +1,14 @@ -use activitystreams::base::{AnyBase}; -use lemmy_utils::{LemmyError}; -use lemmy_websocket::LemmyContext; -use activitystreams::unparsed::Unparsed; -use activitystreams::primitives::{OneOrMany}; -use url::Url; 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 activitystreams::error::DomainError; +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: @@ -43,17 +45,21 @@ pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), LemmyError> { // todo: maybe add a separate method verify() #[async_trait::async_trait(?Send)] pub trait ReceiveActivity { - type Kind; - // todo: would be nice if we didnt have to pass Activity and Self separately // todo: later handle request_counter completely inside library - async fn receive(&self, activity: Activity, context: &LemmyContext, request_counter: &mut i32) -> Result<(), LemmyError>; + 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 url(self) -> Url { + self.0 + } pub fn dereference(self) -> Result { // todo: fetch object from http or database todo!() @@ -92,11 +98,13 @@ pub enum PersonAcceptedActivitiesNew { // todo: there should be a better way to do this (maybe needs a derive macro) #[async_trait::async_trait(?Send)] -impl ReceiveActivity for PersonAcceptedActivitiesNew { - async fn receive(&self, activity: Activity, context: &LemmyContext, request_counter: &mut i32) -> Result<(), LemmyError> { +impl ReceiveActivity for PersonAcceptedActivitiesNew { + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { use PersonAcceptedActivitiesNew::*; - match self { - Accept(a) => a.receive(activity, context, request_counter) - }.await + self.receive(context, request_counter).await } -} \ No newline at end of file +} diff --git a/crates/apub_receive/src/inbox/person_inbox.rs b/crates/apub_receive/src/inbox/person_inbox.rs index d61a173b..9b9d6518 100644 --- a/crates/apub_receive/src/inbox/person_inbox.rs +++ b/crates/apub_receive/src/inbox/person_inbox.rs @@ -23,6 +23,7 @@ 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, @@ -66,7 +67,6 @@ use serde::{Deserialize, Serialize}; use std::fmt::Debug; use strum_macros::EnumString; use url::Url; -use crate::inbox::new_inbox_routing::{PersonAcceptedActivitiesNew, ReceiveActivity, Activity}; /// Allowed activities for person inbox. #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)] @@ -154,8 +154,7 @@ pub(crate) async fn person_receive_message( let kind = activity.kind().context(location_info!())?; let actor_url = actor.actor_id(); match kind { - PersonValidTypes::Accept => { - } + PersonValidTypes::Accept => {} PersonValidTypes::Announce => { Box::pin(receive_announce(&context, any_base, actor, request_counter)).await? } diff --git a/crates/apub_receive/src/lib.rs b/crates/apub_receive/src/lib.rs index 999c178a..36f3ed67 100644 --- a/crates/apub_receive/src/lib.rs +++ b/crates/apub_receive/src/lib.rs @@ -1,5 +1,5 @@ mod activities; +pub mod activities_new; mod http; mod inbox; pub mod routes; -pub mod activities_new;