]> Untitled Git - lemmy.git/blob - crates/apub_receive/src/activities/following/accept.rs
Apub inbox rewrite (#1652)
[lemmy.git] / crates / apub_receive / src / activities / following / accept.rs
1 use crate::activities::{following::follow::FollowCommunity, verify_activity, verify_community};
2 use activitystreams::activity::kind::AcceptType;
3 use lemmy_api_common::blocking;
4 use lemmy_apub::fetcher::{
5   community::get_or_fetch_and_upsert_community,
6   person::get_or_fetch_and_upsert_person,
7 };
8 use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandler};
9 use lemmy_db_queries::Followable;
10 use lemmy_db_schema::source::community::CommunityFollower;
11 use lemmy_utils::LemmyError;
12 use lemmy_websocket::LemmyContext;
13 use url::Url;
14
15 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
16 #[serde(rename_all = "camelCase")]
17 pub struct AcceptFollowCommunity {
18   to: Url,
19   object: FollowCommunity,
20   #[serde(rename = "type")]
21   kind: AcceptType,
22   #[serde(flatten)]
23   common: ActivityCommonFields,
24 }
25
26 /// Handle accepted follows
27 #[async_trait::async_trait(?Send)]
28 impl ActivityHandler for AcceptFollowCommunity {
29   async fn verify(
30     &self,
31     context: &LemmyContext,
32     request_counter: &mut i32,
33   ) -> Result<(), LemmyError> {
34     verify_activity(self.common())?;
35     verify_urls_match(&self.to, &self.object.common.actor)?;
36     verify_urls_match(&self.common.actor, &self.object.to)?;
37     verify_community(&self.common.actor, context, request_counter).await?;
38     self.object.verify(context, request_counter).await?;
39     Ok(())
40   }
41
42   async fn receive(
43     &self,
44     context: &LemmyContext,
45     request_counter: &mut i32,
46   ) -> Result<(), LemmyError> {
47     let actor =
48       get_or_fetch_and_upsert_community(&self.common.actor, context, request_counter).await?;
49     let to = get_or_fetch_and_upsert_person(&self.to, context, request_counter).await?;
50     // This will throw an error if no follow was requested
51     blocking(context.pool(), move |conn| {
52       CommunityFollower::follow_accepted(conn, actor.id, to.id)
53     })
54     .await??;
55
56     Ok(())
57   }
58
59   fn common(&self) -> &ActivityCommonFields {
60     &self.common
61   }
62 }