2 extensions::{context::lemmy_context, signatures::PublicKey},
3 fetcher::community::{fetch_community_outbox, update_community_mods},
4 generate_moderators_url,
5 objects::{create_tombstone, FromApub, ImageObject, Source, ToApub},
9 actor::{kind::GroupType, Endpoints},
11 object::{kind::ImageType, Tombstone},
12 primitives::OneOrMany,
15 use chrono::{DateTime, FixedOffset};
16 use lemmy_api_common::blocking;
18 values::{MediaTypeHtml, MediaTypeMarkdown},
21 use lemmy_db_queries::{source::community::Community_, DbPool};
22 use lemmy_db_schema::{
24 source::community::{Community, CommunityForm},
27 settings::structs::Settings,
28 utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
31 use lemmy_websocket::LemmyContext;
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 = "@context")]
41 context: OneOrMany<AnyBase>,
42 #[serde(rename = "type")]
45 /// username, set at account creation and can never be changed
46 preferred_username: String,
47 /// title (can be changed at any time)
49 content: Option<String>,
50 media_type: Option<MediaTypeHtml>,
51 source: Option<Source>,
52 icon: Option<ImageObject>,
54 image: Option<ImageObject>,
56 sensitive: Option<bool>,
58 pub(crate) moderators: Option<Url>,
60 pub(crate) outbox: Url,
62 endpoints: Endpoints<Url>,
63 public_key: PublicKey,
64 published: DateTime<FixedOffset>,
65 updated: Option<DateTime<FixedOffset>>,
71 pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
72 verify_domains_match(&self.id, expected_domain)?;
75 pub(crate) async fn from_apub_to_form(
77 expected_domain: &Url,
79 ) -> Result<CommunityForm, LemmyError> {
80 let actor_id = Some(group.id(expected_domain)?.clone().into());
81 let name = group.preferred_username.clone();
82 let title = group.name.clone();
83 let description = group.source.clone().map(|s| s.content);
84 let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
86 let slur_regex = &settings.slur_regex();
87 check_slurs(&name, slur_regex)?;
88 check_slurs(&title, slur_regex)?;
89 check_slurs_opt(&description, slur_regex)?;
96 published: Some(group.published.naive_local()),
97 updated: group.updated.map(|u| u.naive_local()),
99 nsfw: Some(group.sensitive.unwrap_or(false)),
103 public_key: Some(group.public_key.public_key_pem.clone()),
104 last_refreshed_at: Some(naive_now()),
105 icon: Some(group.icon.clone().map(|i| i.url.into())),
106 banner: Some(group.image.clone().map(|i| i.url.into())),
107 followers_url: Some(group.followers.clone().into()),
108 inbox_url: Some(group.inbox.clone().into()),
109 shared_inbox_url: Some(shared_inbox),
114 #[async_trait::async_trait(?Send)]
115 impl ToApub for Community {
116 type ApubType = Group;
118 async fn to_apub(&self, _pool: &DbPool) -> Result<Group, LemmyError> {
119 let source = self.description.clone().map(|bio| Source {
121 media_type: MediaTypeMarkdown::Markdown,
123 let icon = self.icon.clone().map(|url| ImageObject {
124 kind: ImageType::Image,
127 let image = self.banner.clone().map(|url| ImageObject {
128 kind: ImageType::Image,
133 context: lemmy_context(),
134 kind: GroupType::Group,
136 preferred_username: self.name.clone(),
137 name: self.title.clone(),
138 content: self.description.as_ref().map(|b| markdown_to_html(b)),
139 media_type: self.description.as_ref().map(|_| MediaTypeHtml::Html),
143 sensitive: Some(self.nsfw),
144 moderators: Some(generate_moderators_url(&self.actor_id)?.into()),
145 inbox: self.inbox_url.clone().into(),
146 outbox: self.get_outbox_url()?,
147 followers: self.followers_url.clone().into(),
148 endpoints: Endpoints {
149 shared_inbox: self.shared_inbox_url.clone().map(|s| s.into()),
152 public_key: self.get_public_key()?,
153 published: convert_datetime(self.published),
154 updated: self.updated.map(convert_datetime),
155 unparsed: Default::default(),
160 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
163 self.actor_id.to_owned().into(),
170 #[async_trait::async_trait(?Send)]
171 impl FromApub for Community {
172 type ApubType = Group;
174 /// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
177 context: &LemmyContext,
178 expected_domain: &Url,
179 request_counter: &mut i32,
180 ) -> Result<Community, LemmyError> {
181 let form = Group::from_apub_to_form(group, expected_domain, &context.settings()).await?;
183 let community = blocking(context.pool(), move |conn| Community::upsert(conn, &form)).await??;
184 update_community_mods(group, &community, context, request_counter).await?;
186 // TODO: doing this unconditionally might cause infinite loop for some reason
187 fetch_community_outbox(context, &group.outbox, request_counter).await?;