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