]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/following/undo_follow.rs
Activitypub crate rewrite (#2782)
[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_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       object,
36       kind: UndoType::Undo,
37       id: generate_activity_id(
38         UndoType::Undo,
39         &context.settings().get_protocol_and_hostname(),
40       )?,
41     };
42     let inbox = vec![community.shared_inbox_or_inbox()];
43     send_lemmy_activity(context, undo, actor, inbox, true).await
44   }
45 }
46
47 #[async_trait::async_trait]
48 impl ActivityHandler for UndoFollow {
49   type DataType = LemmyContext;
50   type Error = LemmyError;
51
52   fn id(&self) -> &Url {
53     &self.id
54   }
55
56   fn actor(&self) -> &Url {
57     self.actor.inner()
58   }
59
60   #[tracing::instrument(skip_all)]
61   async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
62     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
63     verify_person(&self.actor, context).await?;
64     self.object.verify(context).await?;
65     Ok(())
66   }
67
68   #[tracing::instrument(skip_all)]
69   async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
70     insert_activity(&self.id, &self, false, true, context).await?;
71     let person = self.actor.dereference(context).await?;
72     let object = self.object.object.dereference(context).await?;
73
74     match object {
75       UserOrCommunity::User(u) => {
76         let form = PersonFollowerForm {
77           person_id: u.id,
78           follower_id: person.id,
79           pending: false,
80         };
81         PersonFollower::unfollow(context.pool(), &form).await?;
82       }
83       UserOrCommunity::Community(c) => {
84         let form = CommunityFollowerForm {
85           community_id: c.id,
86           person_id: person.id,
87           pending: false,
88         };
89         CommunityFollower::unfollow(context.pool(), &form).await?;
90       }
91     }
92
93     Ok(())
94   }
95 }