3 block::{generate_cc, SiteOrCommunity},
4 community::{announce::GetCommunity, send_activity_in_community},
9 verify_person_in_community,
11 activity_lists::AnnouncableActivities,
13 objects::{community::ApubCommunity, instance::remote_instance_inboxes, person::ApubPerson},
14 protocol::activities::block::block_user::BlockUser,
17 use activitypub_federation::{
18 core::object_id::ObjectId,
20 traits::{ActivityHandler, Actor},
21 utils::verify_domains_match,
23 use activitystreams_kinds::{activity::BlockType, public};
25 use chrono::NaiveDateTime;
26 use lemmy_api_common::utils::{remove_user_data, remove_user_data_in_community};
27 use lemmy_db_schema::{
31 CommunityFollowerForm,
33 CommunityPersonBanForm,
35 moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
36 person::{Person, PersonUpdateForm},
38 traits::{Bannable, Crud, Followable},
40 use lemmy_utils::{error::LemmyError, utils::convert_datetime};
41 use lemmy_websocket::LemmyContext;
45 pub(in crate::activities::block) async fn new(
46 target: &SiteOrCommunity,
49 remove_data: Option<bool>,
50 reason: Option<String>,
51 expires: Option<NaiveDateTime>,
52 context: &LemmyContext,
53 ) -> Result<BlockUser, LemmyError> {
55 actor: ObjectId::new(mod_.actor_id()),
57 object: ObjectId::new(user.actor_id()),
58 cc: generate_cc(target, context.pool()).await?,
60 kind: BlockType::Block,
63 id: generate_activity_id(
65 &context.settings().get_protocol_and_hostname(),
67 expires: expires.map(convert_datetime),
71 #[tracing::instrument(skip_all)]
73 target: &SiteOrCommunity,
77 reason: Option<String>,
78 expires: Option<NaiveDateTime>,
79 context: &LemmyContext,
80 ) -> Result<(), LemmyError> {
81 let block = BlockUser::new(
93 SiteOrCommunity::Site(_) => {
94 let inboxes = remote_instance_inboxes(context.pool()).await?;
95 send_lemmy_activity(context, block, mod_, inboxes, false).await
97 SiteOrCommunity::Community(c) => {
98 let activity = AnnouncableActivities::BlockUser(block);
99 let inboxes = vec![user.shared_inbox_or_inbox()];
100 send_activity_in_community(activity, mod_, c, inboxes, context).await
106 #[async_trait::async_trait(?Send)]
107 impl ActivityHandler for BlockUser {
108 type DataType = LemmyContext;
109 type Error = LemmyError;
111 fn id(&self) -> &Url {
115 fn actor(&self) -> &Url {
119 #[tracing::instrument(skip_all)]
122 context: &Data<LemmyContext>,
123 request_counter: &mut i32,
124 ) -> Result<(), LemmyError> {
125 verify_is_public(&self.to, &self.cc)?;
128 .dereference(context, local_instance(context).await, request_counter)
131 SiteOrCommunity::Site(site) => {
132 let domain = self.object.inner().domain().expect("url needs domain");
133 if context.settings().hostname == domain {
135 anyhow!("Site bans from remote instance can't affect user's home instance").into(),
138 // site ban can only target a user who is on the same instance as the actor (admin)
139 verify_domains_match(&site.actor_id(), self.actor.inner())?;
140 verify_domains_match(&site.actor_id(), self.object.inner())?;
142 SiteOrCommunity::Community(community) => {
143 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
157 #[tracing::instrument(skip_all)]
160 context: &Data<LemmyContext>,
161 request_counter: &mut i32,
162 ) -> Result<(), LemmyError> {
163 let expires = self.expires.map(|u| u.naive_local());
164 let mod_person = self
166 .dereference(context, local_instance(context).await, request_counter)
168 let blocked_person = self
170 .dereference(context, local_instance(context).await, request_counter)
174 .dereference(context, local_instance(context).await, request_counter)
177 SiteOrCommunity::Site(_site) => {
178 let blocked_person = Person::update(
181 &PersonUpdateForm::builder()
183 .ban_expires(Some(expires))
187 if self.remove_data.unwrap_or(false) {
198 let form = ModBanForm {
199 mod_person_id: mod_person.id,
200 other_person_id: blocked_person.id,
201 reason: self.summary,
205 ModBan::create(context.pool(), &form).await?;
207 SiteOrCommunity::Community(community) => {
208 let community_user_ban_form = CommunityPersonBanForm {
209 community_id: community.id,
210 person_id: blocked_person.id,
211 expires: Some(expires),
213 CommunityPersonBan::ban(context.pool(), &community_user_ban_form).await?;
215 // Also unsubscribe them from the community, if they are subscribed
216 let community_follower_form = CommunityFollowerForm {
217 community_id: community.id,
218 person_id: blocked_person.id,
221 CommunityFollower::unfollow(context.pool(), &community_follower_form)
225 if self.remove_data.unwrap_or(false) {
226 remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
230 let form = ModBanFromCommunityForm {
231 mod_person_id: mod_person.id,
232 other_person_id: blocked_person.id,
233 community_id: community.id,
234 reason: self.summary,
238 ModBanFromCommunity::create(context.pool(), &form).await?;
246 #[async_trait::async_trait(?Send)]
247 impl GetCommunity for BlockUser {
248 #[tracing::instrument(skip_all)]
249 async fn get_community(
251 context: &LemmyContext,
252 request_counter: &mut i32,
253 ) -> Result<ApubCommunity, LemmyError> {
256 .dereference(context, local_instance(context).await, request_counter)
259 SiteOrCommunity::Community(c) => Ok(c),
260 SiteOrCommunity::Site(_) => Err(anyhow!("Calling get_community() on site activity").into()),