2 activities::{generate_activity_id, send_lemmy_activity, verify_person},
6 objects::{community::ApubCommunity, person::ApubPerson},
7 protocol::activities::following::{follow::FollowCommunity, undo_follow::UndoFollowCommunity},
10 use activitypub_federation::{
11 core::object_id::ObjectId,
13 traits::{ActivityHandler, Actor},
14 utils::verify_urls_match,
16 use activitystreams_kinds::activity::UndoType;
17 use lemmy_api_common::utils::blocking;
18 use lemmy_db_schema::{
19 source::community::{CommunityFollower, CommunityFollowerForm},
22 use lemmy_utils::error::LemmyError;
23 use lemmy_websocket::LemmyContext;
26 impl UndoFollowCommunity {
27 #[tracing::instrument(skip_all)]
30 community: &ApubCommunity,
31 context: &LemmyContext,
32 ) -> Result<(), LemmyError> {
33 let object = FollowCommunity::new(actor, community, context)?;
34 let undo = UndoFollowCommunity {
35 actor: ObjectId::new(actor.actor_id()),
38 id: generate_activity_id(
40 &context.settings().get_protocol_and_hostname(),
42 unparsed: Default::default(),
44 let inbox = vec![community.shared_inbox_or_inbox()];
45 send_lemmy_activity(context, undo, actor, inbox, true).await
49 #[async_trait::async_trait(?Send)]
50 impl ActivityHandler for UndoFollowCommunity {
51 type DataType = LemmyContext;
52 type Error = LemmyError;
54 fn id(&self) -> &Url {
58 fn actor(&self) -> &Url {
62 #[tracing::instrument(skip_all)]
65 context: &Data<LemmyContext>,
66 request_counter: &mut i32,
67 ) -> Result<(), LemmyError> {
68 let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
69 check_apub_id_valid(self.id(), &local_site_data, context.settings())
70 .map_err(LemmyError::from_message)?;
71 verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
72 verify_person(&self.actor, context, request_counter).await?;
73 self.object.verify(context, request_counter).await?;
77 #[tracing::instrument(skip_all)]
80 context: &Data<LemmyContext>,
81 request_counter: &mut i32,
82 ) -> Result<(), LemmyError> {
85 .dereference(context, local_instance(context), request_counter)
90 .dereference(context, local_instance(context), request_counter)
93 let community_follower_form = CommunityFollowerForm {
94 community_id: community.id,
99 // This will fail if they aren't a follower, but ignore the error.
100 blocking(context.pool(), move |conn| {
101 CommunityFollower::unfollow(conn, &community_follower_form).ok()