2 check_is_apub_id_valid,
4 community_moderators::ApubCommunityModerators,
5 community_outbox::ApubCommunityOutbox,
7 objects::{community::ApubCommunity, read_from_string_or_source_opt},
8 protocol::{objects::Endpoints, ImageObject, Source},
10 use activitystreams_kinds::actor::GroupType;
11 use chrono::{DateTime, FixedOffset};
12 use lemmy_apub_lib::{object_id::ObjectId, signatures::PublicKey, verify::verify_domains_match};
13 use lemmy_db_schema::{source::community::CommunityForm, utils::naive_now};
15 utils::{check_slurs, check_slurs_opt},
18 use lemmy_websocket::LemmyContext;
19 use serde::{Deserialize, Serialize};
20 use serde_with::skip_serializing_none;
23 #[skip_serializing_none]
24 #[derive(Clone, Debug, Deserialize, Serialize)]
25 #[serde(rename_all = "camelCase")]
27 #[serde(rename = "type")]
28 pub(crate) kind: GroupType,
29 pub(crate) id: ObjectId<ApubCommunity>,
30 /// username, set at account creation and usually fixed after that
31 pub(crate) preferred_username: String,
32 pub(crate) inbox: Url,
33 pub(crate) followers: Url,
34 pub(crate) public_key: PublicKey,
37 pub(crate) name: Option<String>,
38 pub(crate) summary: Option<String>,
39 #[serde(deserialize_with = "crate::deserialize_skip_error", default)]
40 pub(crate) source: Option<Source>,
41 pub(crate) icon: Option<ImageObject>,
43 pub(crate) image: Option<ImageObject>,
45 pub(crate) sensitive: Option<bool>,
47 pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
49 pub(crate) posting_restricted_to_mods: Option<bool>,
50 pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
51 pub(crate) endpoints: Option<Endpoints>,
52 pub(crate) published: Option<DateTime<FixedOffset>>,
53 pub(crate) updated: Option<DateTime<FixedOffset>>,
57 pub(crate) async fn verify(
59 expected_domain: &Url,
60 context: &LemmyContext,
61 ) -> Result<(), LemmyError> {
62 check_is_apub_id_valid(self.id.inner(), true, &context.settings())?;
63 verify_domains_match(expected_domain, self.id.inner())?;
65 let slur_regex = &context.settings().slur_regex();
66 check_slurs(&self.preferred_username, slur_regex)?;
67 check_slurs_opt(&self.name, slur_regex)?;
68 let description = read_from_string_or_source_opt(&self.summary, &None, &self.source);
69 check_slurs_opt(&description, slur_regex)?;
73 pub(crate) fn into_form(self) -> CommunityForm {
75 name: self.preferred_username.clone(),
76 title: self.name.unwrap_or(self.preferred_username),
77 description: read_from_string_or_source_opt(&self.summary, &None, &self.source),
79 published: self.published.map(|u| u.naive_local()),
80 updated: self.updated.map(|u| u.naive_local()),
82 nsfw: Some(self.sensitive.unwrap_or(false)),
83 actor_id: Some(self.id.into()),
87 public_key: self.public_key.public_key_pem,
88 last_refreshed_at: Some(naive_now()),
89 icon: Some(self.icon.map(|i| i.url.into())),
90 banner: Some(self.image.map(|i| i.url.into())),
91 followers_url: Some(self.followers.into()),
92 inbox_url: Some(self.inbox.into()),
93 shared_inbox_url: Some(self.endpoints.map(|e| e.shared_inbox.into())),
94 posting_restricted_to_mods: self.posting_restricted_to_mods,