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