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_schema::{
29 CommunityFollowerForm,
31 CommunityPersonBanForm,
35 traits::{Bannable, Followable},
37 use lemmy_utils::LemmyError;
38 use lemmy_websocket::LemmyContext;
39 use serde::{Deserialize, Serialize};
42 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
43 #[serde(rename_all = "camelCase")]
44 pub struct BlockUserFromCommunity {
45 actor: ObjectId<Person>,
47 pub(in crate::activities::community) object: ObjectId<Person>,
48 cc: [ObjectId<Community>; 1],
49 #[serde(rename = "type")]
52 #[serde(rename = "@context")]
53 context: OneOrMany<AnyBase>,
58 impl BlockUserFromCommunity {
59 pub(in crate::activities::community) fn new(
60 community: &Community,
63 context: &LemmyContext,
64 ) -> Result<BlockUserFromCommunity, LemmyError> {
65 Ok(BlockUserFromCommunity {
66 actor: ObjectId::new(actor.actor_id()),
67 to: [PublicUrl::Public],
68 object: ObjectId::new(target.actor_id()),
69 cc: [ObjectId::new(community.actor_id())],
70 kind: BlockType::Block,
71 id: generate_activity_id(
73 &context.settings().get_protocol_and_hostname(),
75 context: lemmy_context(),
76 unparsed: Default::default(),
81 community: &Community,
84 context: &LemmyContext,
85 ) -> Result<(), LemmyError> {
86 let block = BlockUserFromCommunity::new(community, target, actor, context)?;
87 let block_id = block.id.clone();
89 let activity = AnnouncableActivities::BlockUserFromCommunity(block);
90 let inboxes = vec![target.shared_inbox_or_inbox_url()];
91 send_to_community(activity, &block_id, actor, community, inboxes, context).await
95 #[async_trait::async_trait(?Send)]
96 impl ActivityHandler for BlockUserFromCommunity {
97 type DataType = LemmyContext;
100 context: &Data<LemmyContext>,
101 request_counter: &mut i32,
102 ) -> Result<(), LemmyError> {
103 verify_activity(self, &context.settings())?;
104 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
105 verify_mod_action(&self.actor, self.cc[0].clone(), context, request_counter).await?;
111 context: &Data<LemmyContext>,
112 request_counter: &mut i32,
113 ) -> Result<(), LemmyError> {
114 let community = self.cc[0].dereference(context, request_counter).await?;
115 let blocked_user = self.object.dereference(context, request_counter).await?;
117 let community_user_ban_form = CommunityPersonBanForm {
118 community_id: community.id,
119 person_id: blocked_user.id,
122 blocking(context.pool(), move |conn: &'_ _| {
123 CommunityPersonBan::ban(conn, &community_user_ban_form)
127 // Also unsubscribe them from the community, if they are subscribed
128 let community_follower_form = CommunityFollowerForm {
129 community_id: community.id,
130 person_id: blocked_user.id,
133 blocking(context.pool(), move |conn: &'_ _| {
134 CommunityFollower::unfollow(conn, &community_follower_form)