]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/group.rs
f6c8f517f98c67d2678c05761f684b6325d902c3
[lemmy.git] / crates / apub / src / protocol / objects / group.rs
1 use crate::{
2   check_apub_id_valid_with_strictness,
3   collections::{
4     community_moderators::ApubCommunityModerators,
5     community_outbox::ApubCommunityOutbox,
6   },
7   objects::{community::ApubCommunity, read_from_string_or_source_opt},
8   protocol::{
9     objects::{Endpoints, LanguageTag},
10     ImageObject,
11     Source,
12   },
13 };
14 use activitypub_federation::{
15   core::{object_id::ObjectId, signatures::PublicKey},
16   deser::helpers::deserialize_skip_error,
17   utils::verify_domains_match,
18 };
19 use activitystreams_kinds::actor::GroupType;
20 use chrono::{DateTime, FixedOffset};
21 use lemmy_db_schema::{source::community::CommunityForm, utils::naive_now};
22 use lemmy_utils::{
23   error::LemmyError,
24   utils::{check_slurs, check_slurs_opt},
25 };
26 use lemmy_websocket::LemmyContext;
27 use serde::{Deserialize, Serialize};
28 use serde_with::skip_serializing_none;
29 use url::Url;
30
31 #[skip_serializing_none]
32 #[derive(Clone, Debug, Deserialize, Serialize)]
33 #[serde(rename_all = "camelCase")]
34 pub struct Group {
35   #[serde(rename = "type")]
36   pub(crate) kind: GroupType,
37   pub(crate) id: ObjectId<ApubCommunity>,
38   /// username, set at account creation and usually fixed after that
39   pub(crate) preferred_username: String,
40   pub(crate) inbox: Url,
41   pub(crate) followers: Url,
42   pub(crate) public_key: PublicKey,
43
44   /// title
45   pub(crate) name: Option<String>,
46   pub(crate) summary: Option<String>,
47   #[serde(deserialize_with = "deserialize_skip_error", default)]
48   pub(crate) source: Option<Source>,
49   pub(crate) icon: Option<ImageObject>,
50   /// banner
51   pub(crate) image: Option<ImageObject>,
52   // lemmy extension
53   pub(crate) sensitive: Option<bool>,
54   // lemmy extension
55   pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
56   // lemmy extension
57   pub(crate) posting_restricted_to_mods: Option<bool>,
58   pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
59   pub(crate) endpoints: Option<Endpoints>,
60   #[serde(default)]
61   pub(crate) language: Vec<LanguageTag>,
62   pub(crate) published: Option<DateTime<FixedOffset>>,
63   pub(crate) updated: Option<DateTime<FixedOffset>>,
64 }
65
66 impl Group {
67   pub(crate) async fn verify(
68     &self,
69     expected_domain: &Url,
70     context: &LemmyContext,
71   ) -> Result<(), LemmyError> {
72     check_apub_id_valid_with_strictness(self.id.inner(), true, context.settings())?;
73     verify_domains_match(expected_domain, self.id.inner())?;
74
75     let slur_regex = &context.settings().slur_regex();
76     check_slurs(&self.preferred_username, slur_regex)?;
77     check_slurs_opt(&self.name, slur_regex)?;
78     let description = read_from_string_or_source_opt(&self.summary, &None, &self.source);
79     check_slurs_opt(&description, slur_regex)?;
80     Ok(())
81   }
82
83   pub(crate) fn into_form(self) -> CommunityForm {
84     CommunityForm {
85       name: self.preferred_username.clone(),
86       title: self.name.unwrap_or(self.preferred_username),
87       description: Some(read_from_string_or_source_opt(
88         &self.summary,
89         &None,
90         &self.source,
91       )),
92       removed: None,
93       published: self.published.map(|u| u.naive_local()),
94       updated: self.updated.map(|u| u.naive_local()),
95       deleted: None,
96       nsfw: Some(self.sensitive.unwrap_or(false)),
97       actor_id: Some(self.id.into()),
98       local: Some(false),
99       private_key: None,
100       hidden: Some(false),
101       public_key: Some(self.public_key.public_key_pem),
102       last_refreshed_at: Some(naive_now()),
103       icon: Some(self.icon.map(|i| i.url.into())),
104       banner: Some(self.image.map(|i| i.url.into())),
105       followers_url: Some(self.followers.into()),
106       inbox_url: Some(self.inbox.into()),
107       shared_inbox_url: Some(self.endpoints.map(|e| e.shared_inbox.into())),
108       posting_restricted_to_mods: self.posting_restricted_to_mods,
109     }
110   }
111 }