3 community::{announce::GetCommunity, send_to_community},
6 verify_delete_activity,
13 activity_lists::AnnouncableActivities,
14 fetcher::object_id::ObjectId,
15 objects::{community::ApubCommunity, person::ApubPerson},
16 protocol::activities::deletion::delete::Delete,
18 use activitystreams::{activity::kind::DeleteType, public};
20 use lemmy_api_common::blocking;
23 traits::{ActivityHandler, ActorType},
25 use lemmy_db_schema::{
33 ModRemoveCommunityForm,
41 use lemmy_utils::LemmyError;
42 use lemmy_websocket::{
43 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
49 #[async_trait::async_trait(?Send)]
50 impl ActivityHandler for Delete {
51 type DataType = LemmyContext;
54 context: &Data<LemmyContext>,
55 request_counter: &mut i32,
56 ) -> Result<(), LemmyError> {
57 verify_is_public(&self.to)?;
58 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
59 let community = self.get_community(context, request_counter).await?;
60 verify_delete_activity(
64 self.summary.is_some(),
74 context: &Data<LemmyContext>,
75 request_counter: &mut i32,
76 ) -> Result<(), LemmyError> {
77 if let Some(reason) = self.summary {
78 // We set reason to empty string if it doesn't exist, to distinguish between delete and
79 // remove. Here we change it back to option, so we don't write it to db.
80 let reason = if reason.is_empty() {
85 receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
87 receive_delete_action(&self.object, &self.actor, true, context, request_counter).await
93 pub(in crate::activities::deletion) fn new(
95 community: &ApubCommunity,
97 summary: Option<String>,
98 context: &LemmyContext,
99 ) -> Result<Delete, LemmyError> {
101 actor: ObjectId::new(actor.actor_id()),
104 cc: vec![community.actor_id()],
105 kind: DeleteType::Delete,
107 id: generate_activity_id(
109 &context.settings().get_protocol_and_hostname(),
111 unparsed: Default::default(),
114 pub(in crate::activities::deletion) async fn send(
116 community: &ApubCommunity,
118 summary: Option<String>,
119 context: &LemmyContext,
120 ) -> Result<(), LemmyError> {
121 let delete = Delete::new(actor, community, object_id, summary, context)?;
122 let delete_id = delete.id.clone();
124 let activity = AnnouncableActivities::Delete(delete);
125 send_to_community(activity, &delete_id, actor, community, vec![], context).await
129 pub(in crate::activities) async fn receive_remove_action(
130 actor: &ObjectId<ApubPerson>,
132 reason: Option<String>,
133 context: &LemmyContext,
134 request_counter: &mut i32,
135 ) -> Result<(), LemmyError> {
136 let actor = actor.dereference(context, request_counter).await?;
137 use UserOperationCrud::*;
138 match DeletableObjects::read_from_db(object, context).await? {
139 DeletableObjects::Community(community) => {
141 return Err(anyhow!("Only local admin can remove community").into());
143 let form = ModRemoveCommunityForm {
144 mod_person_id: actor.id,
145 community_id: community.id,
150 blocking(context.pool(), move |conn| {
151 ModRemoveCommunity::create(conn, &form)
154 let deleted_community = blocking(context.pool(), move |conn| {
155 Community::update_removed(conn, community.id, true)
159 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
161 DeletableObjects::Post(post) => {
162 let form = ModRemovePostForm {
163 mod_person_id: actor.id,
168 blocking(context.pool(), move |conn| {
169 ModRemovePost::create(conn, &form)
172 let removed_post = blocking(context.pool(), move |conn| {
173 Post::update_removed(conn, post.id, true)
177 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
179 DeletableObjects::Comment(comment) => {
180 let form = ModRemoveCommentForm {
181 mod_person_id: actor.id,
182 comment_id: comment.id,
186 blocking(context.pool(), move |conn| {
187 ModRemoveComment::create(conn, &form)
190 let removed_comment = blocking(context.pool(), move |conn| {
191 Comment::update_removed(conn, comment.id, true)
195 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
201 #[async_trait::async_trait(?Send)]
202 impl GetCommunity for Delete {
203 async fn get_community(
205 context: &LemmyContext,
206 _request_counter: &mut i32,
207 ) -> Result<ApubCommunity, LemmyError> {
208 let community_id = match DeletableObjects::read_from_db(&self.object, context).await? {
209 DeletableObjects::Community(c) => c.id,
210 DeletableObjects::Comment(c) => {
211 let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;
214 DeletableObjects::Post(p) => p.community_id,
216 let community = blocking(context.pool(), move |conn| {
217 Community::read(conn, community_id)