2 check_apub_id_valid_with_strictness,
4 community_moderators::ApubCommunityModerators,
5 community_outbox::ApubCommunityOutbox,
8 objects::{community::ApubCommunity, read_from_string_or_source_opt},
10 objects::{Endpoints, LanguageTag},
15 use activitypub_federation::{
16 core::{object_id::ObjectId, signatures::PublicKey},
17 deser::helpers::deserialize_skip_error,
18 utils::verify_domains_match,
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::{
25 source::community::{CommunityInsertForm, CommunityUpdateForm},
30 utils::{check_slurs, check_slurs_opt},
32 use lemmy_websocket::LemmyContext;
33 use serde::{Deserialize, Serialize};
34 use serde_with::skip_serializing_none;
37 #[skip_serializing_none]
38 #[derive(Clone, Debug, Deserialize, Serialize)]
39 #[serde(rename_all = "camelCase")]
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,
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>,
57 pub(crate) image: Option<ImageObject>,
59 pub(crate) sensitive: Option<bool>,
61 pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
63 pub(crate) posting_restricted_to_mods: Option<bool>,
64 pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
65 pub(crate) endpoints: Option<Endpoints>,
67 pub(crate) language: Vec<LanguageTag>,
68 pub(crate) published: Option<DateTime<FixedOffset>>,
69 pub(crate) updated: Option<DateTime<FixedOffset>>,
73 pub(crate) async fn verify(
75 expected_domain: &Url,
76 context: &LemmyContext,
77 ) -> Result<(), LemmyError> {
78 let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
80 check_apub_id_valid_with_strictness(
86 verify_domains_match(expected_domain, self.id.inner())?;
88 let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
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)?;
97 pub(crate) fn into_insert_form(self, instance_id: InstanceId) -> 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),
103 published: self.published.map(|u| u.naive_local()),
104 updated: self.updated.map(|u| u.naive_local()),
106 nsfw: Some(self.sensitive.unwrap_or(false)),
107 actor_id: Some(self.id.into()),
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,
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(
132 published: self.published.map(|u| u.naive_local()),
133 updated: Some(self.updated.map(|u| u.naive_local())),
135 nsfw: Some(self.sensitive.unwrap_or(false)),
136 actor_id: Some(self.id.into()),
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,