4 announce::{AnnouncableActivities, GetCommunity},
11 verify_person_in_community,
13 fetcher::object_id::ObjectId,
14 objects::{community::ApubCommunity, person::ApubPerson},
16 use activitystreams::{activity::kind::BlockType, public, unparsed::Unparsed};
17 use lemmy_api_common::blocking;
20 traits::{ActivityFields, ActivityHandler, ActorType},
22 use lemmy_db_schema::{
25 CommunityFollowerForm,
27 CommunityPersonBanForm,
29 traits::{Bannable, Followable},
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::LemmyContext;
33 use serde::{Deserialize, Serialize};
36 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
37 #[serde(rename_all = "camelCase")]
38 pub struct BlockUserFromCommunity {
39 actor: ObjectId<ApubPerson>,
41 pub(in crate::activities::community) object: ObjectId<ApubPerson>,
43 target: ObjectId<ApubCommunity>,
44 #[serde(rename = "type")]
51 impl BlockUserFromCommunity {
52 pub(in crate::activities::community) fn new(
53 community: &ApubCommunity,
56 context: &LemmyContext,
57 ) -> Result<BlockUserFromCommunity, LemmyError> {
58 Ok(BlockUserFromCommunity {
59 actor: ObjectId::new(actor.actor_id()),
61 object: ObjectId::new(target.actor_id()),
62 cc: vec![community.actor_id()],
63 target: ObjectId::new(community.actor_id()),
64 kind: BlockType::Block,
65 id: generate_activity_id(
67 &context.settings().get_protocol_and_hostname(),
69 unparsed: Default::default(),
74 community: &ApubCommunity,
77 context: &LemmyContext,
78 ) -> Result<(), LemmyError> {
79 let block = BlockUserFromCommunity::new(community, target, actor, context)?;
80 let block_id = block.id.clone();
82 let activity = AnnouncableActivities::BlockUserFromCommunity(block);
83 let inboxes = vec![target.shared_inbox_or_inbox_url()];
84 send_to_community(activity, &block_id, actor, community, inboxes, context).await
88 #[async_trait::async_trait(?Send)]
89 impl ActivityHandler for BlockUserFromCommunity {
90 type DataType = LemmyContext;
93 context: &Data<LemmyContext>,
94 request_counter: &mut i32,
95 ) -> Result<(), LemmyError> {
96 verify_is_public(&self.to)?;
97 verify_activity(self, &context.settings())?;
98 let community = self.get_community(context, request_counter).await?;
99 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
100 verify_mod_action(&self.actor, &community, context, request_counter).await?;
106 context: &Data<LemmyContext>,
107 request_counter: &mut i32,
108 ) -> Result<(), LemmyError> {
109 let community = self.get_community(context, request_counter).await?;
110 let blocked_user = self.object.dereference(context, request_counter).await?;
112 let community_user_ban_form = CommunityPersonBanForm {
113 community_id: community.id,
114 person_id: blocked_user.id,
117 blocking(context.pool(), move |conn: &'_ _| {
118 CommunityPersonBan::ban(conn, &community_user_ban_form)
122 // Also unsubscribe them from the community, if they are subscribed
123 let community_follower_form = CommunityFollowerForm {
124 community_id: community.id,
125 person_id: blocked_user.id,
128 blocking(context.pool(), move |conn: &'_ _| {
129 CommunityFollower::unfollow(conn, &community_follower_form)
138 #[async_trait::async_trait(?Send)]
139 impl GetCommunity for BlockUserFromCommunity {
140 async fn get_community(
142 context: &LemmyContext,
143 request_counter: &mut i32,
144 ) -> Result<ApubCommunity, LemmyError> {
145 self.target.dereference(context, request_counter).await