3 following::follow::FollowCommunity,
8 activity_queue::send_activity_new,
9 extensions::context::lemmy_context,
10 fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
13 use activitystreams::{
14 activity::kind::UndoType,
16 primitives::OneOrMany,
19 use lemmy_api_common::blocking;
20 use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler};
21 use lemmy_db_queries::Followable;
22 use lemmy_db_schema::source::{
23 community::{Community, CommunityFollower, CommunityFollowerForm},
26 use lemmy_utils::LemmyError;
27 use lemmy_websocket::LemmyContext;
28 use serde::{Deserialize, Serialize};
31 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
32 #[serde(rename_all = "camelCase")]
33 pub struct UndoFollowCommunity {
36 object: FollowCommunity,
37 #[serde(rename = "type")]
40 #[serde(rename = "@context")]
41 context: OneOrMany<AnyBase>,
46 impl UndoFollowCommunity {
49 community: &Community,
50 context: &LemmyContext,
51 ) -> Result<(), LemmyError> {
52 let object = FollowCommunity::new(actor, community)?;
53 let undo = UndoFollowCommunity {
54 actor: actor.actor_id(),
55 to: community.actor_id(),
58 id: generate_activity_id(UndoType::Undo)?,
59 context: lemmy_context(),
60 unparsed: Default::default(),
62 let inbox = vec![community.get_shared_inbox_or_inbox_url()];
63 send_activity_new(context, &undo, &undo.id, actor, inbox, true).await
67 #[async_trait::async_trait(?Send)]
68 impl ActivityHandler for UndoFollowCommunity {
71 context: &LemmyContext,
72 request_counter: &mut i32,
73 ) -> Result<(), LemmyError> {
74 verify_activity(self)?;
75 verify_urls_match(&self.to, &self.object.object)?;
76 verify_urls_match(&self.actor, self.object.actor())?;
77 verify_person(&self.actor, context, request_counter).await?;
78 self.object.verify(context, request_counter).await?;
84 context: &LemmyContext,
85 request_counter: &mut i32,
86 ) -> Result<(), LemmyError> {
87 let actor = get_or_fetch_and_upsert_person(&self.actor, context, request_counter).await?;
88 let community = get_or_fetch_and_upsert_community(&self.to, context, request_counter).await?;
90 let community_follower_form = CommunityFollowerForm {
91 community_id: community.id,
96 // This will fail if they aren't a follower, but ignore the error.
97 blocking(context.pool(), move |conn| {
98 CommunityFollower::unfollow(conn, &community_follower_form).ok()