2 check_is_apub_id_valid,
4 community_moderators::ApubCommunityModerators,
5 community_outbox::ApubCommunityOutbox,
7 objects::{community::ApubCommunity, get_summary_from_string_or_source},
8 protocol::{ImageObject, Source},
10 use activitystreams::{
11 actor::{kind::GroupType, Endpoints},
14 use chrono::{DateTime, FixedOffset};
15 use lemmy_apub_lib::{object_id::ObjectId, signatures::PublicKey, verify::verify_domains_match};
16 use lemmy_db_schema::{naive_now, source::community::CommunityForm};
18 settings::structs::Settings,
19 utils::{check_slurs, check_slurs_opt},
22 use serde::{Deserialize, Serialize};
23 use serde_with::skip_serializing_none;
26 #[skip_serializing_none]
27 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[serde(rename_all = "camelCase")]
30 #[serde(rename = "type")]
31 pub(crate) kind: GroupType,
32 pub(crate) id: ObjectId<ApubCommunity>,
33 /// username, set at account creation and can never be changed
34 pub(crate) preferred_username: String,
35 /// title (can be changed at any time)
36 pub(crate) name: String,
37 pub(crate) summary: Option<String>,
38 pub(crate) source: Option<Source>,
39 pub(crate) icon: Option<ImageObject>,
41 pub(crate) image: Option<ImageObject>,
43 pub(crate) sensitive: Option<bool>,
45 pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
46 pub(crate) inbox: Url,
47 pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
48 pub(crate) followers: Url,
49 pub(crate) endpoints: Endpoints<Url>,
50 pub(crate) public_key: PublicKey,
51 pub(crate) published: Option<DateTime<FixedOffset>>,
52 pub(crate) updated: Option<DateTime<FixedOffset>>,
54 pub(crate) unparsed: Unparsed,
58 pub(crate) async fn into_form(
60 expected_domain: &Url,
62 ) -> Result<CommunityForm, LemmyError> {
63 check_is_apub_id_valid(self.id.inner(), true, settings)?;
64 verify_domains_match(expected_domain, self.id.inner())?;
65 let name = self.preferred_username;
66 let title = self.name;
67 let description = get_summary_from_string_or_source(&self.summary, &self.source);
68 let shared_inbox = self.endpoints.shared_inbox.map(|s| s.into());
70 let slur_regex = &settings.slur_regex();
71 check_slurs(&name, slur_regex)?;
72 check_slurs(&title, slur_regex)?;
73 check_slurs_opt(&description, slur_regex)?;
75 // TODO: test_parse_lemmy_community_moderators() keeps failing here with stack overflow
81 published: self.published.map(|u| u.naive_local()),
82 updated: self.updated.map(|u| u.naive_local()),
84 nsfw: Some(self.sensitive.unwrap_or(false)),
85 actor_id: Some(self.id.into()),
88 public_key: Some(self.public_key.public_key_pem),
89 last_refreshed_at: Some(naive_now()),
90 icon: Some(self.icon.map(|i| i.url.into())),
91 banner: Some(self.image.map(|i| i.url.into())),
92 followers_url: Some(self.followers.into()),
93 inbox_url: Some(self.inbox.into()),
94 shared_inbox_url: Some(shared_inbox),