3 deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
7 objects::{community::ApubCommunity, person::ApubPerson},
8 protocol::{activities::deletion::delete::Delete, IdOrNestedObject},
10 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
11 use activitystreams_kinds::activity::DeleteType;
12 use lemmy_api_common::{
14 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
19 use lemmy_db_schema::{
21 comment::{Comment, CommentUpdateForm},
22 community::{Community, CommunityUpdateForm},
27 ModRemoveCommunityForm,
31 post::{Post, PostUpdateForm},
35 use lemmy_utils::error::LemmyError;
38 #[async_trait::async_trait(?Send)]
39 impl ActivityHandler for Delete {
40 type DataType = LemmyContext;
41 type Error = LemmyError;
43 fn id(&self) -> &Url {
47 fn actor(&self) -> &Url {
51 #[tracing::instrument(skip_all)]
54 context: &Data<LemmyContext>,
55 request_counter: &mut i32,
56 ) -> Result<(), LemmyError> {
57 verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?;
61 #[tracing::instrument(skip_all)]
64 context: &Data<LemmyContext>,
65 request_counter: &mut i32,
66 ) -> Result<(), LemmyError> {
67 if let Some(reason) = self.summary {
68 // We set reason to empty string if it doesn't exist, to distinguish between delete and
69 // remove. Here we change it back to option, so we don't write it to db.
70 let reason = if reason.is_empty() {
75 receive_remove_action(
78 .dereference(context, local_instance(context).await, request_counter)
86 receive_delete_action(
99 pub(in crate::activities::deletion) fn new(
101 object: DeletableObjects,
103 community: Option<&Community>,
104 summary: Option<String>,
105 context: &LemmyContext,
106 ) -> Result<Delete, LemmyError> {
107 let id = generate_activity_id(
109 &context.settings().get_protocol_and_hostname(),
111 let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
113 actor: ObjectId::new(actor.actor_id.clone()),
115 object: IdOrNestedObject::Id(object.id()),
116 cc: cc.into_iter().collect(),
117 kind: DeleteType::Delete,
120 audience: community.map(|c| ObjectId::<ApubCommunity>::new(c.actor_id.clone())),
125 #[tracing::instrument(skip_all)]
126 pub(in crate::activities) async fn receive_remove_action(
129 reason: Option<String>,
130 context: &LemmyContext,
131 ) -> Result<(), LemmyError> {
132 use UserOperationCrud::*;
133 match DeletableObjects::read_from_db(object, context).await? {
134 DeletableObjects::Community(community) => {
136 return Err(LemmyError::from_message(
137 "Only local admin can remove community",
140 let form = ModRemoveCommunityForm {
141 mod_person_id: actor.id,
142 community_id: community.id,
147 ModRemoveCommunity::create(context.pool(), &form).await?;
148 let deleted_community = Community::update(
151 &CommunityUpdateForm::builder().removed(Some(true)).build(),
155 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
157 DeletableObjects::Post(post) => {
158 let form = ModRemovePostForm {
159 mod_person_id: actor.id,
164 ModRemovePost::create(context.pool(), &form).await?;
165 let removed_post = Post::update(
168 &PostUpdateForm::builder().removed(Some(true)).build(),
172 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
174 DeletableObjects::Comment(comment) => {
175 let form = ModRemoveCommentForm {
176 mod_person_id: actor.id,
177 comment_id: comment.id,
181 ModRemoveComment::create(context.pool(), &form).await?;
182 let removed_comment = Comment::update(
185 &CommentUpdateForm::builder().removed(Some(true)).build(),
189 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
191 DeletableObjects::PrivateMessage(_) => unimplemented!(),