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,
31 use lemmy_utils::error::LemmyError;
32 use lemmy_websocket::{
33 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
39 #[async_trait::async_trait(?Send)]
40 impl ActivityHandler for Delete {
41 type DataType = LemmyContext;
42 type Error = LemmyError;
44 fn id(&self) -> &Url {
48 fn actor(&self) -> &Url {
52 #[tracing::instrument(skip_all)]
55 context: &Data<LemmyContext>,
56 request_counter: &mut i32,
57 ) -> Result<(), LemmyError> {
58 verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?;
62 #[tracing::instrument(skip_all)]
65 context: &Data<LemmyContext>,
66 request_counter: &mut i32,
67 ) -> Result<(), LemmyError> {
68 if let Some(reason) = self.summary {
69 // We set reason to empty string if it doesn't exist, to distinguish between delete and
70 // remove. Here we change it back to option, so we don't write it to db.
71 let reason = if reason.is_empty() {
76 receive_remove_action(
79 .dereference(context, local_instance(context), request_counter)
87 receive_delete_action(
100 pub(in crate::activities::deletion) fn new(
102 object: DeletableObjects,
104 community: Option<&Community>,
105 summary: Option<String>,
106 context: &LemmyContext,
107 ) -> Result<Delete, LemmyError> {
108 let id = generate_activity_id(
110 &context.settings().get_protocol_and_hostname(),
112 let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
114 actor: ObjectId::new(actor.actor_id.clone()),
116 object: IdOrNestedObject::Id(object.id()),
117 cc: cc.into_iter().collect(),
118 kind: DeleteType::Delete,
121 unparsed: Default::default(),
126 #[tracing::instrument(skip_all)]
127 pub(in crate::activities) async fn receive_remove_action(
130 reason: Option<String>,
131 context: &LemmyContext,
132 ) -> Result<(), LemmyError> {
133 use UserOperationCrud::*;
134 match DeletableObjects::read_from_db(object, context).await? {
135 DeletableObjects::Community(community) => {
137 return Err(LemmyError::from_message(
138 "Only local admin can remove community",
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?;
195 DeletableObjects::PrivateMessage(_) => unimplemented!(),
200 #[async_trait::async_trait(?Send)]
201 impl GetCommunity for Delete {
202 #[tracing::instrument(skip_all)]
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.id(), 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,
215 DeletableObjects::PrivateMessage(_) => {
216 return Err(anyhow!("Private message is not part of community").into())
219 let community = blocking(context.pool(), move |conn| {
220 Community::read(conn, community_id)