3 block::{generate_cc, SiteOrCommunity},
4 community::{announce::GetCommunity, send_activity_in_community},
9 verify_person_in_community,
11 activity_lists::AnnouncableActivities,
13 fetch_local_site_data,
15 objects::{community::ApubCommunity, instance::remote_instance_inboxes, person::ApubPerson},
16 protocol::activities::block::block_user::BlockUser,
19 use activitypub_federation::{
20 core::object_id::ObjectId,
22 traits::{ActivityHandler, Actor},
23 utils::verify_domains_match,
25 use activitystreams_kinds::{activity::BlockType, public};
27 use chrono::NaiveDateTime;
28 use lemmy_api_common::utils::{blocking, remove_user_data, remove_user_data_in_community};
29 use lemmy_db_schema::{
33 CommunityFollowerForm,
35 CommunityPersonBanForm,
37 moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
38 person::{Person, PersonUpdateForm},
40 traits::{Bannable, Crud, Followable},
42 use lemmy_utils::{error::LemmyError, utils::convert_datetime};
43 use lemmy_websocket::LemmyContext;
47 pub(in crate::activities::block) async fn new(
48 target: &SiteOrCommunity,
51 remove_data: Option<bool>,
52 reason: Option<String>,
53 expires: Option<NaiveDateTime>,
54 context: &LemmyContext,
55 ) -> Result<BlockUser, LemmyError> {
57 actor: ObjectId::new(mod_.actor_id()),
59 object: ObjectId::new(user.actor_id()),
60 cc: generate_cc(target, context.pool()).await?,
62 kind: BlockType::Block,
65 id: generate_activity_id(
67 &context.settings().get_protocol_and_hostname(),
69 expires: expires.map(convert_datetime),
70 unparsed: Default::default(),
74 #[tracing::instrument(skip_all)]
76 target: &SiteOrCommunity,
80 reason: Option<String>,
81 expires: Option<NaiveDateTime>,
82 context: &LemmyContext,
83 ) -> Result<(), LemmyError> {
84 let block = BlockUser::new(
96 SiteOrCommunity::Site(_) => {
97 let inboxes = remote_instance_inboxes(context.pool()).await?;
98 send_lemmy_activity(context, block, mod_, inboxes, false).await
100 SiteOrCommunity::Community(c) => {
101 let activity = AnnouncableActivities::BlockUser(block);
102 let inboxes = vec![user.shared_inbox_or_inbox()];
103 send_activity_in_community(activity, mod_, c, inboxes, context).await
109 #[async_trait::async_trait(?Send)]
110 impl ActivityHandler for BlockUser {
111 type DataType = LemmyContext;
112 type Error = LemmyError;
114 fn id(&self) -> &Url {
118 fn actor(&self) -> &Url {
122 #[tracing::instrument(skip_all)]
125 context: &Data<LemmyContext>,
126 request_counter: &mut i32,
127 ) -> Result<(), LemmyError> {
128 let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
129 check_apub_id_valid(self.id(), &local_site_data, context.settings())
130 .map_err(LemmyError::from_message)?;
132 verify_is_public(&self.to, &self.cc)?;
135 .dereference(context, local_instance(context), request_counter)
138 SiteOrCommunity::Site(site) => {
139 let domain = self.object.inner().domain().expect("url needs domain");
140 if context.settings().hostname == domain {
142 anyhow!("Site bans from remote instance can't affect user's home instance").into(),
145 // site ban can only target a user who is on the same instance as the actor (admin)
146 verify_domains_match(&site.actor_id(), self.actor.inner())?;
147 verify_domains_match(&site.actor_id(), self.object.inner())?;
149 SiteOrCommunity::Community(community) => {
150 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
164 #[tracing::instrument(skip_all)]
167 context: &Data<LemmyContext>,
168 request_counter: &mut i32,
169 ) -> Result<(), LemmyError> {
170 let expires = self.expires.map(|u| u.naive_local());
171 let mod_person = self
173 .dereference(context, local_instance(context), request_counter)
175 let blocked_person = self
177 .dereference(context, local_instance(context), request_counter)
181 .dereference(context, local_instance(context), request_counter)
184 SiteOrCommunity::Site(_site) => {
185 let blocked_person = blocking(context.pool(), move |conn| {
189 &PersonUpdateForm::builder()
191 .ban_expires(Some(expires))
196 if self.remove_data.unwrap_or(false) {
207 let form = ModBanForm {
208 mod_person_id: mod_person.id,
209 other_person_id: blocked_person.id,
210 reason: self.summary,
214 blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
216 SiteOrCommunity::Community(community) => {
217 let community_user_ban_form = CommunityPersonBanForm {
218 community_id: community.id,
219 person_id: blocked_person.id,
220 expires: Some(expires),
222 blocking(context.pool(), move |conn| {
223 CommunityPersonBan::ban(conn, &community_user_ban_form)
227 // Also unsubscribe them from the community, if they are subscribed
228 let community_follower_form = CommunityFollowerForm {
229 community_id: community.id,
230 person_id: blocked_person.id,
233 blocking(context.pool(), move |conn: &mut _| {
234 CommunityFollower::unfollow(conn, &community_follower_form)
239 if self.remove_data.unwrap_or(false) {
240 remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
244 let form = ModBanFromCommunityForm {
245 mod_person_id: mod_person.id,
246 other_person_id: blocked_person.id,
247 community_id: community.id,
248 reason: self.summary,
252 blocking(context.pool(), move |conn| {
253 ModBanFromCommunity::create(conn, &form)
263 #[async_trait::async_trait(?Send)]
264 impl GetCommunity for BlockUser {
265 #[tracing::instrument(skip_all)]
266 async fn get_community(
268 context: &LemmyContext,
269 request_counter: &mut i32,
270 ) -> Result<ApubCommunity, LemmyError> {
273 .dereference(context, local_instance(context), request_counter)
276 SiteOrCommunity::Community(c) => Ok(c),
277 SiteOrCommunity::Site(_) => Err(anyhow!("Calling get_community() on site activity").into()),