]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/block_user.rs
Moving settings and secrets to context.
[lemmy.git] / crates / apub / src / activities / community / block_user.rs
1 use crate::{
2   activities::{
3     community::announce::AnnouncableActivities,
4     generate_activity_id,
5     verify_activity,
6     verify_mod_action,
7     verify_person_in_community,
8   },
9   activity_queue::send_to_community_new,
10   extensions::context::lemmy_context,
11   fetcher::object_id::ObjectId,
12   ActorType,
13 };
14 use activitystreams::{
15   activity::kind::BlockType,
16   base::AnyBase,
17   primitives::OneOrMany,
18   unparsed::Unparsed,
19 };
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::{
24   community::{
25     Community,
26     CommunityFollower,
27     CommunityFollowerForm,
28     CommunityPersonBan,
29     CommunityPersonBanForm,
30   },
31   person::Person,
32 };
33 use lemmy_utils::LemmyError;
34 use lemmy_websocket::LemmyContext;
35 use serde::{Deserialize, Serialize};
36 use url::Url;
37
38 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
39 #[serde(rename_all = "camelCase")]
40 pub struct BlockUserFromCommunity {
41   actor: ObjectId<Person>,
42   to: [PublicUrl; 1],
43   pub(in crate::activities::community) object: ObjectId<Person>,
44   cc: [ObjectId<Community>; 1],
45   #[serde(rename = "type")]
46   kind: BlockType,
47   id: Url,
48   #[serde(rename = "@context")]
49   context: OneOrMany<AnyBase>,
50   #[serde(flatten)]
51   unparsed: Unparsed,
52 }
53
54 impl BlockUserFromCommunity {
55   pub(in crate::activities::community) fn new(
56     community: &Community,
57     target: &Person,
58     actor: &Person,
59     context: &LemmyContext,
60   ) -> Result<BlockUserFromCommunity, LemmyError> {
61     Ok(BlockUserFromCommunity {
62       actor: ObjectId::new(actor.actor_id()),
63       to: [PublicUrl::Public],
64       object: ObjectId::new(target.actor_id()),
65       cc: [ObjectId::new(community.actor_id())],
66       kind: BlockType::Block,
67       id: generate_activity_id(
68         BlockType::Block,
69         &context.settings().get_protocol_and_hostname(),
70       )?,
71       context: lemmy_context(),
72       unparsed: Default::default(),
73     })
74   }
75
76   pub async fn send(
77     community: &Community,
78     target: &Person,
79     actor: &Person,
80     context: &LemmyContext,
81   ) -> Result<(), LemmyError> {
82     let block = BlockUserFromCommunity::new(community, target, actor, context)?;
83     let block_id = block.id.clone();
84
85     let activity = AnnouncableActivities::BlockUserFromCommunity(block);
86     let inboxes = vec![target.get_shared_inbox_or_inbox_url()];
87     send_to_community_new(activity, &block_id, actor, community, inboxes, context).await
88   }
89 }
90
91 #[async_trait::async_trait(?Send)]
92 impl ActivityHandler for BlockUserFromCommunity {
93   async fn verify(
94     &self,
95     context: &LemmyContext,
96     request_counter: &mut i32,
97   ) -> Result<(), LemmyError> {
98     verify_activity(self, &context.settings())?;
99     verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
100     verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
101     Ok(())
102   }
103
104   async fn receive(
105     self,
106     context: &LemmyContext,
107     request_counter: &mut i32,
108   ) -> Result<(), LemmyError> {
109     let community = self.cc[0].dereference(context, request_counter).await?;
110     let blocked_user = self.object.dereference(context, request_counter).await?;
111
112     let community_user_ban_form = CommunityPersonBanForm {
113       community_id: community.id,
114       person_id: blocked_user.id,
115     };
116
117     blocking(context.pool(), move |conn: &'_ _| {
118       CommunityPersonBan::ban(conn, &community_user_ban_form)
119     })
120     .await??;
121
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,
126       pending: false,
127     };
128     blocking(context.pool(), move |conn: &'_ _| {
129       CommunityFollower::unfollow(conn, &community_follower_form)
130     })
131     .await?
132     .ok();
133
134     Ok(())
135   }
136 }