]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/following/undo_follow.rs
Split activity table into sent and received parts (fixes #3103) (#3583)
[lemmy.git] / crates / apub / src / activities / following / undo_follow.rs
1 use crate::{
2   activities::{generate_activity_id, send_lemmy_activity, verify_person},
3   fetcher::user_or_community::UserOrCommunity,
4   insert_received_activity,
5   objects::{community::ApubCommunity, person::ApubPerson},
6   protocol::activities::following::{follow::Follow, undo_follow::UndoFollow},
7 };
8 use activitypub_federation::{
9   config::Data,
10   kinds::activity::UndoType,
11   protocol::verification::verify_urls_match,
12   traits::{ActivityHandler, Actor},
13 };
14 use lemmy_api_common::context::LemmyContext;
15 use lemmy_db_schema::{
16   source::{
17     community::{CommunityFollower, CommunityFollowerForm},
18     person::{PersonFollower, PersonFollowerForm},
19   },
20   traits::Followable,
21 };
22 use lemmy_utils::error::LemmyError;
23 use url::Url;
24
25 impl UndoFollow {
26   #[tracing::instrument(skip_all)]
27   pub async fn send(
28     actor: &ApubPerson,
29     community: &ApubCommunity,
30     context: &Data<LemmyContext>,
31   ) -> Result<(), LemmyError> {
32     let object = Follow::new(actor, community, context)?;
33     let undo = UndoFollow {
34       actor: actor.id().into(),
35       to: Some([community.id().into()]),
36       object,
37       kind: UndoType::Undo,
38       id: generate_activity_id(
39         UndoType::Undo,
40         &context.settings().get_protocol_and_hostname(),
41       )?,
42     };
43     let inbox = vec![community.shared_inbox_or_inbox()];
44     send_lemmy_activity(context, undo, actor, inbox, true).await
45   }
46 }
47
48 #[async_trait::async_trait]
49 impl ActivityHandler for UndoFollow {
50   type DataType = LemmyContext;
51   type Error = LemmyError;
52
53   fn id(&self) -> &Url {
54     &self.id
55   }
56
57   fn actor(&self) -> &Url {
58     self.actor.inner()
59   }
60
61   #[tracing::instrument(skip_all)]
62   async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
63     insert_received_activity(&self.id, context).await?;
64     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
65     verify_person(&self.actor, context).await?;
66     self.object.verify(context).await?;
67     if let Some(to) = &self.to {
68       verify_urls_match(to[0].inner(), self.object.object.inner())?;
69     }
70     Ok(())
71   }
72
73   #[tracing::instrument(skip_all)]
74   async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
75     let person = self.actor.dereference(context).await?;
76     let object = self.object.object.dereference(context).await?;
77
78     match object {
79       UserOrCommunity::User(u) => {
80         let form = PersonFollowerForm {
81           person_id: u.id,
82           follower_id: person.id,
83           pending: false,
84         };
85         PersonFollower::unfollow(&mut context.pool(), &form).await?;
86       }
87       UserOrCommunity::Community(c) => {
88         let form = CommunityFollowerForm {
89           community_id: c.id,
90           person_id: person.id,
91           pending: false,
92         };
93         CommunityFollower::unfollow(&mut context.pool(), &form).await?;
94       }
95     }
96
97     Ok(())
98   }
99 }