]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/group.rs
Fix problem where actors can have empty public key (fixes #2347) (#2348)
[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::{objects::Endpoints, ImageObject, Source},
9 };
10 use activitypub_federation::{
11   core::{object_id::ObjectId, signatures::PublicKey},
12   deser::helpers::deserialize_skip_error,
13   utils::verify_domains_match,
14 };
15 use activitystreams_kinds::actor::GroupType;
16 use chrono::{DateTime, FixedOffset};
17 use lemmy_db_schema::{source::community::CommunityForm, utils::naive_now};
18 use lemmy_utils::{
19   error::LemmyError,
20   utils::{check_slurs, check_slurs_opt},
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(deserialize_with = "deserialize_skip_error", default)]
44   pub(crate) source: Option<Source>,
45   pub(crate) icon: Option<ImageObject>,
46   /// banner
47   pub(crate) image: Option<ImageObject>,
48   // lemmy extension
49   pub(crate) sensitive: Option<bool>,
50   // lemmy extension
51   pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
52   // lemmy extension
53   pub(crate) posting_restricted_to_mods: Option<bool>,
54   pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
55   pub(crate) endpoints: Option<Endpoints>,
56   pub(crate) published: Option<DateTime<FixedOffset>>,
57   pub(crate) updated: Option<DateTime<FixedOffset>>,
58 }
59
60 impl Group {
61   pub(crate) async fn verify(
62     &self,
63     expected_domain: &Url,
64     context: &LemmyContext,
65   ) -> Result<(), LemmyError> {
66     check_apub_id_valid_with_strictness(self.id.inner(), true, context.settings())?;
67     verify_domains_match(expected_domain, self.id.inner())?;
68
69     let slur_regex = &context.settings().slur_regex();
70     check_slurs(&self.preferred_username, slur_regex)?;
71     check_slurs_opt(&self.name, slur_regex)?;
72     let description = read_from_string_or_source_opt(&self.summary, &None, &self.source);
73     check_slurs_opt(&description, slur_regex)?;
74     Ok(())
75   }
76
77   pub(crate) fn into_form(self) -> CommunityForm {
78     CommunityForm {
79       name: self.preferred_username.clone(),
80       title: self.name.unwrap_or(self.preferred_username),
81       description: read_from_string_or_source_opt(&self.summary, &None, &self.source),
82       removed: None,
83       published: self.published.map(|u| u.naive_local()),
84       updated: self.updated.map(|u| u.naive_local()),
85       deleted: None,
86       nsfw: Some(self.sensitive.unwrap_or(false)),
87       actor_id: Some(self.id.into()),
88       local: Some(false),
89       private_key: None,
90       hidden: Some(false),
91       public_key: Some(self.public_key.public_key_pem),
92       last_refreshed_at: Some(naive_now()),
93       icon: Some(self.icon.map(|i| i.url.into())),
94       banner: Some(self.image.map(|i| i.url.into())),
95       followers_url: Some(self.followers.into()),
96       inbox_url: Some(self.inbox.into()),
97       shared_inbox_url: Some(self.endpoints.map(|e| e.shared_inbox.into())),
98       posting_restricted_to_mods: self.posting_restricted_to_mods,
99     }
100   }
101 }