]> Untitled Git - lemmy.git/blob - lemmy_apub/src/community.rs
Fix nginx config for local federation setup (#104)
[lemmy.git] / lemmy_apub / src / community.rs
1 use crate::{
2   activities::generate_activity_id,
3   activity_queue::send_activity,
4   check_actor_domain,
5   create_apub_response,
6   create_apub_tombstone_response,
7   create_tombstone,
8   extensions::group_extensions::GroupExtension,
9   fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_user},
10   insert_activity,
11   ActorType,
12   FromApub,
13   GroupExt,
14   ToApub,
15 };
16 use activitystreams::{
17   activity::{
18     kind::{AcceptType, AnnounceType, DeleteType, LikeType, RemoveType, UndoType},
19     Accept,
20     Announce,
21     Delete,
22     Follow,
23     Remove,
24     Undo,
25   },
26   actor::{kind::GroupType, ApActor, Endpoints, Group},
27   base::{AnyBase, BaseExt},
28   collection::{OrderedCollection, UnorderedCollection},
29   object::{Image, Tombstone},
30   prelude::*,
31   public,
32 };
33 use activitystreams_ext::Ext2;
34 use actix_web::{body::Body, web, HttpResponse};
35 use anyhow::Context;
36 use itertools::Itertools;
37 use lemmy_db::{
38   community::{Community, CommunityForm},
39   community_view::{CommunityFollowerView, CommunityModeratorView},
40   naive_now,
41   post::Post,
42   user::User_,
43   DbPool,
44 };
45 use lemmy_structs::blocking;
46 use lemmy_utils::{
47   apub::get_apub_protocol_string,
48   location_info,
49   utils::{check_slurs, check_slurs_opt, convert_datetime},
50   LemmyError,
51 };
52 use lemmy_websocket::LemmyContext;
53 use serde::Deserialize;
54 use url::Url;
55
56 #[derive(Deserialize)]
57 pub struct CommunityQuery {
58   community_name: String,
59 }
60
61 #[async_trait::async_trait(?Send)]
62 impl ToApub for Community {
63   type Response = GroupExt;
64
65   // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
66   async fn to_apub(&self, pool: &DbPool) -> Result<GroupExt, LemmyError> {
67     // The attributed to, is an ordered vector with the creator actor_ids first,
68     // then the rest of the moderators
69     // TODO Technically the instance admins can mod the community, but lets
70     // ignore that for now
71     let id = self.id;
72     let moderators = blocking(pool, move |conn| {
73       CommunityModeratorView::for_community(&conn, id)
74     })
75     .await??;
76     let moderators: Vec<String> = moderators.into_iter().map(|m| m.user_actor_id).collect();
77
78     let mut group = Group::new();
79     group
80       .set_context(activitystreams::context())
81       .set_id(Url::parse(&self.actor_id)?)
82       .set_name(self.name.to_owned())
83       .set_published(convert_datetime(self.published))
84       .set_many_attributed_tos(moderators);
85
86     if let Some(u) = self.updated.to_owned() {
87       group.set_updated(convert_datetime(u));
88     }
89     if let Some(d) = self.description.to_owned() {
90       // TODO: this should be html, also add source field with raw markdown
91       //       -> same for post.content and others
92       group.set_content(d);
93     }
94
95     if let Some(icon) = &self.icon {
96       let mut image = Image::new();
97       image.set_url(icon.to_owned());
98       group.set_icon(image.into_any_base()?);
99     }
100
101     if let Some(banner_url) = &self.banner {
102       let mut image = Image::new();
103       image.set_url(banner_url.to_owned());
104       group.set_image(image.into_any_base()?);
105     }
106
107     let mut ap_actor = ApActor::new(self.get_inbox_url()?, group);
108     ap_actor
109       .set_preferred_username(self.title.to_owned())
110       .set_outbox(self.get_outbox_url()?)
111       .set_followers(self.get_followers_url()?)
112       .set_following(self.get_following_url().parse()?)
113       .set_liked(self.get_liked_url().parse()?)
114       .set_endpoints(Endpoints {
115         shared_inbox: Some(self.get_shared_inbox_url()?),
116         ..Default::default()
117       });
118
119     let nsfw = self.nsfw;
120     let category_id = self.category_id;
121     let group_extension = blocking(pool, move |conn| {
122       GroupExtension::new(conn, category_id, nsfw)
123     })
124     .await??;
125
126     Ok(Ext2::new(
127       ap_actor,
128       group_extension,
129       self.get_public_key_ext()?,
130     ))
131   }
132
133   fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
134     create_tombstone(self.deleted, &self.actor_id, self.updated, GroupType::Group)
135   }
136 }
137
138 #[async_trait::async_trait(?Send)]
139 impl ActorType for Community {
140   fn actor_id_str(&self) -> String {
141     self.actor_id.to_owned()
142   }
143
144   fn public_key(&self) -> Option<String> {
145     self.public_key.to_owned()
146   }
147   fn private_key(&self) -> Option<String> {
148     self.private_key.to_owned()
149   }
150
151   /// As a local community, accept the follow request from a remote user.
152   async fn send_accept_follow(
153     &self,
154     follow: Follow,
155     context: &LemmyContext,
156   ) -> Result<(), LemmyError> {
157     let actor_uri = follow
158       .actor()?
159       .as_single_xsd_any_uri()
160       .context(location_info!())?;
161     let actor = get_or_fetch_and_upsert_actor(actor_uri, context).await?;
162
163     let mut accept = Accept::new(self.actor_id.to_owned(), follow.into_any_base()?);
164     let to = actor.get_inbox_url()?;
165     accept
166       .set_context(activitystreams::context())
167       .set_id(generate_activity_id(AcceptType::Accept)?)
168       .set_to(to.clone());
169
170     insert_activity(self.creator_id, accept.clone(), true, context.pool()).await?;
171
172     send_activity(context.activity_queue(), accept, self, vec![to])?;
173     Ok(())
174   }
175
176   async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
177     let group = self.to_apub(context.pool()).await?;
178
179     let mut delete = Delete::new(creator.actor_id.to_owned(), group.into_any_base()?);
180     delete
181       .set_context(activitystreams::context())
182       .set_id(generate_activity_id(DeleteType::Delete)?)
183       .set_to(public())
184       .set_many_ccs(vec![self.get_followers_url()?]);
185
186     insert_activity(self.creator_id, delete.clone(), true, context.pool()).await?;
187
188     let inboxes = self.get_follower_inboxes(context.pool()).await?;
189
190     // Note: For an accept, since it was automatic, no one pushed a button,
191     // the community was the actor.
192     // But for delete, the creator is the actor, and does the signing
193     send_activity(context.activity_queue(), delete, creator, inboxes)?;
194     Ok(())
195   }
196
197   async fn send_undo_delete(
198     &self,
199     creator: &User_,
200     context: &LemmyContext,
201   ) -> Result<(), LemmyError> {
202     let group = self.to_apub(context.pool()).await?;
203
204     let mut delete = Delete::new(creator.actor_id.to_owned(), group.into_any_base()?);
205     delete
206       .set_context(activitystreams::context())
207       .set_id(generate_activity_id(DeleteType::Delete)?)
208       .set_to(public())
209       .set_many_ccs(vec![self.get_followers_url()?]);
210
211     let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
212     undo
213       .set_context(activitystreams::context())
214       .set_id(generate_activity_id(UndoType::Undo)?)
215       .set_to(public())
216       .set_many_ccs(vec![self.get_followers_url()?]);
217
218     insert_activity(self.creator_id, undo.clone(), true, context.pool()).await?;
219
220     let inboxes = self.get_follower_inboxes(context.pool()).await?;
221
222     // Note: For an accept, since it was automatic, no one pushed a button,
223     // the community was the actor.
224     // But for delete, the creator is the actor, and does the signing
225     send_activity(context.activity_queue(), undo, creator, inboxes)?;
226     Ok(())
227   }
228
229   async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
230     let group = self.to_apub(context.pool()).await?;
231
232     let mut remove = Remove::new(mod_.actor_id.to_owned(), group.into_any_base()?);
233     remove
234       .set_context(activitystreams::context())
235       .set_id(generate_activity_id(RemoveType::Remove)?)
236       .set_to(public())
237       .set_many_ccs(vec![self.get_followers_url()?]);
238
239     insert_activity(mod_.id, remove.clone(), true, context.pool()).await?;
240
241     let inboxes = self.get_follower_inboxes(context.pool()).await?;
242
243     // Note: For an accept, since it was automatic, no one pushed a button,
244     // the community was the actor.
245     // But for delete, the creator is the actor, and does the signing
246     send_activity(context.activity_queue(), remove, mod_, inboxes)?;
247     Ok(())
248   }
249
250   async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
251     let group = self.to_apub(context.pool()).await?;
252
253     let mut remove = Remove::new(mod_.actor_id.to_owned(), group.into_any_base()?);
254     remove
255       .set_context(activitystreams::context())
256       .set_id(generate_activity_id(RemoveType::Remove)?)
257       .set_to(public())
258       .set_many_ccs(vec![self.get_followers_url()?]);
259
260     // Undo that fake activity
261     let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
262     undo
263       .set_context(activitystreams::context())
264       .set_id(generate_activity_id(LikeType::Like)?)
265       .set_to(public())
266       .set_many_ccs(vec![self.get_followers_url()?]);
267
268     insert_activity(mod_.id, undo.clone(), true, context.pool()).await?;
269
270     let inboxes = self.get_follower_inboxes(context.pool()).await?;
271
272     // Note: For an accept, since it was automatic, no one pushed a button,
273     // the community was the actor.
274     // But for remove , the creator is the actor, and does the signing
275     send_activity(context.activity_queue(), undo, mod_, inboxes)?;
276     Ok(())
277   }
278
279   /// For a given community, returns the inboxes of all followers.
280   ///
281   /// TODO: this function is very badly implemented, we should just store shared_inbox_url in
282   ///       CommunityFollowerView
283   async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
284     let id = self.id;
285
286     let inboxes = blocking(pool, move |conn| {
287       CommunityFollowerView::for_community(conn, id)
288     })
289     .await??;
290     let inboxes = inboxes
291       .into_iter()
292       .map(|u| -> Result<Url, LemmyError> {
293         let url = Url::parse(&u.user_actor_id)?;
294         let domain = url.domain().context(location_info!())?;
295         let port = if let Some(port) = url.port() {
296           format!(":{}", port)
297         } else {
298           "".to_string()
299         };
300         Ok(Url::parse(&format!(
301           "{}://{}{}/inbox",
302           get_apub_protocol_string(),
303           domain,
304           port,
305         ))?)
306       })
307       .filter_map(Result::ok)
308       .unique()
309       .collect();
310
311     Ok(inboxes)
312   }
313
314   async fn send_follow(
315     &self,
316     _follow_actor_id: &Url,
317     _context: &LemmyContext,
318   ) -> Result<(), LemmyError> {
319     unimplemented!()
320   }
321
322   async fn send_unfollow(
323     &self,
324     _follow_actor_id: &Url,
325     _context: &LemmyContext,
326   ) -> Result<(), LemmyError> {
327     unimplemented!()
328   }
329
330   fn user_id(&self) -> i32 {
331     self.creator_id
332   }
333 }
334
335 #[async_trait::async_trait(?Send)]
336 impl FromApub for CommunityForm {
337   type ApubType = GroupExt;
338
339   /// Parse an ActivityPub group received from another instance into a Lemmy community.
340   async fn from_apub(
341     group: &GroupExt,
342     context: &LemmyContext,
343     expected_domain: Option<Url>,
344   ) -> Result<Self, LemmyError> {
345     let creator_and_moderator_uris = group.inner.attributed_to().context(location_info!())?;
346     let creator_uri = creator_and_moderator_uris
347       .as_many()
348       .context(location_info!())?
349       .iter()
350       .next()
351       .context(location_info!())?
352       .as_xsd_any_uri()
353       .context(location_info!())?;
354
355     let creator = get_or_fetch_and_upsert_user(creator_uri, context).await?;
356     let name = group
357       .inner
358       .name()
359       .context(location_info!())?
360       .as_one()
361       .context(location_info!())?
362       .as_xsd_string()
363       .context(location_info!())?
364       .to_string();
365     let title = group
366       .inner
367       .preferred_username()
368       .context(location_info!())?
369       .to_string();
370     // TODO: should be parsed as html and tags like <script> removed (or use markdown source)
371     //       -> same for post.content etc
372     let description = group
373       .inner
374       .content()
375       .map(|s| s.as_single_xsd_string())
376       .flatten()
377       .map(|s| s.to_string());
378     check_slurs(&name)?;
379     check_slurs(&title)?;
380     check_slurs_opt(&description)?;
381
382     let icon = match group.icon() {
383       Some(any_image) => Some(
384         Image::from_any_base(any_image.as_one().context(location_info!())?.clone())
385           .context(location_info!())?
386           .context(location_info!())?
387           .url()
388           .context(location_info!())?
389           .as_single_xsd_any_uri()
390           .map(|u| u.to_string()),
391       ),
392       None => None,
393     };
394
395     let banner = match group.image() {
396       Some(any_image) => Some(
397         Image::from_any_base(any_image.as_one().context(location_info!())?.clone())
398           .context(location_info!())?
399           .context(location_info!())?
400           .url()
401           .context(location_info!())?
402           .as_single_xsd_any_uri()
403           .map(|u| u.to_string()),
404       ),
405       None => None,
406     };
407
408     Ok(CommunityForm {
409       name,
410       title,
411       description,
412       category_id: group.ext_one.category.identifier.parse::<i32>()?,
413       creator_id: creator.id,
414       removed: None,
415       published: group.inner.published().map(|u| u.to_owned().naive_local()),
416       updated: group.inner.updated().map(|u| u.to_owned().naive_local()),
417       deleted: None,
418       nsfw: group.ext_one.sensitive,
419       actor_id: Some(check_actor_domain(group, expected_domain)?),
420       local: false,
421       private_key: None,
422       public_key: Some(group.ext_two.to_owned().public_key.public_key_pem),
423       last_refreshed_at: Some(naive_now()),
424       icon,
425       banner,
426     })
427   }
428 }
429
430 /// Return the community json over HTTP.
431 pub async fn get_apub_community_http(
432   info: web::Path<CommunityQuery>,
433   context: web::Data<LemmyContext>,
434 ) -> Result<HttpResponse<Body>, LemmyError> {
435   let community = blocking(context.pool(), move |conn| {
436     Community::read_from_name(conn, &info.community_name)
437   })
438   .await??;
439
440   if !community.deleted {
441     let apub = community.to_apub(context.pool()).await?;
442
443     Ok(create_apub_response(&apub))
444   } else {
445     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
446   }
447 }
448
449 /// Returns an empty followers collection, only populating the size (for privacy).
450 pub async fn get_apub_community_followers(
451   info: web::Path<CommunityQuery>,
452   context: web::Data<LemmyContext>,
453 ) -> Result<HttpResponse<Body>, LemmyError> {
454   let community = blocking(context.pool(), move |conn| {
455     Community::read_from_name(&conn, &info.community_name)
456   })
457   .await??;
458
459   let community_id = community.id;
460   let community_followers = blocking(context.pool(), move |conn| {
461     CommunityFollowerView::for_community(&conn, community_id)
462   })
463   .await??;
464
465   let mut collection = UnorderedCollection::new();
466   collection
467     .set_context(activitystreams::context())
468     .set_id(community.get_followers_url()?)
469     .set_total_items(community_followers.len() as u64);
470   Ok(create_apub_response(&collection))
471 }
472
473 pub async fn get_apub_community_outbox(
474   info: web::Path<CommunityQuery>,
475   context: web::Data<LemmyContext>,
476 ) -> Result<HttpResponse<Body>, LemmyError> {
477   let community = blocking(context.pool(), move |conn| {
478     Community::read_from_name(&conn, &info.community_name)
479   })
480   .await??;
481
482   let community_id = community.id;
483   let posts = blocking(context.pool(), move |conn| {
484     Post::list_for_community(conn, community_id)
485   })
486   .await??;
487
488   let mut pages: Vec<AnyBase> = vec![];
489   for p in posts {
490     pages.push(p.to_apub(context.pool()).await?.into_any_base()?);
491   }
492
493   let len = pages.len();
494   let mut collection = OrderedCollection::new();
495   collection
496     .set_many_items(pages)
497     .set_context(activitystreams::context())
498     .set_id(community.get_outbox_url()?)
499     .set_total_items(len as u64);
500   Ok(create_apub_response(&collection))
501 }
502
503 pub async fn do_announce(
504   activity: AnyBase,
505   community: &Community,
506   sender: &User_,
507   context: &LemmyContext,
508 ) -> Result<(), LemmyError> {
509   let mut announce = Announce::new(community.actor_id.to_owned(), activity);
510   announce
511     .set_context(activitystreams::context())
512     .set_id(generate_activity_id(AnnounceType::Announce)?)
513     .set_to(public())
514     .set_many_ccs(vec![community.get_followers_url()?]);
515
516   insert_activity(community.creator_id, announce.clone(), true, context.pool()).await?;
517
518   let mut to: Vec<Url> = community.get_follower_inboxes(context.pool()).await?;
519
520   // dont send to the local instance, nor to the instance where the activity originally came from,
521   // because that would result in a database error (same data inserted twice)
522   // this seems to be the "easiest" stable alternative for remove_item()
523   let sender_shared_inbox = sender.get_shared_inbox_url()?;
524   to.retain(|x| x != &sender_shared_inbox);
525   let community_shared_inbox = community.get_shared_inbox_url()?;
526   to.retain(|x| x != &community_shared_inbox);
527
528   send_activity(context.activity_queue(), announce, community, to)?;
529
530   Ok(())
531 }