1 use activitystreams::{activity::kind::DeleteType, public};
5 use lemmy_api_common::blocking;
8 traits::{ActivityHandler, ActorType},
10 use lemmy_db_schema::{
18 ModRemoveCommunityForm,
26 use lemmy_utils::LemmyError;
27 use lemmy_websocket::{
28 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
35 community::{announce::GetCommunity, send_to_community},
37 receive_delete_action,
38 verify_delete_activity,
46 activity_lists::AnnouncableActivities,
47 fetcher::object_id::ObjectId,
48 objects::{community::ApubCommunity, person::ApubPerson},
49 protocol::activities::deletion::delete::Delete,
52 #[async_trait::async_trait(?Send)]
53 impl ActivityHandler for Delete {
54 type DataType = LemmyContext;
57 context: &Data<LemmyContext>,
58 request_counter: &mut i32,
59 ) -> Result<(), LemmyError> {
60 verify_is_public(&self.to)?;
61 verify_activity(self, &context.settings())?;
62 let community = self.get_community(context, request_counter).await?;
63 verify_delete_activity(
67 self.summary.is_some(),
77 context: &Data<LemmyContext>,
78 request_counter: &mut i32,
79 ) -> Result<(), LemmyError> {
80 if let Some(reason) = self.summary {
81 // We set reason to empty string if it doesn't exist, to distinguish between delete and
82 // remove. Here we change it back to option, so we don't write it to db.
83 let reason = if reason.is_empty() {
88 receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
90 receive_delete_action(
94 community: UserOperationCrud::DeleteCommunity,
95 post: UserOperationCrud::DeletePost,
96 comment: UserOperationCrud::DeleteComment,
108 pub(in crate::activities::deletion) fn new(
110 community: &ApubCommunity,
112 summary: Option<String>,
113 context: &LemmyContext,
114 ) -> Result<Delete, LemmyError> {
116 actor: ObjectId::new(actor.actor_id()),
119 cc: vec![community.actor_id()],
120 kind: DeleteType::Delete,
122 id: generate_activity_id(
124 &context.settings().get_protocol_and_hostname(),
126 unparsed: Default::default(),
129 pub(in crate::activities::deletion) async fn send(
131 community: &ApubCommunity,
133 summary: Option<String>,
134 context: &LemmyContext,
135 ) -> Result<(), LemmyError> {
136 let delete = Delete::new(actor, community, object_id, summary, context)?;
137 let delete_id = delete.id.clone();
139 let activity = AnnouncableActivities::Delete(delete);
140 send_to_community(activity, &delete_id, actor, community, vec![], context).await
144 pub(in crate::activities) async fn receive_remove_action(
145 actor: &ObjectId<ApubPerson>,
147 reason: Option<String>,
148 context: &LemmyContext,
149 request_counter: &mut i32,
150 ) -> Result<(), LemmyError> {
151 let actor = actor.dereference(context, request_counter).await?;
152 use UserOperationCrud::*;
153 match DeletableObjects::read_from_db(object, context).await? {
154 DeletableObjects::Community(community) => {
156 return Err(anyhow!("Only local admin can remove community").into());
158 let form = ModRemoveCommunityForm {
159 mod_person_id: actor.id,
160 community_id: community.id,
165 blocking(context.pool(), move |conn| {
166 ModRemoveCommunity::create(conn, &form)
169 let deleted_community = blocking(context.pool(), move |conn| {
170 Community::update_removed(conn, community.id, true)
174 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
176 DeletableObjects::Post(post) => {
177 let form = ModRemovePostForm {
178 mod_person_id: actor.id,
183 blocking(context.pool(), move |conn| {
184 ModRemovePost::create(conn, &form)
187 let removed_post = blocking(context.pool(), move |conn| {
188 Post::update_removed(conn, post.id, true)
192 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
194 DeletableObjects::Comment(comment) => {
195 let form = ModRemoveCommentForm {
196 mod_person_id: actor.id,
197 comment_id: comment.id,
201 blocking(context.pool(), move |conn| {
202 ModRemoveComment::create(conn, &form)
205 let removed_comment = blocking(context.pool(), move |conn| {
206 Comment::update_removed(conn, comment.id, true)
210 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
216 #[async_trait::async_trait(?Send)]
217 impl GetCommunity for Delete {
218 async fn get_community(
220 context: &LemmyContext,
221 _request_counter: &mut i32,
222 ) -> Result<ApubCommunity, LemmyError> {
223 let community_id = match DeletableObjects::read_from_db(&self.object, context).await? {
224 DeletableObjects::Community(c) => c.id,
225 DeletableObjects::Comment(c) => {
226 let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;
229 DeletableObjects::Post(p) => p.community_id,
231 let community = blocking(context.pool(), move |conn| {
232 Community::read(conn, community_id)