]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/following/follow.rs
Merge websocket crate into api_common
[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   fetcher::user_or_community::UserOrCommunity,
9   local_instance,
10   objects::{community::ApubCommunity, person::ApubPerson},
11   protocol::activities::following::{accept::AcceptFollow, follow::Follow},
12   ActorType,
13 };
14 use activitypub_federation::{
15   core::object_id::ObjectId,
16   data::Data,
17   traits::{ActivityHandler, Actor},
18 };
19 use activitystreams_kinds::activity::FollowType;
20 use lemmy_api_common::LemmyContext;
21 use lemmy_db_schema::{
22   source::{
23     community::{CommunityFollower, CommunityFollowerForm},
24     person::{PersonFollower, PersonFollowerForm},
25   },
26   traits::Followable,
27 };
28 use lemmy_utils::error::LemmyError;
29 use url::Url;
30
31 impl Follow {
32   pub(in crate::activities::following) fn new(
33     actor: &ApubPerson,
34     community: &ApubCommunity,
35     context: &LemmyContext,
36   ) -> Result<Follow, LemmyError> {
37     Ok(Follow {
38       actor: ObjectId::new(actor.actor_id()),
39       object: ObjectId::new(community.actor_id()),
40       kind: FollowType::Follow,
41       id: generate_activity_id(
42         FollowType::Follow,
43         &context.settings().get_protocol_and_hostname(),
44       )?,
45     })
46   }
47
48   #[tracing::instrument(skip_all)]
49   pub async fn send(
50     actor: &ApubPerson,
51     community: &ApubCommunity,
52     context: &LemmyContext,
53   ) -> Result<(), LemmyError> {
54     let community_follower_form = CommunityFollowerForm {
55       community_id: community.id,
56       person_id: actor.id,
57       pending: true,
58     };
59     CommunityFollower::follow(context.pool(), &community_follower_form)
60       .await
61       .ok();
62
63     let follow = Follow::new(actor, community, context)?;
64     let inbox = vec![community.shared_inbox_or_inbox()];
65     send_lemmy_activity(context, follow, actor, inbox, true).await
66   }
67 }
68
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for Follow {
71   type DataType = LemmyContext;
72   type Error = LemmyError;
73
74   fn id(&self) -> &Url {
75     &self.id
76   }
77
78   fn actor(&self) -> &Url {
79     self.actor.inner()
80   }
81
82   #[tracing::instrument(skip_all)]
83   async fn verify(
84     &self,
85     context: &Data<LemmyContext>,
86     request_counter: &mut i32,
87   ) -> Result<(), LemmyError> {
88     verify_person(&self.actor, context, request_counter).await?;
89     let object = self
90       .object
91       .dereference(context, local_instance(context).await, request_counter)
92       .await?;
93     if let UserOrCommunity::Community(c) = object {
94       verify_person_in_community(&self.actor, &c, context, request_counter).await?;
95     }
96     Ok(())
97   }
98
99   #[tracing::instrument(skip_all)]
100   async fn receive(
101     self,
102     context: &Data<LemmyContext>,
103     request_counter: &mut i32,
104   ) -> Result<(), LemmyError> {
105     let actor = self
106       .actor
107       .dereference(context, local_instance(context).await, request_counter)
108       .await?;
109     let object = self
110       .object
111       .dereference(context, local_instance(context).await, request_counter)
112       .await?;
113     match object {
114       UserOrCommunity::User(u) => {
115         let form = PersonFollowerForm {
116           person_id: u.id,
117           follower_id: actor.id,
118           pending: false,
119         };
120         PersonFollower::follow(context.pool(), &form).await?;
121       }
122       UserOrCommunity::Community(c) => {
123         let form = CommunityFollowerForm {
124           community_id: c.id,
125           person_id: actor.id,
126           pending: false,
127         };
128         CommunityFollower::follow(context.pool(), &form).await?;
129       }
130     }
131
132     AcceptFollow::send(self, context, request_counter).await
133   }
134 }