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