3 community::{announce::GetCommunity, send_activity_in_community},
4 deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
9 activity_lists::AnnouncableActivities,
10 objects::{community::ApubCommunity, person::ApubPerson},
11 protocol::activities::deletion::delete::Delete,
13 use activitystreams::{activity::kind::DeleteType, public};
15 use lemmy_api_common::blocking;
19 traits::{ActivityHandler, ActorType},
21 use lemmy_db_schema::{
29 ModRemoveCommunityForm,
37 use lemmy_utils::LemmyError;
38 use lemmy_websocket::{
39 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
45 #[async_trait::async_trait(?Send)]
46 impl ActivityHandler for Delete {
47 type DataType = LemmyContext;
50 context: &Data<LemmyContext>,
51 request_counter: &mut i32,
52 ) -> Result<(), LemmyError> {
53 verify_is_public(&self.to, &self.cc)?;
54 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
55 let community = self.get_community(context, request_counter).await?;
56 verify_delete_activity(
60 self.summary.is_some(),
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(&self.actor, &self.object, reason, context, request_counter).await
83 receive_delete_action(&self.object, &self.actor, true, context, request_counter).await
89 pub(in crate::activities::deletion) fn new(
91 community: &ApubCommunity,
93 summary: Option<String>,
94 context: &LemmyContext,
95 ) -> Result<Delete, LemmyError> {
97 actor: ObjectId::new(actor.actor_id()),
100 cc: vec![community.actor_id()],
101 kind: DeleteType::Delete,
103 id: generate_activity_id(
105 &context.settings().get_protocol_and_hostname(),
107 unparsed: Default::default(),
110 pub(in crate::activities::deletion) async fn send(
112 community: &ApubCommunity,
114 summary: Option<String>,
115 context: &LemmyContext,
116 ) -> Result<(), LemmyError> {
117 let delete = Delete::new(actor, community, object_id, summary, context)?;
118 let delete_id = delete.id.clone();
120 let activity = AnnouncableActivities::Delete(delete);
121 send_activity_in_community(activity, &delete_id, actor, community, vec![], context).await
125 pub(in crate::activities) async fn receive_remove_action(
126 actor: &ObjectId<ApubPerson>,
128 reason: Option<String>,
129 context: &LemmyContext,
130 request_counter: &mut i32,
131 ) -> Result<(), LemmyError> {
132 let actor = actor.dereference(context, request_counter).await?;
133 use UserOperationCrud::*;
134 match DeletableObjects::read_from_db(object, context).await? {
135 DeletableObjects::Community(community) => {
137 return Err(anyhow!("Only local admin can remove community").into());
139 let form = ModRemoveCommunityForm {
140 mod_person_id: actor.id,
141 community_id: community.id,
146 blocking(context.pool(), move |conn| {
147 ModRemoveCommunity::create(conn, &form)
150 let deleted_community = blocking(context.pool(), move |conn| {
151 Community::update_removed(conn, community.id, true)
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 blocking(context.pool(), move |conn| {
165 ModRemovePost::create(conn, &form)
168 let removed_post = blocking(context.pool(), move |conn| {
169 Post::update_removed(conn, post.id, true)
173 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
175 DeletableObjects::Comment(comment) => {
176 let form = ModRemoveCommentForm {
177 mod_person_id: actor.id,
178 comment_id: comment.id,
182 blocking(context.pool(), move |conn| {
183 ModRemoveComment::create(conn, &form)
186 let removed_comment = blocking(context.pool(), move |conn| {
187 Comment::update_removed(conn, comment.id, true)
191 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
197 #[async_trait::async_trait(?Send)]
198 impl GetCommunity for Delete {
199 async fn get_community(
201 context: &LemmyContext,
202 _request_counter: &mut i32,
203 ) -> Result<ApubCommunity, LemmyError> {
204 let community_id = match DeletableObjects::read_from_db(&self.object, context).await? {
205 DeletableObjects::Community(c) => c.id,
206 DeletableObjects::Comment(c) => {
207 let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;
210 DeletableObjects::Post(p) => p.community_id,
212 let community = blocking(context.pool(), move |conn| {
213 Community::read(conn, community_id)