3 community::announce::GetCommunity,
4 deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
10 objects::{community::ApubCommunity, person::ApubPerson},
11 protocol::{activities::deletion::delete::Delete, IdOrNestedObject},
13 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
14 use activitystreams_kinds::activity::DeleteType;
16 use lemmy_api_common::utils::blocking;
17 use lemmy_db_schema::{
19 comment::{Comment, CommentUpdateForm},
20 community::{Community, CommunityUpdateForm},
25 ModRemoveCommunityForm,
29 post::{Post, PostUpdateForm},
33 use lemmy_utils::error::LemmyError;
34 use lemmy_websocket::{
35 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
41 #[async_trait::async_trait(?Send)]
42 impl ActivityHandler for Delete {
43 type DataType = LemmyContext;
44 type Error = LemmyError;
46 fn id(&self) -> &Url {
50 fn actor(&self) -> &Url {
54 #[tracing::instrument(skip_all)]
57 context: &Data<LemmyContext>,
58 request_counter: &mut i32,
59 ) -> Result<(), LemmyError> {
60 let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
61 check_apub_id_valid(self.id(), &local_site_data, context.settings())
62 .map_err(LemmyError::from_message)?;
63 verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?;
67 #[tracing::instrument(skip_all)]
70 context: &Data<LemmyContext>,
71 request_counter: &mut i32,
72 ) -> Result<(), LemmyError> {
73 if let Some(reason) = self.summary {
74 // We set reason to empty string if it doesn't exist, to distinguish between delete and
75 // remove. Here we change it back to option, so we don't write it to db.
76 let reason = if reason.is_empty() {
81 receive_remove_action(
84 .dereference(context, local_instance(context), request_counter)
92 receive_delete_action(
105 pub(in crate::activities::deletion) fn new(
107 object: DeletableObjects,
109 community: Option<&Community>,
110 summary: Option<String>,
111 context: &LemmyContext,
112 ) -> Result<Delete, LemmyError> {
113 let id = generate_activity_id(
115 &context.settings().get_protocol_and_hostname(),
117 let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
119 actor: ObjectId::new(actor.actor_id.clone()),
121 object: IdOrNestedObject::Id(object.id()),
122 cc: cc.into_iter().collect(),
123 kind: DeleteType::Delete,
126 unparsed: Default::default(),
131 #[tracing::instrument(skip_all)]
132 pub(in crate::activities) async fn receive_remove_action(
135 reason: Option<String>,
136 context: &LemmyContext,
137 ) -> Result<(), LemmyError> {
138 use UserOperationCrud::*;
139 match DeletableObjects::read_from_db(object, context).await? {
140 DeletableObjects::Community(community) => {
142 return Err(LemmyError::from_message(
143 "Only local admin can remove community",
146 let form = ModRemoveCommunityForm {
147 mod_person_id: actor.id,
148 community_id: community.id,
153 blocking(context.pool(), move |conn| {
154 ModRemoveCommunity::create(conn, &form)
157 let deleted_community = blocking(context.pool(), move |conn| {
161 &CommunityUpdateForm::builder().removed(Some(true)).build(),
166 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
168 DeletableObjects::Post(post) => {
169 let form = ModRemovePostForm {
170 mod_person_id: actor.id,
175 blocking(context.pool(), move |conn| {
176 ModRemovePost::create(conn, &form)
179 let removed_post = blocking(context.pool(), move |conn| {
183 &PostUpdateForm::builder().removed(Some(true)).build(),
188 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
190 DeletableObjects::Comment(comment) => {
191 let form = ModRemoveCommentForm {
192 mod_person_id: actor.id,
193 comment_id: comment.id,
197 blocking(context.pool(), move |conn| {
198 ModRemoveComment::create(conn, &form)
201 let removed_comment = blocking(context.pool(), move |conn| {
205 &CommentUpdateForm::builder().removed(Some(true)).build(),
210 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
212 DeletableObjects::PrivateMessage(_) => unimplemented!(),
217 #[async_trait::async_trait(?Send)]
218 impl GetCommunity for Delete {
219 #[tracing::instrument(skip_all)]
220 async fn get_community(
222 context: &LemmyContext,
223 _request_counter: &mut i32,
224 ) -> Result<ApubCommunity, LemmyError> {
225 let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? {
226 DeletableObjects::Community(c) => c.id,
227 DeletableObjects::Comment(c) => {
228 let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;
231 DeletableObjects::Post(p) => p.community_id,
232 DeletableObjects::PrivateMessage(_) => {
233 return Err(anyhow!("Private message is not part of community").into())
236 let community = blocking(context.pool(), move |conn| {
237 Community::read(conn, community_id)