]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/group.rs
Move object and collection structs to protocol folder
[lemmy.git] / crates / apub / src / protocol / objects / group.rs
1 use crate::{
2   collections::{
3     community_moderators::ApubCommunityModerators,
4     community_outbox::ApubCommunityOutbox,
5   },
6   fetcher::object_id::ObjectId,
7   objects::get_summary_from_string_or_source,
8   protocol::{ImageObject, Source},
9 };
10 use activitystreams::{
11   actor::{kind::GroupType, Endpoints},
12   unparsed::Unparsed,
13 };
14 use chrono::{DateTime, FixedOffset};
15 use lemmy_apub_lib::{signatures::PublicKey, verify::verify_domains_match};
16 use lemmy_db_schema::{naive_now, source::community::CommunityForm};
17 use lemmy_utils::{
18   settings::structs::Settings,
19   utils::{check_slurs, check_slurs_opt},
20   LemmyError,
21 };
22 use serde::{Deserialize, Serialize};
23 use serde_with::skip_serializing_none;
24 use url::Url;
25
26 #[skip_serializing_none]
27 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[serde(rename_all = "camelCase")]
29 pub struct Group {
30   #[serde(rename = "type")]
31   pub(crate) kind: GroupType,
32   pub(crate) id: Url,
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>,
40   /// banner
41   pub(crate) image: Option<ImageObject>,
42   // lemmy extension
43   pub(crate) sensitive: Option<bool>,
44   // lemmy extension
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>>,
53   #[serde(flatten)]
54   pub(crate) unparsed: Unparsed,
55 }
56
57 impl Group {
58   pub(crate) async fn from_apub_to_form(
59     group: &Group,
60     expected_domain: &Url,
61     settings: &Settings,
62   ) -> Result<CommunityForm, LemmyError> {
63     verify_domains_match(expected_domain, &group.id)?;
64     let name = group.preferred_username.clone();
65     let title = group.name.clone();
66     let description = get_summary_from_string_or_source(&group.summary, &group.source);
67     let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
68
69     let slur_regex = &settings.slur_regex();
70     check_slurs(&name, slur_regex)?;
71     check_slurs(&title, slur_regex)?;
72     check_slurs_opt(&description, slur_regex)?;
73
74     Ok(CommunityForm {
75       name,
76       title,
77       description,
78       removed: None,
79       published: group.published.map(|u| u.naive_local()),
80       updated: group.updated.map(|u| u.naive_local()),
81       deleted: None,
82       nsfw: Some(group.sensitive.unwrap_or(false)),
83       actor_id: Some(group.id.clone().into()),
84       local: Some(false),
85       private_key: None,
86       public_key: Some(group.public_key.public_key_pem.clone()),
87       last_refreshed_at: Some(naive_now()),
88       icon: Some(group.icon.clone().map(|i| i.url.into())),
89       banner: Some(group.image.clone().map(|i| i.url.into())),
90       followers_url: Some(group.followers.clone().into()),
91       inbox_url: Some(group.inbox.clone().into()),
92       shared_inbox_url: Some(shared_inbox),
93     })
94   }
95 }