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