]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/following/follow.rs
f78fadff0a9e010fa862e2ba1b6237e414892a04
[lemmy.git] / crates / apub / src / activities / following / follow.rs
1 use crate::{
2   activities::{
3     generate_activity_id,
4     send_lemmy_activity,
5     verify_person,
6     verify_person_in_community,
7   },
8   local_instance,
9   objects::{community::ApubCommunity, person::ApubPerson},
10   protocol::activities::following::{accept::AcceptFollowCommunity, follow::FollowCommunity},
11   ActorType,
12 };
13 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
14 use activitystreams_kinds::activity::FollowType;
15 use lemmy_api_common::utils::blocking;
16 use lemmy_db_schema::{
17   source::community::{CommunityFollower, CommunityFollowerForm},
18   traits::Followable,
19 };
20 use lemmy_utils::error::LemmyError;
21 use lemmy_websocket::LemmyContext;
22 use url::Url;
23
24 impl FollowCommunity {
25   pub(in crate::activities::following) fn new(
26     actor: &ApubPerson,
27     community: &ApubCommunity,
28     context: &LemmyContext,
29   ) -> Result<FollowCommunity, LemmyError> {
30     Ok(FollowCommunity {
31       actor: ObjectId::new(actor.actor_id()),
32       object: ObjectId::new(community.actor_id()),
33       kind: FollowType::Follow,
34       id: generate_activity_id(
35         FollowType::Follow,
36         &context.settings().get_protocol_and_hostname(),
37       )?,
38       unparsed: Default::default(),
39     })
40   }
41
42   #[tracing::instrument(skip_all)]
43   pub async fn send(
44     actor: &ApubPerson,
45     community: &ApubCommunity,
46     context: &LemmyContext,
47   ) -> Result<(), LemmyError> {
48     let community_follower_form = CommunityFollowerForm {
49       community_id: community.id,
50       person_id: actor.id,
51       pending: true,
52     };
53     blocking(context.pool(), move |conn| {
54       CommunityFollower::follow(conn, &community_follower_form).ok()
55     })
56     .await?;
57
58     let follow = FollowCommunity::new(actor, community, context)?;
59     let inbox = vec![community.inbox_url.clone().into()];
60     send_lemmy_activity(context, &follow, &follow.id, actor, inbox, true).await
61   }
62 }
63
64 #[async_trait::async_trait(?Send)]
65 impl ActivityHandler for FollowCommunity {
66   type DataType = LemmyContext;
67   type Error = LemmyError;
68
69   fn id(&self) -> &Url {
70     &self.id
71   }
72
73   fn actor(&self) -> &Url {
74     self.actor.inner()
75   }
76
77   #[tracing::instrument(skip_all)]
78   async fn verify(
79     &self,
80     context: &Data<LemmyContext>,
81     request_counter: &mut i32,
82   ) -> Result<(), LemmyError> {
83     verify_person(&self.actor, context, request_counter).await?;
84     let community = self
85       .object
86       .dereference::<LemmyError>(context, local_instance(context), request_counter)
87       .await?;
88     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
89     Ok(())
90   }
91
92   #[tracing::instrument(skip_all)]
93   async fn receive(
94     self,
95     context: &Data<LemmyContext>,
96     request_counter: &mut i32,
97   ) -> Result<(), LemmyError> {
98     let person = self
99       .actor
100       .dereference::<LemmyError>(context, local_instance(context), request_counter)
101       .await?;
102     let community = self
103       .object
104       .dereference::<LemmyError>(context, local_instance(context), request_counter)
105       .await?;
106     let community_follower_form = CommunityFollowerForm {
107       community_id: community.id,
108       person_id: person.id,
109       pending: false,
110     };
111
112     // This will fail if they're already a follower, but ignore the error.
113     blocking(context.pool(), move |conn| {
114       CommunityFollower::follow(conn, &community_follower_form).ok()
115     })
116     .await?;
117
118     AcceptFollowCommunity::send(self, context, request_counter).await
119   }
120 }