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::{context::LemmyContext, utils::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 serde::{Deserialize, Serialize};
33 use serde_with::skip_serializing_none;
36 #[skip_serializing_none]
37 #[derive(Clone, Debug, Deserialize, Serialize)]
38 #[serde(rename_all = "camelCase")]
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,
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>,
56 pub(crate) image: Option<ImageObject>,
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>>,
64 pub(crate) posting_restricted_to_mods: Option<bool>,
65 pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
66 pub(crate) endpoints: Option<Endpoints>,
68 pub(crate) language: Vec<LanguageTag>,
69 pub(crate) published: Option<DateTime<FixedOffset>>,
70 pub(crate) updated: Option<DateTime<FixedOffset>>,
74 pub(crate) async fn verify(
76 expected_domain: &Url,
77 context: &LemmyContext,
78 ) -> Result<(), LemmyError> {
79 let local_site_data = fetch_local_site_data(context.pool()).await?;
81 check_apub_id_valid_with_strictness(
87 verify_domains_match(expected_domain, self.id.inner())?;
89 let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
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)?;
98 pub(crate) fn into_insert_form(self, instance_id: InstanceId) -> 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),
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()),
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,
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(
133 published: self.published.map(|u| u.naive_local()),
134 updated: Some(self.updated.map(|u| u.naive_local())),
136 nsfw: Some(self.sensitive.unwrap_or(false)),
137 actor_id: Some(self.id.into()),
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,