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,
16 use activitypub_federation::{
18 kinds::{activity::BlockType, public},
19 protocol::verification::verify_domains_match,
20 traits::{ActivityHandler, Actor},
23 use chrono::NaiveDateTime;
24 use lemmy_api_common::{
25 context::LemmyContext,
26 utils::{remove_user_data, remove_user_data_in_community},
28 use lemmy_db_schema::{
32 CommunityFollowerForm,
34 CommunityPersonBanForm,
36 moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
37 person::{Person, PersonUpdateForm},
39 traits::{Bannable, Crud, Followable},
41 use lemmy_utils::{error::LemmyError, utils::time::convert_datetime};
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: &Data<LemmyContext>,
53 ) -> Result<BlockUser, LemmyError> {
54 let audience = if let SiteOrCommunity::Community(c) = target {
60 actor: mod_.id().into(),
62 object: user.id().into(),
63 cc: generate_cc(target, &mut context.pool()).await?,
65 kind: BlockType::Block,
68 id: generate_activity_id(
70 &context.settings().get_protocol_and_hostname(),
73 expires: expires.map(convert_datetime),
77 #[tracing::instrument(skip_all)]
79 target: &SiteOrCommunity,
83 reason: Option<String>,
84 expires: Option<NaiveDateTime>,
85 context: &Data<LemmyContext>,
86 ) -> Result<(), LemmyError> {
87 let block = BlockUser::new(
99 SiteOrCommunity::Site(_) => {
100 let inboxes = remote_instance_inboxes(&mut context.pool()).await?;
101 send_lemmy_activity(context, block, mod_, inboxes, false).await
103 SiteOrCommunity::Community(c) => {
104 let activity = AnnouncableActivities::BlockUser(block);
105 let inboxes = vec![user.shared_inbox_or_inbox()];
106 send_activity_in_community(activity, mod_, c, inboxes, true, context).await
112 #[async_trait::async_trait]
113 impl ActivityHandler for BlockUser {
114 type DataType = LemmyContext;
115 type Error = LemmyError;
117 fn id(&self) -> &Url {
121 fn actor(&self) -> &Url {
125 #[tracing::instrument(skip_all)]
126 async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
127 verify_is_public(&self.to, &self.cc)?;
128 match self.target.dereference(context).await? {
129 SiteOrCommunity::Site(site) => {
130 let domain = self.object.inner().domain().expect("url needs domain");
131 if context.settings().hostname == domain {
133 anyhow!("Site bans from remote instance can't affect user's home instance").into(),
136 // site ban can only target a user who is on the same instance as the actor (admin)
137 verify_domains_match(&site.id(), self.actor.inner())?;
138 verify_domains_match(&site.id(), self.object.inner())?;
140 SiteOrCommunity::Community(community) => {
141 verify_person_in_community(&self.actor, &community, context).await?;
142 verify_mod_action(&self.actor, self.object.inner(), community.id, context).await?;
148 #[tracing::instrument(skip_all)]
149 async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
150 insert_activity(&self.id, &self, false, false, context).await?;
151 let expires = self.expires.map(|u| u.naive_local());
152 let mod_person = self.actor.dereference(context).await?;
153 let blocked_person = self.object.dereference(context).await?;
154 let target = self.target.dereference(context).await?;
156 SiteOrCommunity::Site(_site) => {
157 let blocked_person = Person::update(
160 &PersonUpdateForm::builder()
162 .ban_expires(Some(expires))
166 if self.remove_data.unwrap_or(false) {
177 let form = ModBanForm {
178 mod_person_id: mod_person.id,
179 other_person_id: blocked_person.id,
180 reason: self.summary,
184 ModBan::create(&mut context.pool(), &form).await?;
186 SiteOrCommunity::Community(community) => {
187 let community_user_ban_form = CommunityPersonBanForm {
188 community_id: community.id,
189 person_id: blocked_person.id,
190 expires: Some(expires),
192 CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form).await?;
194 // Also unsubscribe them from the community, if they are subscribed
195 let community_follower_form = CommunityFollowerForm {
196 community_id: community.id,
197 person_id: blocked_person.id,
200 CommunityFollower::unfollow(&mut context.pool(), &community_follower_form)
204 if self.remove_data.unwrap_or(false) {
205 remove_user_data_in_community(community.id, blocked_person.id, &mut context.pool())
210 let form = ModBanFromCommunityForm {
211 mod_person_id: mod_person.id,
212 other_person_id: blocked_person.id,
213 community_id: community.id,
214 reason: self.summary,
218 ModBanFromCommunity::create(&mut context.pool(), &form).await?;