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, NestedObject},
11 use activitystreams_kinds::activity::DeleteType;
13 use lemmy_api_common::blocking;
14 use lemmy_apub_lib::{data::Data, object_id::ObjectId, traits::ActivityHandler};
15 use lemmy_db_schema::{
23 ModRemoveCommunityForm,
32 use lemmy_utils::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;
44 #[tracing::instrument(skip_all)]
47 context: &Data<LemmyContext>,
48 request_counter: &mut i32,
49 ) -> Result<(), LemmyError> {
50 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
51 verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?;
55 #[tracing::instrument(skip_all)]
58 context: &Data<LemmyContext>,
59 request_counter: &mut i32,
60 ) -> Result<(), LemmyError> {
61 if let Some(reason) = self.summary {
62 // We set reason to empty string if it doesn't exist, to distinguish between delete and
63 // remove. Here we change it back to option, so we don't write it to db.
64 let reason = if reason.is_empty() {
69 receive_remove_action(
72 .dereference(context, context.client(), request_counter)
80 receive_delete_action(
93 pub(in crate::activities::deletion) fn new(
95 object: DeletableObjects,
97 community: Option<&Community>,
98 summary: Option<String>,
99 context: &LemmyContext,
100 ) -> Result<Delete, LemmyError> {
101 let id = generate_activity_id(
103 &context.settings().get_protocol_and_hostname(),
105 let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
107 actor: ObjectId::new(actor.actor_id.clone()),
109 object: IdOrNestedObject::NestedObject(NestedObject {
111 kind: Default::default(),
113 cc: cc.into_iter().collect(),
114 kind: DeleteType::Delete,
117 unparsed: Default::default(),
122 #[tracing::instrument(skip_all)]
123 pub(in crate::activities) async fn receive_remove_action(
126 reason: Option<String>,
127 context: &LemmyContext,
128 ) -> Result<(), LemmyError> {
129 use UserOperationCrud::*;
130 match DeletableObjects::read_from_db(object, context).await? {
131 DeletableObjects::Community(community) => {
133 return Err(LemmyError::from_message(
134 "Only local admin can remove community",
137 let form = ModRemoveCommunityForm {
138 mod_person_id: actor.id,
139 community_id: community.id,
144 blocking(context.pool(), move |conn| {
145 ModRemoveCommunity::create(conn, &form)
148 let deleted_community = blocking(context.pool(), move |conn| {
149 Community::update_removed(conn, community.id, true)
153 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
155 DeletableObjects::Post(post) => {
156 let form = ModRemovePostForm {
157 mod_person_id: actor.id,
162 blocking(context.pool(), move |conn| {
163 ModRemovePost::create(conn, &form)
166 let removed_post = blocking(context.pool(), move |conn| {
167 Post::update_removed(conn, post.id, true)
171 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
173 DeletableObjects::Comment(comment) => {
174 let form = ModRemoveCommentForm {
175 mod_person_id: actor.id,
176 comment_id: comment.id,
180 blocking(context.pool(), move |conn| {
181 ModRemoveComment::create(conn, &form)
184 let removed_comment = blocking(context.pool(), move |conn| {
185 Comment::update_removed(conn, comment.id, true)
189 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
191 DeletableObjects::PrivateMessage(_) => unimplemented!(),
196 #[async_trait::async_trait(?Send)]
197 impl GetCommunity for Delete {
198 #[tracing::instrument(skip_all)]
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.id(), 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,
211 DeletableObjects::PrivateMessage(_) => {
212 return Err(anyhow!("Private message is not part of community").into())
215 let community = blocking(context.pool(), move |conn| {
216 Community::read(conn, community_id)