]> Untitled Git - lemmy.git/blob - src/apub/inbox/activities/delete.rs
Fix nginx config for local federation setup (#104)
[lemmy.git] / src / apub / inbox / activities / delete.rs
1 use crate::{
2   apub::{
3     fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
4     inbox::shared_inbox::{
5       announce_if_community_is_local,
6       get_user_from_activity,
7       receive_unhandled_activity,
8     },
9     ActorType,
10     FromApub,
11     GroupExt,
12     PageExt,
13   },
14   LemmyContext,
15 };
16 use activitystreams::{activity::Delete, base::AnyBase, object::Note, prelude::*};
17 use actix_web::HttpResponse;
18 use anyhow::Context;
19 use lemmy_db::{
20   comment::{Comment, CommentForm},
21   comment_view::CommentView,
22   community::{Community, CommunityForm},
23   community_view::CommunityView,
24   naive_now,
25   post::{Post, PostForm},
26   post_view::PostView,
27   Crud,
28 };
29 use lemmy_structs::{
30   blocking,
31   comment::CommentResponse,
32   community::CommunityResponse,
33   post::PostResponse,
34   websocket::{SendComment, SendCommunityRoomMessage, SendPost, UserOperation},
35 };
36 use lemmy_utils::{location_info, LemmyError};
37
38 pub async fn receive_delete(
39   activity: AnyBase,
40   context: &LemmyContext,
41 ) -> Result<HttpResponse, LemmyError> {
42   let delete = Delete::from_any_base(activity)?.context(location_info!())?;
43   match delete.object().as_single_kind_str() {
44     Some("Page") => receive_delete_post(delete, context).await,
45     Some("Note") => receive_delete_comment(delete, context).await,
46     Some("Group") => receive_delete_community(delete, context).await,
47     _ => receive_unhandled_activity(delete),
48   }
49 }
50
51 async fn receive_delete_post(
52   delete: Delete,
53   context: &LemmyContext,
54 ) -> Result<HttpResponse, LemmyError> {
55   let user = get_user_from_activity(&delete, context).await?;
56   let page = PageExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
57     .context(location_info!())?;
58
59   let post_ap_id = PostForm::from_apub(&page, context, Some(user.actor_id()?))
60     .await?
61     .get_ap_id()?;
62
63   let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
64
65   let post_form = PostForm {
66     name: post.name.to_owned(),
67     url: post.url.to_owned(),
68     body: post.body.to_owned(),
69     creator_id: post.creator_id.to_owned(),
70     community_id: post.community_id,
71     removed: None,
72     deleted: Some(true),
73     nsfw: post.nsfw,
74     locked: None,
75     stickied: None,
76     updated: Some(naive_now()),
77     embed_title: post.embed_title,
78     embed_description: post.embed_description,
79     embed_html: post.embed_html,
80     thumbnail_url: post.thumbnail_url,
81     ap_id: Some(post.ap_id),
82     local: post.local,
83     published: None,
84   };
85   let post_id = post.id;
86   blocking(context.pool(), move |conn| {
87     Post::update(conn, post_id, &post_form)
88   })
89   .await??;
90
91   // Refetch the view
92   let post_id = post.id;
93   let post_view = blocking(context.pool(), move |conn| {
94     PostView::read(conn, post_id, None)
95   })
96   .await??;
97
98   let res = PostResponse { post: post_view };
99
100   context.chat_server().do_send(SendPost {
101     op: UserOperation::EditPost,
102     post: res,
103     websocket_id: None,
104   });
105
106   announce_if_community_is_local(delete, &user, context).await?;
107   Ok(HttpResponse::Ok().finish())
108 }
109
110 async fn receive_delete_comment(
111   delete: Delete,
112   context: &LemmyContext,
113 ) -> Result<HttpResponse, LemmyError> {
114   let user = get_user_from_activity(&delete, context).await?;
115   let note = Note::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
116     .context(location_info!())?;
117
118   let comment_ap_id = CommentForm::from_apub(&note, context, Some(user.actor_id()?))
119     .await?
120     .get_ap_id()?;
121
122   let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
123
124   let comment_form = CommentForm {
125     content: comment.content.to_owned(),
126     parent_id: comment.parent_id,
127     post_id: comment.post_id,
128     creator_id: comment.creator_id,
129     removed: None,
130     deleted: Some(true),
131     read: None,
132     published: None,
133     updated: Some(naive_now()),
134     ap_id: Some(comment.ap_id),
135     local: comment.local,
136   };
137   let comment_id = comment.id;
138   blocking(context.pool(), move |conn| {
139     Comment::update(conn, comment_id, &comment_form)
140   })
141   .await??;
142
143   // Refetch the view
144   let comment_id = comment.id;
145   let comment_view = blocking(context.pool(), move |conn| {
146     CommentView::read(conn, comment_id, None)
147   })
148   .await??;
149
150   // TODO get those recipient actor ids from somewhere
151   let recipient_ids = vec![];
152   let res = CommentResponse {
153     comment: comment_view,
154     recipient_ids,
155     form_id: None,
156   };
157
158   context.chat_server().do_send(SendComment {
159     op: UserOperation::EditComment,
160     comment: res,
161     websocket_id: None,
162   });
163
164   announce_if_community_is_local(delete, &user, context).await?;
165   Ok(HttpResponse::Ok().finish())
166 }
167
168 async fn receive_delete_community(
169   delete: Delete,
170   context: &LemmyContext,
171 ) -> Result<HttpResponse, LemmyError> {
172   let group = GroupExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
173     .context(location_info!())?;
174   let user = get_user_from_activity(&delete, context).await?;
175
176   let community_actor_id = CommunityForm::from_apub(&group, context, Some(user.actor_id()?))
177     .await?
178     .actor_id
179     .context(location_info!())?;
180
181   let community = blocking(context.pool(), move |conn| {
182     Community::read_from_actor_id(conn, &community_actor_id)
183   })
184   .await??;
185
186   let community_form = CommunityForm {
187     name: community.name.to_owned(),
188     title: community.title.to_owned(),
189     description: community.description.to_owned(),
190     category_id: community.category_id, // Note: need to keep this due to foreign key constraint
191     creator_id: community.creator_id,   // Note: need to keep this due to foreign key constraint
192     removed: None,
193     published: None,
194     updated: Some(naive_now()),
195     deleted: Some(true),
196     nsfw: community.nsfw,
197     actor_id: Some(community.actor_id),
198     local: community.local,
199     private_key: community.private_key,
200     public_key: community.public_key,
201     last_refreshed_at: None,
202     icon: Some(community.icon.to_owned()),
203     banner: Some(community.banner.to_owned()),
204   };
205
206   let community_id = community.id;
207   blocking(context.pool(), move |conn| {
208     Community::update(conn, community_id, &community_form)
209   })
210   .await??;
211
212   let community_id = community.id;
213   let res = CommunityResponse {
214     community: blocking(context.pool(), move |conn| {
215       CommunityView::read(conn, community_id, None)
216     })
217     .await??,
218   };
219
220   let community_id = res.community.id;
221
222   context.chat_server().do_send(SendCommunityRoomMessage {
223     op: UserOperation::EditCommunity,
224     response: res,
225     community_id,
226     websocket_id: None,
227   });
228
229   announce_if_community_is_local(delete, &user, context).await?;
230   Ok(HttpResponse::Ok().finish())
231 }