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 activitystreams_kinds::activity::DeleteType;
17 use lemmy_api_common::utils::blocking;
18 use lemmy_apub_lib::{data::Data, object_id::ObjectId, traits::ActivityHandler};
19 use lemmy_db_schema::{
27 ModRemoveCommunityForm,
36 use lemmy_utils::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;
48 #[tracing::instrument(skip_all)]
51 context: &Data<LemmyContext>,
52 request_counter: &mut i32,
53 ) -> Result<(), LemmyError> {
54 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
55 verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?;
59 #[tracing::instrument(skip_all)]
62 context: &Data<LemmyContext>,
63 request_counter: &mut i32,
64 ) -> Result<(), LemmyError> {
65 if let Some(reason) = self.summary {
66 // We set reason to empty string if it doesn't exist, to distinguish between delete and
67 // remove. Here we change it back to option, so we don't write it to db.
68 let reason = if reason.is_empty() {
73 receive_remove_action(
76 .dereference(context, context.client(), request_counter)
84 receive_delete_action(
97 pub(in crate::activities::deletion) fn new(
99 object: DeletableObjects,
101 community: Option<&Community>,
102 summary: Option<String>,
103 context: &LemmyContext,
104 ) -> Result<Delete, LemmyError> {
105 let id = generate_activity_id(
107 &context.settings().get_protocol_and_hostname(),
109 let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
111 actor: ObjectId::new(actor.actor_id.clone()),
113 object: IdOrNestedObject::NestedObject(Tombstone {
115 kind: Default::default(),
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)