]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/group.rs
Merge pull request #2593 from LemmyNet/refactor-notifications
[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   fetch_local_site_data,
8   objects::{community::ApubCommunity, read_from_string_or_source_opt},
9   protocol::{
10     objects::{Endpoints, LanguageTag},
11     ImageObject,
12     Source,
13   },
14 };
15 use activitypub_federation::{
16   core::{object_id::ObjectId, signatures::PublicKey},
17   deser::helpers::deserialize_skip_error,
18   utils::verify_domains_match,
19 };
20 use activitystreams_kinds::actor::GroupType;
21 use chrono::{DateTime, FixedOffset};
22 use lemmy_api_common::{context::LemmyContext, utils::local_site_opt_to_slur_regex};
23 use lemmy_db_schema::{
24   newtypes::InstanceId,
25   source::community::{CommunityInsertForm, CommunityUpdateForm},
26   utils::naive_now,
27 };
28 use lemmy_utils::{
29   error::LemmyError,
30   utils::{check_slurs, check_slurs_opt},
31 };
32 use serde::{Deserialize, Serialize};
33 use serde_with::skip_serializing_none;
34 use url::Url;
35
36 #[skip_serializing_none]
37 #[derive(Clone, Debug, Deserialize, Serialize)]
38 #[serde(rename_all = "camelCase")]
39 pub struct Group {
40   #[serde(rename = "type")]
41   pub(crate) kind: GroupType,
42   pub(crate) id: ObjectId<ApubCommunity>,
43   /// username, set at account creation and usually fixed after that
44   pub(crate) preferred_username: String,
45   pub(crate) inbox: Url,
46   pub(crate) followers: Url,
47   pub(crate) public_key: PublicKey,
48
49   /// title
50   pub(crate) name: Option<String>,
51   pub(crate) summary: Option<String>,
52   #[serde(deserialize_with = "deserialize_skip_error", default)]
53   pub(crate) source: Option<Source>,
54   pub(crate) icon: Option<ImageObject>,
55   /// banner
56   pub(crate) image: Option<ImageObject>,
57   // lemmy extension
58   pub(crate) sensitive: Option<bool>,
59   // deprecated, use attributed_to instead
60   pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
61   #[serde(deserialize_with = "deserialize_skip_error", default)]
62   pub(crate) attributed_to: Option<ObjectId<ApubCommunityModerators>>,
63   // lemmy extension
64   pub(crate) posting_restricted_to_mods: Option<bool>,
65   pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
66   pub(crate) endpoints: Option<Endpoints>,
67   #[serde(default)]
68   pub(crate) language: Vec<LanguageTag>,
69   pub(crate) published: Option<DateTime<FixedOffset>>,
70   pub(crate) updated: Option<DateTime<FixedOffset>>,
71 }
72
73 impl Group {
74   pub(crate) async fn verify(
75     &self,
76     expected_domain: &Url,
77     context: &LemmyContext,
78   ) -> Result<(), LemmyError> {
79     let local_site_data = fetch_local_site_data(context.pool()).await?;
80
81     check_apub_id_valid_with_strictness(
82       self.id.inner(),
83       true,
84       &local_site_data,
85       context.settings(),
86     )?;
87     verify_domains_match(expected_domain, self.id.inner())?;
88
89     let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
90
91     check_slurs(&self.preferred_username, slur_regex)?;
92     check_slurs_opt(&self.name, slur_regex)?;
93     let description = read_from_string_or_source_opt(&self.summary, &None, &self.source);
94     check_slurs_opt(&description, slur_regex)?;
95     Ok(())
96   }
97
98   pub(crate) fn into_insert_form(self, instance_id: InstanceId) -> CommunityInsertForm {
99     CommunityInsertForm {
100       name: self.preferred_username.clone(),
101       title: self.name.unwrap_or(self.preferred_username),
102       description: read_from_string_or_source_opt(&self.summary, &None, &self.source),
103       removed: None,
104       published: self.published.map(|u| u.naive_local()),
105       updated: self.updated.map(|u| u.naive_local()),
106       deleted: Some(false),
107       nsfw: Some(self.sensitive.unwrap_or(false)),
108       actor_id: Some(self.id.into()),
109       local: Some(false),
110       private_key: None,
111       hidden: Some(false),
112       public_key: self.public_key.public_key_pem,
113       last_refreshed_at: Some(naive_now()),
114       icon: self.icon.map(|i| i.url.into()),
115       banner: self.image.map(|i| i.url.into()),
116       followers_url: Some(self.followers.into()),
117       inbox_url: Some(self.inbox.into()),
118       shared_inbox_url: self.endpoints.map(|e| e.shared_inbox.into()),
119       posting_restricted_to_mods: self.posting_restricted_to_mods,
120       instance_id,
121     }
122   }
123
124   pub(crate) fn into_update_form(self) -> CommunityUpdateForm {
125     CommunityUpdateForm {
126       title: Some(self.name.unwrap_or(self.preferred_username)),
127       description: Some(read_from_string_or_source_opt(
128         &self.summary,
129         &None,
130         &self.source,
131       )),
132       removed: None,
133       published: self.published.map(|u| u.naive_local()),
134       updated: Some(self.updated.map(|u| u.naive_local())),
135       deleted: None,
136       nsfw: Some(self.sensitive.unwrap_or(false)),
137       actor_id: Some(self.id.into()),
138       local: Some(false),
139       private_key: None,
140       hidden: Some(false),
141       public_key: Some(self.public_key.public_key_pem),
142       last_refreshed_at: Some(naive_now()),
143       icon: Some(self.icon.map(|i| i.url.into())),
144       banner: Some(self.image.map(|i| i.url.into())),
145       followers_url: Some(self.followers.into()),
146       inbox_url: Some(self.inbox.into()),
147       shared_inbox_url: Some(self.endpoints.map(|e| e.shared_inbox.into())),
148       posting_restricted_to_mods: self.posting_restricted_to_mods,
149     }
150   }
151 }