2 activities::{generate_activity_id, send_lemmy_activity, verify_activity, verify_person},
3 fetcher::object_id::ObjectId,
4 objects::{community::ApubCommunity, person::ApubPerson},
5 protocol::activities::following::{follow::FollowCommunity, undo_follow::UndoFollowCommunity},
7 use activitystreams::activity::kind::UndoType;
8 use lemmy_api_common::blocking;
11 traits::{ActivityFields, ActivityHandler, ActorType},
12 verify::verify_urls_match,
14 use lemmy_db_schema::{
15 source::community::{CommunityFollower, CommunityFollowerForm},
18 use lemmy_utils::LemmyError;
19 use lemmy_websocket::LemmyContext;
21 impl UndoFollowCommunity {
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())],
33 id: generate_activity_id(
35 &context.settings().get_protocol_and_hostname(),
37 unparsed: Default::default(),
39 let inbox = vec![community.shared_inbox_or_inbox_url()];
40 send_lemmy_activity(context, &undo, &undo.id, actor, inbox, true).await
44 #[async_trait::async_trait(?Send)]
45 impl ActivityHandler for UndoFollowCommunity {
46 type DataType = LemmyContext;
49 context: &Data<LemmyContext>,
50 request_counter: &mut i32,
51 ) -> Result<(), LemmyError> {
52 verify_activity(self, &context.settings())?;
53 verify_urls_match(self.to[0].inner(), self.object.object.inner())?;
54 verify_urls_match(self.actor(), self.object.actor())?;
55 verify_person(&self.actor, context, request_counter).await?;
56 self.object.verify(context, request_counter).await?;
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?;
68 let community_follower_form = CommunityFollowerForm {
69 community_id: community.id,
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()