]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/block_user.rs
Move @context out of object/activity definitions
[lemmy.git] / crates / apub / src / activities / community / block_user.rs
1 use crate::{
2   activities::{
3     community::{
4       announce::{AnnouncableActivities, GetCommunity},
5       send_to_community,
6     },
7     generate_activity_id,
8     verify_activity,
9     verify_is_public,
10     verify_mod_action,
11     verify_person_in_community,
12   },
13   fetcher::object_id::ObjectId,
14   objects::{community::ApubCommunity, person::ApubPerson},
15 };
16 use activitystreams::{activity::kind::BlockType, public, unparsed::Unparsed};
17 use lemmy_api_common::blocking;
18 use lemmy_apub_lib::{
19   data::Data,
20   traits::{ActivityFields, ActivityHandler, ActorType},
21 };
22 use lemmy_db_schema::{
23   source::community::{
24     CommunityFollower,
25     CommunityFollowerForm,
26     CommunityPersonBan,
27     CommunityPersonBanForm,
28   },
29   traits::{Bannable, Followable},
30 };
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::LemmyContext;
33 use serde::{Deserialize, Serialize};
34 use url::Url;
35
36 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
37 #[serde(rename_all = "camelCase")]
38 pub struct BlockUserFromCommunity {
39   actor: ObjectId<ApubPerson>,
40   to: Vec<Url>,
41   pub(in crate::activities::community) object: ObjectId<ApubPerson>,
42   cc: Vec<Url>,
43   target: ObjectId<ApubCommunity>,
44   #[serde(rename = "type")]
45   kind: BlockType,
46   id: Url,
47   #[serde(flatten)]
48   unparsed: Unparsed,
49 }
50
51 impl BlockUserFromCommunity {
52   pub(in crate::activities::community) fn new(
53     community: &ApubCommunity,
54     target: &ApubPerson,
55     actor: &ApubPerson,
56     context: &LemmyContext,
57   ) -> Result<BlockUserFromCommunity, LemmyError> {
58     Ok(BlockUserFromCommunity {
59       actor: ObjectId::new(actor.actor_id()),
60       to: vec![public()],
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(
66         BlockType::Block,
67         &context.settings().get_protocol_and_hostname(),
68       )?,
69       unparsed: Default::default(),
70     })
71   }
72
73   pub async fn send(
74     community: &ApubCommunity,
75     target: &ApubPerson,
76     actor: &ApubPerson,
77     context: &LemmyContext,
78   ) -> Result<(), LemmyError> {
79     let block = BlockUserFromCommunity::new(community, target, actor, context)?;
80     let block_id = block.id.clone();
81
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
85   }
86 }
87
88 #[async_trait::async_trait(?Send)]
89 impl ActivityHandler for BlockUserFromCommunity {
90   type DataType = LemmyContext;
91   async fn verify(
92     &self,
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?;
101     Ok(())
102   }
103
104   async fn receive(
105     self,
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?;
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 }
137
138 #[async_trait::async_trait(?Send)]
139 impl GetCommunity for BlockUserFromCommunity {
140   async fn get_community(
141     &self,
142     context: &LemmyContext,
143     request_counter: &mut i32,
144   ) -> Result<ApubCommunity, LemmyError> {
145     self.target.dereference(context, request_counter).await
146   }
147 }