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},
36 deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
41 activity_lists::AnnouncableActivities,
42 fetcher::object_id::ObjectId,
43 objects::{community::ApubCommunity, person::ApubPerson},
44 protocol::activities::deletion::delete::Delete,
47 #[async_trait::async_trait(?Send)]
48 impl ActivityHandler for Delete {
49 type DataType = LemmyContext;
52 context: &Data<LemmyContext>,
53 request_counter: &mut i32,
54 ) -> Result<(), LemmyError> {
55 verify_is_public(&self.to)?;
56 verify_activity(self, &context.settings())?;
57 let community = self.get_community(context, request_counter).await?;
58 verify_delete_activity(
62 self.summary.is_some(),
72 context: &Data<LemmyContext>,
73 request_counter: &mut i32,
74 ) -> Result<(), LemmyError> {
75 if let Some(reason) = self.summary {
76 // We set reason to empty string if it doesn't exist, to distinguish between delete and
77 // remove. Here we change it back to option, so we don't write it to db.
78 let reason = if reason.is_empty() {
83 receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
85 receive_delete_action(&self.object, &self.actor, true, context, request_counter).await
91 pub(in crate::activities::deletion) fn new(
93 community: &ApubCommunity,
95 summary: Option<String>,
96 context: &LemmyContext,
97 ) -> Result<Delete, LemmyError> {
99 actor: ObjectId::new(actor.actor_id()),
102 cc: vec![community.actor_id()],
103 kind: DeleteType::Delete,
105 id: generate_activity_id(
107 &context.settings().get_protocol_and_hostname(),
109 unparsed: Default::default(),
112 pub(in crate::activities::deletion) async fn send(
114 community: &ApubCommunity,
116 summary: Option<String>,
117 context: &LemmyContext,
118 ) -> Result<(), LemmyError> {
119 let delete = Delete::new(actor, community, object_id, summary, context)?;
120 let delete_id = delete.id.clone();
122 let activity = AnnouncableActivities::Delete(delete);
123 send_to_community(activity, &delete_id, actor, community, vec![], context).await
127 pub(in crate::activities) async fn receive_remove_action(
128 actor: &ObjectId<ApubPerson>,
130 reason: Option<String>,
131 context: &LemmyContext,
132 request_counter: &mut i32,
133 ) -> Result<(), LemmyError> {
134 let actor = actor.dereference(context, request_counter).await?;
135 use UserOperationCrud::*;
136 match DeletableObjects::read_from_db(object, context).await? {
137 DeletableObjects::Community(community) => {
139 return Err(anyhow!("Only local admin can remove community").into());
141 let form = ModRemoveCommunityForm {
142 mod_person_id: actor.id,
143 community_id: community.id,
148 blocking(context.pool(), move |conn| {
149 ModRemoveCommunity::create(conn, &form)
152 let deleted_community = blocking(context.pool(), move |conn| {
153 Community::update_removed(conn, community.id, true)
157 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
159 DeletableObjects::Post(post) => {
160 let form = ModRemovePostForm {
161 mod_person_id: actor.id,
166 blocking(context.pool(), move |conn| {
167 ModRemovePost::create(conn, &form)
170 let removed_post = blocking(context.pool(), move |conn| {
171 Post::update_removed(conn, post.id, true)
175 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
177 DeletableObjects::Comment(comment) => {
178 let form = ModRemoveCommentForm {
179 mod_person_id: actor.id,
180 comment_id: comment.id,
184 blocking(context.pool(), move |conn| {
185 ModRemoveComment::create(conn, &form)
188 let removed_comment = blocking(context.pool(), move |conn| {
189 Comment::update_removed(conn, comment.id, true)
193 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
199 #[async_trait::async_trait(?Send)]
200 impl GetCommunity for Delete {
201 async fn get_community(
203 context: &LemmyContext,
204 _request_counter: &mut i32,
205 ) -> Result<ApubCommunity, LemmyError> {
206 let community_id = match DeletableObjects::read_from_db(&self.object, context).await? {
207 DeletableObjects::Community(c) => c.id,
208 DeletableObjects::Comment(c) => {
209 let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;
212 DeletableObjects::Post(p) => p.community_id,
214 let community = blocking(context.pool(), move |conn| {
215 Community::read(conn, community_id)