]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/following/undo_follow.rs
Merge pull request #1877 from LemmyNet/refactor-apub-2
[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::activity::kind::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   pub async fn send(
23     actor: &ApubPerson,
24     community: &ApubCommunity,
25     context: &LemmyContext,
26   ) -> Result<(), LemmyError> {
27     let object = FollowCommunity::new(actor, community, context)?;
28     let undo = UndoFollowCommunity {
29       actor: ObjectId::new(actor.actor_id()),
30       to: [ObjectId::new(community.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   async fn verify(
48     &self,
49     context: &Data<LemmyContext>,
50     request_counter: &mut i32,
51   ) -> Result<(), LemmyError> {
52     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
53     verify_urls_match(self.to[0].inner(), self.object.object.inner())?;
54     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
55     verify_person(&self.actor, context, request_counter).await?;
56     self.object.verify(context, request_counter).await?;
57     Ok(())
58   }
59
60   async fn receive(
61     self,
62     context: &Data<LemmyContext>,
63     request_counter: &mut i32,
64   ) -> Result<(), LemmyError> {
65     let actor = self.actor.dereference(context, request_counter).await?;
66     let community = self.to[0].dereference(context, request_counter).await?;
67
68     let community_follower_form = CommunityFollowerForm {
69       community_id: community.id,
70       person_id: actor.id,
71       pending: false,
72     };
73
74     // This will fail if they aren't a follower, but ignore the error.
75     blocking(context.pool(), move |conn| {
76       CommunityFollower::unfollow(conn, &community_follower_form).ok()
77     })
78     .await?;
79     Ok(())
80   }
81 }