]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/community.rs
Pleroma federation2 (#1855)
[lemmy.git] / crates / apub / src / objects / community.rs
1 use crate::{
2   check_is_apub_id_valid,
3   context::lemmy_context,
4   fetcher::community::{fetch_community_outbox, update_community_mods},
5   generate_moderators_url,
6   generate_outbox_url,
7   objects::{create_tombstone, ImageObject, Source},
8   CommunityType,
9 };
10 use activitystreams::{
11   actor::{kind::GroupType, Endpoints},
12   base::AnyBase,
13   chrono::NaiveDateTime,
14   object::{kind::ImageType, Tombstone},
15   primitives::OneOrMany,
16   unparsed::Unparsed,
17 };
18 use chrono::{DateTime, FixedOffset};
19 use itertools::Itertools;
20 use lemmy_api_common::blocking;
21 use lemmy_apub_lib::{
22   signatures::PublicKey,
23   traits::{ActorType, ApubObject, FromApub, ToApub},
24   values::{MediaTypeHtml, MediaTypeMarkdown},
25   verify::verify_domains_match,
26 };
27 use lemmy_db_schema::{
28   naive_now,
29   source::community::{Community, CommunityForm},
30   DbPool,
31 };
32 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
33 use lemmy_utils::{
34   settings::structs::Settings,
35   utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
36   LemmyError,
37 };
38 use lemmy_websocket::LemmyContext;
39 use log::debug;
40 use serde::{Deserialize, Serialize};
41 use serde_with::skip_serializing_none;
42 use std::ops::Deref;
43 use url::Url;
44
45 #[skip_serializing_none]
46 #[derive(Clone, Debug, Deserialize, Serialize)]
47 #[serde(rename_all = "camelCase")]
48 pub struct Group {
49   #[serde(rename = "@context")]
50   context: OneOrMany<AnyBase>,
51   #[serde(rename = "type")]
52   kind: GroupType,
53   id: Url,
54   /// username, set at account creation and can never be changed
55   preferred_username: String,
56   /// title (can be changed at any time)
57   name: String,
58   content: Option<String>,
59   media_type: Option<MediaTypeHtml>,
60   source: Option<Source>,
61   icon: Option<ImageObject>,
62   /// banner
63   image: Option<ImageObject>,
64   // lemmy extension
65   sensitive: Option<bool>,
66   // lemmy extension
67   pub(crate) moderators: Option<Url>,
68   inbox: Url,
69   pub(crate) outbox: Url,
70   followers: Url,
71   endpoints: Endpoints<Url>,
72   public_key: PublicKey,
73   published: Option<DateTime<FixedOffset>>,
74   updated: Option<DateTime<FixedOffset>>,
75   #[serde(flatten)]
76   unparsed: Unparsed,
77 }
78
79 impl Group {
80   pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
81     verify_domains_match(&self.id, expected_domain)?;
82     Ok(&self.id)
83   }
84   pub(crate) async fn from_apub_to_form(
85     group: &Group,
86     expected_domain: &Url,
87     settings: &Settings,
88   ) -> Result<CommunityForm, LemmyError> {
89     let actor_id = Some(group.id(expected_domain)?.clone().into());
90     let name = group.preferred_username.clone();
91     let title = group.name.clone();
92     let description = group.source.clone().map(|s| s.content);
93     let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
94
95     let slur_regex = &settings.slur_regex();
96     check_slurs(&name, slur_regex)?;
97     check_slurs(&title, slur_regex)?;
98     check_slurs_opt(&description, slur_regex)?;
99
100     Ok(CommunityForm {
101       name,
102       title,
103       description,
104       removed: None,
105       published: group.published.map(|u| u.naive_local()),
106       updated: group.updated.map(|u| u.naive_local()),
107       deleted: None,
108       nsfw: Some(group.sensitive.unwrap_or(false)),
109       actor_id,
110       local: Some(false),
111       private_key: None,
112       public_key: Some(group.public_key.public_key_pem.clone()),
113       last_refreshed_at: Some(naive_now()),
114       icon: Some(group.icon.clone().map(|i| i.url.into())),
115       banner: Some(group.image.clone().map(|i| i.url.into())),
116       followers_url: Some(group.followers.clone().into()),
117       inbox_url: Some(group.inbox.clone().into()),
118       shared_inbox_url: Some(shared_inbox),
119     })
120   }
121 }
122
123 #[derive(Clone, Debug)]
124 pub struct ApubCommunity(Community);
125
126 impl Deref for ApubCommunity {
127   type Target = Community;
128   fn deref(&self) -> &Self::Target {
129     &self.0
130   }
131 }
132
133 impl From<Community> for ApubCommunity {
134   fn from(c: Community) -> Self {
135     ApubCommunity { 0: c }
136   }
137 }
138
139 #[async_trait::async_trait(?Send)]
140 impl ApubObject for ApubCommunity {
141   type DataType = LemmyContext;
142
143   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
144     Some(self.last_refreshed_at)
145   }
146
147   async fn read_from_apub_id(
148     object_id: Url,
149     context: &LemmyContext,
150   ) -> Result<Option<Self>, LemmyError> {
151     Ok(
152       blocking(context.pool(), move |conn| {
153         Community::read_from_apub_id(conn, object_id)
154       })
155       .await??
156       .map(Into::into),
157     )
158   }
159
160   async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
161     blocking(context.pool(), move |conn| {
162       Community::update_deleted(conn, self.id, true)
163     })
164     .await??;
165     Ok(())
166   }
167 }
168
169 impl ActorType for ApubCommunity {
170   fn is_local(&self) -> bool {
171     self.local
172   }
173   fn actor_id(&self) -> Url {
174     self.actor_id.to_owned().into()
175   }
176   fn name(&self) -> String {
177     self.name.clone()
178   }
179   fn public_key(&self) -> Option<String> {
180     self.public_key.to_owned()
181   }
182   fn private_key(&self) -> Option<String> {
183     self.private_key.to_owned()
184   }
185
186   fn inbox_url(&self) -> Url {
187     self.inbox_url.clone().into()
188   }
189
190   fn shared_inbox_url(&self) -> Option<Url> {
191     self.shared_inbox_url.clone().map(|s| s.into_inner())
192   }
193 }
194
195 #[async_trait::async_trait(?Send)]
196 impl ToApub for ApubCommunity {
197   type ApubType = Group;
198   type TombstoneType = Tombstone;
199   type DataType = DbPool;
200
201   async fn to_apub(&self, _pool: &DbPool) -> Result<Group, LemmyError> {
202     let source = self.description.clone().map(|bio| Source {
203       content: bio,
204       media_type: MediaTypeMarkdown::Markdown,
205     });
206     let icon = self.icon.clone().map(|url| ImageObject {
207       kind: ImageType::Image,
208       url: url.into(),
209     });
210     let image = self.banner.clone().map(|url| ImageObject {
211       kind: ImageType::Image,
212       url: url.into(),
213     });
214
215     let group = Group {
216       context: lemmy_context(),
217       kind: GroupType::Group,
218       id: self.actor_id(),
219       preferred_username: self.name.clone(),
220       name: self.title.clone(),
221       content: self.description.as_ref().map(|b| markdown_to_html(b)),
222       media_type: self.description.as_ref().map(|_| MediaTypeHtml::Html),
223       source,
224       icon,
225       image,
226       sensitive: Some(self.nsfw),
227       moderators: Some(generate_moderators_url(&self.actor_id)?.into()),
228       inbox: self.inbox_url.clone().into(),
229       outbox: generate_outbox_url(&self.actor_id)?.into(),
230       followers: self.followers_url.clone().into(),
231       endpoints: Endpoints {
232         shared_inbox: self.shared_inbox_url.clone().map(|s| s.into()),
233         ..Default::default()
234       },
235       public_key: self.get_public_key()?,
236       published: Some(convert_datetime(self.published)),
237       updated: self.updated.map(convert_datetime),
238       unparsed: Default::default(),
239     };
240     Ok(group)
241   }
242
243   fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
244     create_tombstone(
245       self.deleted,
246       self.actor_id.to_owned().into(),
247       self.updated,
248       GroupType::Group,
249     )
250   }
251 }
252
253 #[async_trait::async_trait(?Send)]
254 impl FromApub for ApubCommunity {
255   type ApubType = Group;
256   type DataType = LemmyContext;
257
258   /// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
259   async fn from_apub(
260     group: &Group,
261     context: &LemmyContext,
262     expected_domain: &Url,
263     request_counter: &mut i32,
264   ) -> Result<ApubCommunity, LemmyError> {
265     let form = Group::from_apub_to_form(group, expected_domain, &context.settings()).await?;
266
267     // Fetching mods and outbox is not necessary for Lemmy to work, so ignore errors. Besides,
268     // we need to ignore these errors so that tests can work entirely offline.
269     let community = blocking(context.pool(), move |conn| Community::upsert(conn, &form)).await??;
270     update_community_mods(group, &community, context, request_counter)
271       .await
272       .map_err(|e| debug!("{}", e))
273       .ok();
274
275     // TODO: doing this unconditionally might cause infinite loop for some reason
276     fetch_community_outbox(context, &group.outbox, request_counter)
277       .await
278       .map_err(|e| debug!("{}", e))
279       .ok();
280
281     Ok(community.into())
282   }
283 }
284
285 #[async_trait::async_trait(?Send)]
286 impl CommunityType for Community {
287   fn followers_url(&self) -> Url {
288     self.followers_url.clone().into()
289   }
290
291   /// For a given community, returns the inboxes of all followers.
292   async fn get_follower_inboxes(
293     &self,
294     pool: &DbPool,
295     settings: &Settings,
296   ) -> Result<Vec<Url>, LemmyError> {
297     let id = self.id;
298
299     let follows = blocking(pool, move |conn| {
300       CommunityFollowerView::for_community(conn, id)
301     })
302     .await??;
303     let inboxes = follows
304       .into_iter()
305       .filter(|f| !f.follower.local)
306       .map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
307       .map(|i| i.into_inner())
308       .unique()
309       // Don't send to blocked instances
310       .filter(|inbox| check_is_apub_id_valid(inbox, false, settings).is_ok())
311       .collect();
312
313     Ok(inboxes)
314   }
315 }
316
317 #[cfg(test)]
318 mod tests {
319   use super::*;
320   use crate::objects::tests::{file_to_json_object, init_context};
321   use assert_json_diff::assert_json_include;
322   use lemmy_db_schema::traits::Crud;
323   use serial_test::serial;
324
325   #[actix_rt::test]
326   #[serial]
327   async fn test_fetch_lemmy_community() {
328     let context = init_context();
329     let mut json: Group = file_to_json_object("assets/lemmy-community.json");
330     let json_orig = json.clone();
331     // change these links so they dont fetch over the network
332     json.moderators = Some(Url::parse("https://lemmy.ml/c/announcements/not_moderators").unwrap());
333     json.outbox = Url::parse("https://lemmy.ml/c/announcements/not_outbox").unwrap();
334
335     let url = Url::parse("https://lemmy.ml/c/announcements").unwrap();
336     let mut request_counter = 0;
337     let community = ApubCommunity::from_apub(&json, &context, &url, &mut request_counter)
338       .await
339       .unwrap();
340
341     assert_eq!(community.actor_id.clone().into_inner(), url);
342     assert_eq!(community.title, "Announcements");
343     assert!(community.public_key.is_some());
344     assert!(!community.local);
345     assert_eq!(community.description.as_ref().unwrap().len(), 126);
346     // this makes two requests to the (intentionally) broken outbox/moderators collections
347     assert_eq!(request_counter, 2);
348
349     let to_apub = community.to_apub(context.pool()).await.unwrap();
350     assert_json_include!(actual: json_orig, expected: to_apub);
351
352     Community::delete(&*context.pool().get().unwrap(), community.id).unwrap();
353   }
354 }