3 community::{announce::AnnouncableActivities, send_to_community},
7 verify_person_in_community,
9 context::lemmy_context,
10 fetcher::object_id::ObjectId,
12 use activitystreams::{
13 activity::kind::BlockType,
15 primitives::OneOrMany,
18 use lemmy_api_common::blocking;
21 traits::{ActivityFields, ActivityHandler, ActorType},
24 use lemmy_db_queries::{Bannable, Followable};
25 use lemmy_db_schema::source::{
29 CommunityFollowerForm,
31 CommunityPersonBanForm,
35 use lemmy_utils::LemmyError;
36 use lemmy_websocket::LemmyContext;
37 use serde::{Deserialize, Serialize};
40 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
41 #[serde(rename_all = "camelCase")]
42 pub struct BlockUserFromCommunity {
43 actor: ObjectId<Person>,
45 pub(in crate::activities::community) object: ObjectId<Person>,
46 cc: [ObjectId<Community>; 1],
47 #[serde(rename = "type")]
50 #[serde(rename = "@context")]
51 context: OneOrMany<AnyBase>,
56 impl BlockUserFromCommunity {
57 pub(in crate::activities::community) fn new(
58 community: &Community,
61 context: &LemmyContext,
62 ) -> Result<BlockUserFromCommunity, LemmyError> {
63 Ok(BlockUserFromCommunity {
64 actor: ObjectId::new(actor.actor_id()),
65 to: [PublicUrl::Public],
66 object: ObjectId::new(target.actor_id()),
67 cc: [ObjectId::new(community.actor_id())],
68 kind: BlockType::Block,
69 id: generate_activity_id(
71 &context.settings().get_protocol_and_hostname(),
73 context: lemmy_context(),
74 unparsed: Default::default(),
79 community: &Community,
82 context: &LemmyContext,
83 ) -> Result<(), LemmyError> {
84 let block = BlockUserFromCommunity::new(community, target, actor, context)?;
85 let block_id = block.id.clone();
87 let activity = AnnouncableActivities::BlockUserFromCommunity(block);
88 let inboxes = vec![target.shared_inbox_or_inbox_url()];
89 send_to_community(activity, &block_id, actor, community, inboxes, context).await
93 #[async_trait::async_trait(?Send)]
94 impl ActivityHandler for BlockUserFromCommunity {
95 type DataType = LemmyContext;
98 context: &Data<LemmyContext>,
99 request_counter: &mut i32,
100 ) -> Result<(), LemmyError> {
101 verify_activity(self, &context.settings())?;
102 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
103 verify_mod_action(&self.actor, self.cc[0].clone(), context, request_counter).await?;
109 context: &Data<LemmyContext>,
110 request_counter: &mut i32,
111 ) -> Result<(), LemmyError> {
112 let community = self.cc[0].dereference(context, request_counter).await?;
113 let blocked_user = self.object.dereference(context, request_counter).await?;
115 let community_user_ban_form = CommunityPersonBanForm {
116 community_id: community.id,
117 person_id: blocked_user.id,
120 blocking(context.pool(), move |conn: &'_ _| {
121 CommunityPersonBan::ban(conn, &community_user_ban_form)
125 // Also unsubscribe them from the community, if they are subscribed
126 let community_follower_form = CommunityFollowerForm {
127 community_id: community.id,
128 person_id: blocked_user.id,
131 blocking(context.pool(), move |conn: &'_ _| {
132 CommunityFollower::unfollow(conn, &community_follower_form)