3 community::{announce::AnnouncableActivities, send_to_community},
8 verify_person_in_community,
10 context::lemmy_context,
11 fetcher::object_id::ObjectId,
12 objects::{community::ApubCommunity, person::ApubPerson},
14 use activitystreams::{
15 activity::kind::BlockType,
17 primitives::OneOrMany,
21 use lemmy_api_common::blocking;
24 traits::{ActivityFields, ActivityHandler, ActorType},
26 use lemmy_db_schema::{
29 CommunityFollowerForm,
31 CommunityPersonBanForm,
33 traits::{Bannable, Followable},
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<ApubPerson>,
45 pub(in crate::activities::community) object: ObjectId<ApubPerson>,
46 cc: [ObjectId<ApubCommunity>; 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: &ApubCommunity,
61 context: &LemmyContext,
62 ) -> Result<BlockUserFromCommunity, LemmyError> {
63 Ok(BlockUserFromCommunity {
64 actor: ObjectId::new(actor.actor_id()),
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: &ApubCommunity,
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_is_public(&self.to)?;
102 verify_activity(self, &context.settings())?;
103 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
104 verify_mod_action(&self.actor, &self.cc[0], context, request_counter).await?;
110 context: &Data<LemmyContext>,
111 request_counter: &mut i32,
112 ) -> Result<(), LemmyError> {
113 let community = self.cc[0].dereference(context, request_counter).await?;
114 let blocked_user = self.object.dereference(context, request_counter).await?;
116 let community_user_ban_form = CommunityPersonBanForm {
117 community_id: community.id,
118 person_id: blocked_user.id,
121 blocking(context.pool(), move |conn: &'_ _| {
122 CommunityPersonBan::ban(conn, &community_user_ban_form)
126 // Also unsubscribe them from the community, if they are subscribed
127 let community_follower_form = CommunityFollowerForm {
128 community_id: community.id,
129 person_id: blocked_user.id,
132 blocking(context.pool(), move |conn: &'_ _| {
133 CommunityFollower::unfollow(conn, &community_follower_form)