3 community::announce::GetCommunity,
4 deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
8 objects::{community::ApubCommunity, person::ApubPerson},
10 activities::deletion::delete::Delete,
11 objects::tombstone::Tombstone,
15 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
16 use activitystreams_kinds::activity::DeleteType;
18 use lemmy_api_common::utils::blocking;
19 use lemmy_db_schema::{
27 ModRemoveCommunityForm,
36 use lemmy_utils::error::LemmyError;
37 use lemmy_websocket::{
38 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
44 #[async_trait::async_trait(?Send)]
45 impl ActivityHandler for Delete {
46 type DataType = LemmyContext;
47 type Error = LemmyError;
49 fn id(&self) -> &Url {
53 fn actor(&self) -> &Url {
57 #[tracing::instrument(skip_all)]
60 context: &Data<LemmyContext>,
61 request_counter: &mut i32,
62 ) -> Result<(), LemmyError> {
63 verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?;
67 #[tracing::instrument(skip_all)]
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(
84 .dereference::<LemmyError>(context, local_instance(context), request_counter)
92 receive_delete_action(
105 pub(in crate::activities::deletion) fn new(
107 object: DeletableObjects,
109 community: Option<&Community>,
110 summary: Option<String>,
111 context: &LemmyContext,
112 ) -> Result<Delete, LemmyError> {
113 let id = generate_activity_id(
115 &context.settings().get_protocol_and_hostname(),
117 let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
119 actor: ObjectId::new(actor.actor_id.clone()),
121 object: IdOrNestedObject::NestedObject(Tombstone {
123 kind: Default::default(),
125 cc: cc.into_iter().collect(),
126 kind: DeleteType::Delete,
129 unparsed: Default::default(),
134 #[tracing::instrument(skip_all)]
135 pub(in crate::activities) async fn receive_remove_action(
138 reason: Option<String>,
139 context: &LemmyContext,
140 ) -> Result<(), LemmyError> {
141 use UserOperationCrud::*;
142 match DeletableObjects::read_from_db(object, context).await? {
143 DeletableObjects::Community(community) => {
145 return Err(LemmyError::from_message(
146 "Only local admin can remove community",
149 let form = ModRemoveCommunityForm {
150 mod_person_id: actor.id,
151 community_id: community.id,
156 blocking(context.pool(), move |conn| {
157 ModRemoveCommunity::create(conn, &form)
160 let deleted_community = blocking(context.pool(), move |conn| {
161 Community::update_removed(conn, community.id, true)
165 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
167 DeletableObjects::Post(post) => {
168 let form = ModRemovePostForm {
169 mod_person_id: actor.id,
174 blocking(context.pool(), move |conn| {
175 ModRemovePost::create(conn, &form)
178 let removed_post = blocking(context.pool(), move |conn| {
179 Post::update_removed(conn, post.id, true)
183 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
185 DeletableObjects::Comment(comment) => {
186 let form = ModRemoveCommentForm {
187 mod_person_id: actor.id,
188 comment_id: comment.id,
192 blocking(context.pool(), move |conn| {
193 ModRemoveComment::create(conn, &form)
196 let removed_comment = blocking(context.pool(), move |conn| {
197 Comment::update_removed(conn, comment.id, true)
201 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
203 DeletableObjects::PrivateMessage(_) => unimplemented!(),
208 #[async_trait::async_trait(?Send)]
209 impl GetCommunity for Delete {
210 #[tracing::instrument(skip_all)]
211 async fn get_community(
213 context: &LemmyContext,
214 _request_counter: &mut i32,
215 ) -> Result<ApubCommunity, LemmyError> {
216 let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? {
217 DeletableObjects::Community(c) => c.id,
218 DeletableObjects::Comment(c) => {
219 let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;
222 DeletableObjects::Post(p) => p.community_id,
223 DeletableObjects::PrivateMessage(_) => {
224 return Err(anyhow!("Private message is not part of community").into())
227 let community = blocking(context.pool(), move |conn| {
228 Community::read(conn, community_id)