3 community::announce::AnnouncableActivities,
7 verify_person_in_community,
9 activity_queue::send_to_community_new,
10 extensions::context::lemmy_context,
11 fetcher::object_id::ObjectId,
14 use activitystreams::{
15 activity::kind::BlockType,
17 primitives::OneOrMany,
20 use lemmy_api_common::blocking;
21 use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler};
22 use lemmy_db_queries::{Bannable, Followable};
23 use lemmy_db_schema::source::{
27 CommunityFollowerForm,
29 CommunityPersonBanForm,
33 use lemmy_utils::LemmyError;
34 use lemmy_websocket::LemmyContext;
35 use serde::{Deserialize, Serialize};
38 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
39 #[serde(rename_all = "camelCase")]
40 pub struct BlockUserFromCommunity {
41 actor: ObjectId<Person>,
43 pub(in crate::activities::community) object: ObjectId<Person>,
44 cc: [ObjectId<Community>; 1],
45 #[serde(rename = "type")]
48 #[serde(rename = "@context")]
49 context: OneOrMany<AnyBase>,
54 impl BlockUserFromCommunity {
55 pub(in crate::activities::community) fn new(
56 community: &Community,
59 ) -> Result<BlockUserFromCommunity, LemmyError> {
60 Ok(BlockUserFromCommunity {
61 actor: ObjectId::new(actor.actor_id()),
62 to: [PublicUrl::Public],
63 object: ObjectId::new(target.actor_id()),
64 cc: [ObjectId::new(community.actor_id())],
65 kind: BlockType::Block,
66 id: generate_activity_id(BlockType::Block)?,
67 context: lemmy_context(),
68 unparsed: Default::default(),
73 community: &Community,
76 context: &LemmyContext,
77 ) -> Result<(), LemmyError> {
78 let block = BlockUserFromCommunity::new(community, target, actor)?;
79 let block_id = block.id.clone();
81 let activity = AnnouncableActivities::BlockUserFromCommunity(block);
82 let inboxes = vec![target.get_shared_inbox_or_inbox_url()];
83 send_to_community_new(activity, &block_id, actor, community, inboxes, context).await
87 #[async_trait::async_trait(?Send)]
88 impl ActivityHandler for BlockUserFromCommunity {
91 context: &LemmyContext,
92 request_counter: &mut i32,
93 ) -> Result<(), LemmyError> {
94 verify_activity(self)?;
95 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
96 verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
102 context: &LemmyContext,
103 request_counter: &mut i32,
104 ) -> Result<(), LemmyError> {
105 let community = self.cc[0].dereference(context, request_counter).await?;
106 let blocked_user = self.object.dereference(context, request_counter).await?;
108 let community_user_ban_form = CommunityPersonBanForm {
109 community_id: community.id,
110 person_id: blocked_user.id,
113 blocking(context.pool(), move |conn: &'_ _| {
114 CommunityPersonBan::ban(conn, &community_user_ban_form)
118 // Also unsubscribe them from the community, if they are subscribed
119 let community_follower_form = CommunityFollowerForm {
120 community_id: community.id,
121 person_id: blocked_user.id,
124 blocking(context.pool(), move |conn: &'_ _| {
125 CommunityFollower::unfollow(conn, &community_follower_form)