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