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