2 check_apub_id_valid_with_strictness,
4 community_moderators::ApubCommunityModerators,
5 community_outbox::ApubCommunityOutbox,
7 objects::{community::ApubCommunity, read_from_string_or_source_opt},
9 objects::{Endpoints, LanguageTag},
14 use activitypub_federation::{
15 core::{object_id::ObjectId, signatures::PublicKey},
16 deser::helpers::deserialize_skip_error,
17 utils::verify_domains_match,
19 use activitystreams_kinds::actor::GroupType;
20 use chrono::{DateTime, FixedOffset};
21 use lemmy_db_schema::{source::community::CommunityForm, utils::naive_now};
24 utils::{check_slurs, check_slurs_opt},
26 use lemmy_websocket::LemmyContext;
27 use serde::{Deserialize, Serialize};
28 use serde_with::skip_serializing_none;
31 #[skip_serializing_none]
32 #[derive(Clone, Debug, Deserialize, Serialize)]
33 #[serde(rename_all = "camelCase")]
35 #[serde(rename = "type")]
36 pub(crate) kind: GroupType,
37 pub(crate) id: ObjectId<ApubCommunity>,
38 /// username, set at account creation and usually fixed after that
39 pub(crate) preferred_username: String,
40 pub(crate) inbox: Url,
41 pub(crate) followers: Url,
42 pub(crate) public_key: PublicKey,
45 pub(crate) name: Option<String>,
46 pub(crate) summary: Option<String>,
47 #[serde(deserialize_with = "deserialize_skip_error", default)]
48 pub(crate) source: Option<Source>,
49 pub(crate) icon: Option<ImageObject>,
51 pub(crate) image: Option<ImageObject>,
53 pub(crate) sensitive: Option<bool>,
55 pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
57 pub(crate) posting_restricted_to_mods: Option<bool>,
58 pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
59 pub(crate) endpoints: Option<Endpoints>,
61 pub(crate) language: Vec<LanguageTag>,
62 pub(crate) published: Option<DateTime<FixedOffset>>,
63 pub(crate) updated: Option<DateTime<FixedOffset>>,
67 pub(crate) async fn verify(
69 expected_domain: &Url,
70 context: &LemmyContext,
71 ) -> Result<(), LemmyError> {
72 check_apub_id_valid_with_strictness(self.id.inner(), true, context.settings())?;
73 verify_domains_match(expected_domain, self.id.inner())?;
75 let slur_regex = &context.settings().slur_regex();
76 check_slurs(&self.preferred_username, slur_regex)?;
77 check_slurs_opt(&self.name, slur_regex)?;
78 let description = read_from_string_or_source_opt(&self.summary, &None, &self.source);
79 check_slurs_opt(&description, slur_regex)?;
83 pub(crate) fn into_form(self) -> CommunityForm {
85 name: self.preferred_username.clone(),
86 title: self.name.unwrap_or(self.preferred_username),
87 description: Some(read_from_string_or_source_opt(
93 published: self.published.map(|u| u.naive_local()),
94 updated: self.updated.map(|u| u.naive_local()),
96 nsfw: Some(self.sensitive.unwrap_or(false)),
97 actor_id: Some(self.id.into()),
101 public_key: Some(self.public_key.public_key_pem),
102 last_refreshed_at: Some(naive_now()),
103 icon: Some(self.icon.map(|i| i.url.into())),
104 banner: Some(self.image.map(|i| i.url.into())),
105 followers_url: Some(self.followers.into()),
106 inbox_url: Some(self.inbox.into()),
107 shared_inbox_url: Some(self.endpoints.map(|e| e.shared_inbox.into())),
108 posting_restricted_to_mods: self.posting_restricted_to_mods,