3 community::announce::GetCommunity,
4 deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
8 objects::{community::ApubCommunity, person::ApubPerson},
9 protocol::{activities::deletion::delete::Delete, IdOrNestedObject},
11 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
12 use activitystreams_kinds::activity::DeleteType;
14 use lemmy_api_common::utils::blocking;
15 use lemmy_db_schema::{
23 ModRemoveCommunityForm,
32 use lemmy_utils::error::LemmyError;
33 use lemmy_websocket::{
34 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
40 #[async_trait::async_trait(?Send)]
41 impl ActivityHandler for Delete {
42 type DataType = LemmyContext;
43 type Error = LemmyError;
45 fn id(&self) -> &Url {
49 fn actor(&self) -> &Url {
53 #[tracing::instrument(skip_all)]
56 context: &Data<LemmyContext>,
57 request_counter: &mut i32,
58 ) -> Result<(), LemmyError> {
59 verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?;
63 #[tracing::instrument(skip_all)]
66 context: &Data<LemmyContext>,
67 request_counter: &mut i32,
68 ) -> Result<(), LemmyError> {
69 if let Some(reason) = self.summary {
70 // We set reason to empty string if it doesn't exist, to distinguish between delete and
71 // remove. Here we change it back to option, so we don't write it to db.
72 let reason = if reason.is_empty() {
77 receive_remove_action(
80 .dereference::<LemmyError>(context, local_instance(context), request_counter)
88 receive_delete_action(
101 pub(in crate::activities::deletion) fn new(
103 object: DeletableObjects,
105 community: Option<&Community>,
106 summary: Option<String>,
107 context: &LemmyContext,
108 ) -> Result<Delete, LemmyError> {
109 let id = generate_activity_id(
111 &context.settings().get_protocol_and_hostname(),
113 let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
115 actor: ObjectId::new(actor.actor_id.clone()),
117 object: IdOrNestedObject::Id(object.id()),
118 cc: cc.into_iter().collect(),
119 kind: DeleteType::Delete,
122 unparsed: Default::default(),
127 #[tracing::instrument(skip_all)]
128 pub(in crate::activities) async fn receive_remove_action(
131 reason: Option<String>,
132 context: &LemmyContext,
133 ) -> Result<(), LemmyError> {
134 use UserOperationCrud::*;
135 match DeletableObjects::read_from_db(object, context).await? {
136 DeletableObjects::Community(community) => {
138 return Err(LemmyError::from_message(
139 "Only local admin can remove community",
142 let form = ModRemoveCommunityForm {
143 mod_person_id: actor.id,
144 community_id: community.id,
149 blocking(context.pool(), move |conn| {
150 ModRemoveCommunity::create(conn, &form)
153 let deleted_community = blocking(context.pool(), move |conn| {
154 Community::update_removed(conn, community.id, true)
158 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
160 DeletableObjects::Post(post) => {
161 let form = ModRemovePostForm {
162 mod_person_id: actor.id,
167 blocking(context.pool(), move |conn| {
168 ModRemovePost::create(conn, &form)
171 let removed_post = blocking(context.pool(), move |conn| {
172 Post::update_removed(conn, post.id, true)
176 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
178 DeletableObjects::Comment(comment) => {
179 let form = ModRemoveCommentForm {
180 mod_person_id: actor.id,
181 comment_id: comment.id,
185 blocking(context.pool(), move |conn| {
186 ModRemoveComment::create(conn, &form)
189 let removed_comment = blocking(context.pool(), move |conn| {
190 Comment::update_removed(conn, comment.id, true)
194 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
196 DeletableObjects::PrivateMessage(_) => unimplemented!(),
201 #[async_trait::async_trait(?Send)]
202 impl GetCommunity for Delete {
203 #[tracing::instrument(skip_all)]
204 async fn get_community(
206 context: &LemmyContext,
207 _request_counter: &mut i32,
208 ) -> Result<ApubCommunity, LemmyError> {
209 let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? {
210 DeletableObjects::Community(c) => c.id,
211 DeletableObjects::Comment(c) => {
212 let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;
215 DeletableObjects::Post(p) => p.community_id,
216 DeletableObjects::PrivateMessage(_) => {
217 return Err(anyhow!("Private message is not part of community").into())
220 let community = blocking(context.pool(), move |conn| {
221 Community::read(conn, community_id)