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