3 block::{generate_cc, generate_instance_inboxes, SiteOrCommunity},
4 community::{announce::GetCommunity, send_activity_in_community},
10 verify_person_in_community,
12 activity_lists::AnnouncableActivities,
13 objects::{community::ApubCommunity, person::ApubPerson},
14 protocol::activities::block::block_user::BlockUser,
16 use activitystreams_kinds::{activity::BlockType, public};
18 use chrono::NaiveDateTime;
19 use lemmy_api_common::utils::{blocking, remove_user_data, remove_user_data_in_community};
23 traits::{ActivityHandler, ActorType},
24 verify::verify_domains_match,
26 use lemmy_db_schema::{
30 CommunityFollowerForm,
32 CommunityPersonBanForm,
34 moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
37 traits::{Bannable, Crud, Followable},
39 use lemmy_utils::{settings::structs::Settings, utils::convert_datetime, LemmyError};
40 use lemmy_websocket::LemmyContext;
43 pub(in crate::activities::block) async fn new(
44 target: &SiteOrCommunity,
47 remove_data: Option<bool>,
48 reason: Option<String>,
49 expires: Option<NaiveDateTime>,
50 context: &LemmyContext,
51 ) -> Result<BlockUser, LemmyError> {
53 actor: ObjectId::new(mod_.actor_id()),
55 object: ObjectId::new(user.actor_id()),
56 cc: generate_cc(target, context.pool()).await?,
58 kind: BlockType::Block,
61 id: generate_activity_id(
63 &context.settings().get_protocol_and_hostname(),
65 expires: expires.map(convert_datetime),
66 unparsed: Default::default(),
70 #[tracing::instrument(skip_all)]
72 target: &SiteOrCommunity,
76 reason: Option<String>,
77 expires: Option<NaiveDateTime>,
78 context: &LemmyContext,
79 ) -> Result<(), LemmyError> {
80 let block = BlockUser::new(
90 let block_id = block.id.clone();
93 SiteOrCommunity::Site(_) => {
94 let inboxes = generate_instance_inboxes(user, context.pool()).await?;
95 send_lemmy_activity(context, &block, &block_id, mod_, inboxes, false).await
97 SiteOrCommunity::Community(c) => {
98 let activity = AnnouncableActivities::BlockUser(block);
99 let inboxes = vec![user.shared_inbox_or_inbox_url()];
100 send_activity_in_community(activity, &block_id, mod_, c, inboxes, context).await
106 #[async_trait::async_trait(?Send)]
107 impl ActivityHandler for BlockUser {
108 type DataType = LemmyContext;
110 #[tracing::instrument(skip_all)]
113 context: &Data<LemmyContext>,
114 request_counter: &mut i32,
115 ) -> Result<(), LemmyError> {
116 verify_is_public(&self.to, &self.cc)?;
117 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
120 .dereference(context, context.client(), request_counter)
123 SiteOrCommunity::Site(site) => {
124 let domain = self.object.inner().domain().expect("url needs domain");
125 if Settings::get().hostname == domain {
127 anyhow!("Site bans from remote instance can't affect user's home instance").into(),
130 // site ban can only target a user who is on the same instance as the actor (admin)
131 verify_domains_match(&site.actor_id(), self.actor.inner())?;
132 verify_domains_match(&site.actor_id(), self.object.inner())?;
134 SiteOrCommunity::Community(community) => {
135 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
149 #[tracing::instrument(skip_all)]
152 context: &Data<LemmyContext>,
153 request_counter: &mut i32,
154 ) -> Result<(), LemmyError> {
155 let expires = self.expires.map(|u| u.naive_local());
156 let mod_person = self
158 .dereference(context, context.client(), request_counter)
160 let blocked_person = self
162 .dereference(context, context.client(), request_counter)
166 .dereference(context, context.client(), request_counter)
169 SiteOrCommunity::Site(_site) => {
170 let blocked_person = blocking(context.pool(), move |conn| {
171 Person::ban_person(conn, blocked_person.id, true, expires)
174 if self.remove_data.unwrap_or(false) {
175 remove_user_data(blocked_person.id, context.pool()).await?;
179 let form = ModBanForm {
180 mod_person_id: mod_person.id,
181 other_person_id: blocked_person.id,
182 reason: self.summary,
186 blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
188 SiteOrCommunity::Community(community) => {
189 let community_user_ban_form = CommunityPersonBanForm {
190 community_id: community.id,
191 person_id: blocked_person.id,
192 expires: Some(expires),
194 blocking(context.pool(), move |conn| {
195 CommunityPersonBan::ban(conn, &community_user_ban_form)
199 // Also unsubscribe them from the community, if they are subscribed
200 let community_follower_form = CommunityFollowerForm {
201 community_id: community.id,
202 person_id: blocked_person.id,
205 blocking(context.pool(), move |conn: &'_ _| {
206 CommunityFollower::unfollow(conn, &community_follower_form)
211 if self.remove_data.unwrap_or(false) {
212 remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
216 let form = ModBanFromCommunityForm {
217 mod_person_id: mod_person.id,
218 other_person_id: blocked_person.id,
219 community_id: community.id,
220 reason: self.summary,
224 blocking(context.pool(), move |conn| {
225 ModBanFromCommunity::create(conn, &form)
235 #[async_trait::async_trait(?Send)]
236 impl GetCommunity for BlockUser {
237 #[tracing::instrument(skip_all)]
238 async fn get_community(
240 context: &LemmyContext,
241 request_counter: &mut i32,
242 ) -> Result<ApubCommunity, LemmyError> {
245 .dereference(context, context.client(), request_counter)
248 SiteOrCommunity::Community(c) => Ok(c),
249 SiteOrCommunity::Site(_) => Err(anyhow!("Calling get_community() on site activity").into()),