3 deletion::{delete::Delete, undo_delete::UndoDelete},
5 verify_person_in_community,
7 fetcher::object_id::ObjectId,
9 use diesel::PgConnection;
10 use lemmy_api_common::blocking;
12 traits::{ActivityFields, ActorType, ApubObject},
13 verify::verify_domains_match,
15 use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
16 use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
17 use lemmy_utils::LemmyError;
18 use lemmy_websocket::{
19 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
28 pub async fn send_apub_delete(
30 community: &Community,
33 context: &LemmyContext,
34 ) -> Result<(), LemmyError> {
36 Delete::send(actor, community, object_id, None, context).await
38 UndoDelete::send(actor, community, object_id, None, context).await
42 // TODO: remove reason is actually optional in lemmy. we set an empty string in that case, but its
44 pub async fn send_apub_remove(
46 community: &Community,
50 context: &LemmyContext,
51 ) -> Result<(), LemmyError> {
53 Delete::send(actor, community, object_id, Some(reason), context).await
55 UndoDelete::send(actor, community, object_id, Some(reason), context).await
59 pub enum DeletableObjects {
60 Community(Box<Community>),
61 Comment(Box<Comment>),
65 impl DeletableObjects {
66 pub(crate) async fn read_from_db(
68 context: &LemmyContext,
69 ) -> Result<DeletableObjects, LemmyError> {
71 DeletableObjects::read_type_from_db::<Community>(ap_id.clone(), context).await?
73 return Ok(DeletableObjects::Community(Box::new(c)));
75 if let Some(p) = DeletableObjects::read_type_from_db::<Post>(ap_id.clone(), context).await? {
76 return Ok(DeletableObjects::Post(Box::new(p)));
78 if let Some(c) = DeletableObjects::read_type_from_db::<Comment>(ap_id.clone(), context).await? {
79 return Ok(DeletableObjects::Comment(Box::new(c)));
81 Err(diesel::NotFound.into())
84 // TODO: a method like this should be provided by fetcher module
85 async fn read_type_from_db<Type>(
87 context: &LemmyContext,
88 ) -> Result<Option<Type>, LemmyError>
90 Type: ApubObject<DataType = PgConnection> + Send + 'static,
92 blocking(context.pool(), move |conn| {
93 Type::read_from_apub_id(conn, ap_id)
99 pub(in crate::activities) async fn verify_delete_activity(
101 activity: &dyn ActivityFields,
102 community_id: &ObjectId<Community>,
104 context: &LemmyContext,
105 request_counter: &mut i32,
106 ) -> Result<(), LemmyError> {
107 let object = DeletableObjects::read_from_db(object, context).await?;
108 let actor = ObjectId::new(activity.actor().clone());
110 DeletableObjects::Community(c) => {
112 // can only do this check for local community, in remote case it would try to fetch the
113 // deleted community (which fails)
114 verify_person_in_community(&actor, community_id, context, request_counter).await?;
116 // community deletion is always a mod (or admin) action
119 ObjectId::new(c.actor_id()),
125 DeletableObjects::Post(p) => {
126 verify_delete_activity_post_or_comment(
136 DeletableObjects::Comment(c) => {
137 verify_delete_activity_post_or_comment(
151 async fn verify_delete_activity_post_or_comment(
152 activity: &dyn ActivityFields,
154 community_id: &ObjectId<Community>,
156 context: &LemmyContext,
157 request_counter: &mut i32,
158 ) -> Result<(), LemmyError> {
159 let actor = ObjectId::new(activity.actor().clone());
160 verify_person_in_community(&actor, community_id, context, request_counter).await?;
162 verify_mod_action(&actor, community_id.clone(), context, request_counter).await?;
164 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
165 verify_domains_match(activity.actor(), object_id)?;
170 struct WebsocketMessages {
171 community: UserOperationCrud,
172 post: UserOperationCrud,
173 comment: UserOperationCrud,
176 /// Write deletion or restoring of an object to the database, and send websocket message.
177 /// TODO: we should do something similar for receive_remove_action(), but its much more complicated
178 /// because of the mod log
179 async fn receive_delete_action(
181 actor: &ObjectId<Person>,
182 ws_messages: WebsocketMessages,
184 context: &LemmyContext,
185 request_counter: &mut i32,
186 ) -> Result<(), LemmyError> {
187 match DeletableObjects::read_from_db(object, context).await? {
188 DeletableObjects::Community(community) => {
190 let mod_ = actor.dereference(context, request_counter).await?;
191 let object = community.actor_id();
192 send_apub_delete(&mod_, &community.clone(), object, true, context).await?;
195 let community = blocking(context.pool(), move |conn| {
196 Community::update_deleted(conn, community.id, deleted)
199 send_community_ws_message(community.id, ws_messages.community, None, None, context).await?;
201 DeletableObjects::Post(post) => {
202 let deleted_post = blocking(context.pool(), move |conn| {
203 Post::update_deleted(conn, post.id, deleted)
206 send_post_ws_message(deleted_post.id, ws_messages.post, None, None, context).await?;
208 DeletableObjects::Comment(comment) => {
209 let deleted_comment = blocking(context.pool(), move |conn| {
210 Comment::update_deleted(conn, comment.id, deleted)
213 send_comment_ws_message_simple(deleted_comment.id, ws_messages.comment, context).await?;