2 activities::{generate_activity_id, send_lemmy_activity, verify_person},
4 objects::{community::ApubCommunity, person::ApubPerson},
5 protocol::activities::following::{follow::FollowCommunity, undo_follow::UndoFollowCommunity},
8 use activitypub_federation::{
9 core::object_id::ObjectId,
11 traits::{ActivityHandler, Actor},
12 utils::verify_urls_match,
14 use activitystreams_kinds::activity::UndoType;
15 use lemmy_api_common::utils::blocking;
16 use lemmy_db_schema::{
17 source::community::{CommunityFollower, CommunityFollowerForm},
20 use lemmy_utils::error::LemmyError;
21 use lemmy_websocket::LemmyContext;
24 impl UndoFollowCommunity {
25 #[tracing::instrument(skip_all)]
28 community: &ApubCommunity,
29 context: &LemmyContext,
30 ) -> Result<(), LemmyError> {
31 let object = FollowCommunity::new(actor, community, context)?;
32 let undo = UndoFollowCommunity {
33 actor: ObjectId::new(actor.actor_id()),
36 id: generate_activity_id(
38 &context.settings().get_protocol_and_hostname(),
40 unparsed: Default::default(),
42 let inbox = vec![community.shared_inbox_or_inbox()];
43 send_lemmy_activity(context, undo, actor, inbox, true).await
47 #[async_trait::async_trait(?Send)]
48 impl ActivityHandler for UndoFollowCommunity {
49 type DataType = LemmyContext;
50 type Error = LemmyError;
52 fn id(&self) -> &Url {
56 fn actor(&self) -> &Url {
60 #[tracing::instrument(skip_all)]
63 context: &Data<LemmyContext>,
64 request_counter: &mut i32,
65 ) -> Result<(), LemmyError> {
66 verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
67 verify_person(&self.actor, context, request_counter).await?;
68 self.object.verify(context, request_counter).await?;
72 #[tracing::instrument(skip_all)]
75 context: &Data<LemmyContext>,
76 request_counter: &mut i32,
77 ) -> Result<(), LemmyError> {
80 .dereference(context, local_instance(context), request_counter)
85 .dereference(context, local_instance(context), request_counter)
88 let community_follower_form = CommunityFollowerForm {
89 community_id: community.id,
94 // This will fail if they aren't a follower, but ignore the error.
95 blocking(context.pool(), move |conn| {
96 CommunityFollower::unfollow(conn, &community_follower_form).ok()