]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/group.rs
dbc823748bcbef77c89223053d383d1ba34e8e90
[lemmy.git] / crates / apub / src / protocol / objects / group.rs
1 use crate::{
2   check_is_apub_id_valid,
3   collections::{
4     community_moderators::ApubCommunityModerators,
5     community_outbox::ApubCommunityOutbox,
6   },
7   objects::{community::ApubCommunity, get_summary_from_string_or_source},
8   protocol::{ImageObject, Source},
9 };
10 use activitystreams::{
11   actor::{kind::GroupType, Endpoints},
12   unparsed::Unparsed,
13 };
14 use chrono::{DateTime, FixedOffset};
15 use lemmy_apub_lib::{object_id::ObjectId, signatures::PublicKey, verify::verify_domains_match};
16 use lemmy_db_schema::{naive_now, source::community::CommunityForm};
17 use lemmy_utils::{
18   settings::structs::Settings,
19   utils::{check_slurs, check_slurs_opt},
20   LemmyError,
21 };
22 use serde::{Deserialize, Serialize};
23 use serde_with::skip_serializing_none;
24 use url::Url;
25
26 #[skip_serializing_none]
27 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[serde(rename_all = "camelCase")]
29 pub struct Group {
30   #[serde(rename = "type")]
31   pub(crate) kind: GroupType,
32   pub(crate) id: ObjectId<ApubCommunity>,
33   /// username, set at account creation and can never be changed
34   pub(crate) preferred_username: String,
35   /// title (can be changed at any time)
36   pub(crate) name: String,
37   pub(crate) summary: Option<String>,
38   pub(crate) source: Option<Source>,
39   pub(crate) icon: Option<ImageObject>,
40   /// banner
41   pub(crate) image: Option<ImageObject>,
42   // lemmy extension
43   pub(crate) sensitive: Option<bool>,
44   // lemmy extension
45   pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
46   pub(crate) inbox: Url,
47   pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
48   pub(crate) followers: Url,
49   pub(crate) endpoints: Endpoints<Url>,
50   pub(crate) public_key: PublicKey,
51   pub(crate) published: Option<DateTime<FixedOffset>>,
52   pub(crate) updated: Option<DateTime<FixedOffset>>,
53   #[serde(flatten)]
54   pub(crate) unparsed: Unparsed,
55 }
56
57 impl Group {
58   pub(crate) async fn from_apub_to_form(
59     group: &Group,
60     expected_domain: &Url,
61     settings: &Settings,
62   ) -> Result<CommunityForm, LemmyError> {
63     check_is_apub_id_valid(group.id.inner(), true, settings)?;
64     verify_domains_match(expected_domain, group.id.inner())?;
65     let name = group.preferred_username.clone();
66     let title = group.name.clone();
67     let description = get_summary_from_string_or_source(&group.summary, &group.source);
68     let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
69
70     let slur_regex = &settings.slur_regex();
71     check_slurs(&name, slur_regex)?;
72     check_slurs(&title, slur_regex)?;
73     check_slurs_opt(&description, slur_regex)?;
74
75     // TODO: test_parse_lemmy_community_moderators() keeps failing here with stack overflow
76     Ok(CommunityForm {
77       name,
78       title,
79       description,
80       removed: None,
81       published: group.published.map(|u| u.naive_local()),
82       updated: group.updated.map(|u| u.naive_local()),
83       deleted: None,
84       nsfw: Some(group.sensitive.unwrap_or(false)),
85       actor_id: Some(group.id.clone().into()),
86       local: Some(false),
87       private_key: None,
88       public_key: Some(group.public_key.public_key_pem.clone()),
89       last_refreshed_at: Some(naive_now()),
90       icon: Some(group.icon.clone().map(|i| i.url.into())),
91       banner: Some(group.image.clone().map(|i| i.url.into())),
92       followers_url: Some(group.followers.clone().into()),
93       inbox_url: Some(group.inbox.clone().into()),
94       shared_inbox_url: Some(shared_inbox),
95     })
96   }
97 }