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