3 block::{generate_cc, SiteOrCommunity},
4 community::send_activity_in_community,
9 verify_person_in_community,
11 activity_lists::AnnouncableActivities,
13 objects::{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::{
27 context::LemmyContext,
28 utils::{remove_user_data, remove_user_data_in_community},
30 use lemmy_db_schema::{
34 CommunityFollowerForm,
36 CommunityPersonBanForm,
38 moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
39 person::{Person, PersonUpdateForm},
41 traits::{Bannable, Crud, Followable},
43 use lemmy_utils::{error::LemmyError, utils::convert_datetime};
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> {
56 let audience = if let SiteOrCommunity::Community(c) = target {
57 Some(ObjectId::new(c.actor_id()))
62 actor: ObjectId::new(mod_.actor_id()),
64 object: ObjectId::new(user.actor_id()),
65 cc: generate_cc(target, context.pool()).await?,
67 kind: BlockType::Block,
70 id: generate_activity_id(
72 &context.settings().get_protocol_and_hostname(),
75 expires: expires.map(convert_datetime),
79 #[tracing::instrument(skip_all)]
81 target: &SiteOrCommunity,
85 reason: Option<String>,
86 expires: Option<NaiveDateTime>,
87 context: &LemmyContext,
88 ) -> Result<(), LemmyError> {
89 let block = BlockUser::new(
101 SiteOrCommunity::Site(_) => {
102 let inboxes = remote_instance_inboxes(context.pool()).await?;
103 send_lemmy_activity(context, block, mod_, inboxes, false).await
105 SiteOrCommunity::Community(c) => {
106 let activity = AnnouncableActivities::BlockUser(block);
107 let inboxes = vec![user.shared_inbox_or_inbox()];
108 send_activity_in_community(activity, mod_, c, inboxes, true, context).await
114 #[async_trait::async_trait(?Send)]
115 impl ActivityHandler for BlockUser {
116 type DataType = LemmyContext;
117 type Error = LemmyError;
119 fn id(&self) -> &Url {
123 fn actor(&self) -> &Url {
127 #[tracing::instrument(skip_all)]
130 context: &Data<LemmyContext>,
131 request_counter: &mut i32,
132 ) -> Result<(), LemmyError> {
133 verify_is_public(&self.to, &self.cc)?;
136 .dereference(context, local_instance(context).await, request_counter)
139 SiteOrCommunity::Site(site) => {
140 let domain = self.object.inner().domain().expect("url needs domain");
141 if context.settings().hostname == domain {
143 anyhow!("Site bans from remote instance can't affect user's home instance").into(),
146 // site ban can only target a user who is on the same instance as the actor (admin)
147 verify_domains_match(&site.actor_id(), self.actor.inner())?;
148 verify_domains_match(&site.actor_id(), self.object.inner())?;
150 SiteOrCommunity::Community(community) => {
151 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
165 #[tracing::instrument(skip_all)]
168 context: &Data<LemmyContext>,
169 request_counter: &mut i32,
170 ) -> Result<(), LemmyError> {
171 let expires = self.expires.map(|u| u.naive_local());
172 let mod_person = self
174 .dereference(context, local_instance(context).await, request_counter)
176 let blocked_person = self
178 .dereference(context, local_instance(context).await, request_counter)
182 .dereference(context, local_instance(context).await, request_counter)
185 SiteOrCommunity::Site(_site) => {
186 let blocked_person = Person::update(
189 &PersonUpdateForm::builder()
191 .ban_expires(Some(expires))
195 if self.remove_data.unwrap_or(false) {
206 let form = ModBanForm {
207 mod_person_id: mod_person.id,
208 other_person_id: blocked_person.id,
209 reason: self.summary,
213 ModBan::create(context.pool(), &form).await?;
215 SiteOrCommunity::Community(community) => {
216 let community_user_ban_form = CommunityPersonBanForm {
217 community_id: community.id,
218 person_id: blocked_person.id,
219 expires: Some(expires),
221 CommunityPersonBan::ban(context.pool(), &community_user_ban_form).await?;
223 // Also unsubscribe them from the community, if they are subscribed
224 let community_follower_form = CommunityFollowerForm {
225 community_id: community.id,
226 person_id: blocked_person.id,
229 CommunityFollower::unfollow(context.pool(), &community_follower_form)
233 if self.remove_data.unwrap_or(false) {
234 remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
238 let form = ModBanFromCommunityForm {
239 mod_person_id: mod_person.id,
240 other_person_id: blocked_person.id,
241 community_id: community.id,
242 reason: self.summary,
246 ModBanFromCommunity::create(context.pool(), &form).await?;