2 extensions::{context::lemmy_context, signatures::PublicKey},
3 fetcher::community::fetch_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::{ApubObject, DbPool};
22 use lemmy_db_schema::{
24 source::community::{Community, CommunityForm},
27 utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
30 use lemmy_websocket::LemmyContext;
31 use serde::{Deserialize, Serialize};
32 use serde_with::skip_serializing_none;
35 #[skip_serializing_none]
36 #[derive(Clone, Debug, Deserialize, Serialize)]
37 #[serde(rename_all = "camelCase")]
39 #[serde(rename = "@context")]
40 context: OneOrMany<AnyBase>,
41 #[serde(rename = "type")]
44 /// username, set at account creation and can never be changed
45 preferred_username: String,
46 /// title (can be changed at any time)
48 content: Option<String>,
49 media_type: Option<MediaTypeHtml>,
50 source: Option<Source>,
51 icon: Option<ImageObject>,
53 image: Option<ImageObject>,
55 sensitive: Option<bool>,
57 pub(crate) moderators: Option<Url>,
59 pub(crate) outbox: Url,
61 endpoints: Endpoints<Url>,
62 public_key: PublicKey,
63 published: DateTime<FixedOffset>,
64 updated: Option<DateTime<FixedOffset>>,
70 pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
71 verify_domains_match(&self.id, expected_domain)?;
74 pub(crate) async fn from_apub_to_form(
76 expected_domain: &Url,
77 ) -> Result<CommunityForm, LemmyError> {
78 let actor_id = Some(group.id(expected_domain)?.clone().into());
79 let name = group.preferred_username.clone();
80 let title = group.name.clone();
81 let description = group.source.clone().map(|s| s.content);
82 let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
86 check_slurs_opt(&description)?;
93 published: Some(group.published.naive_local()),
94 updated: group.updated.map(|u| u.naive_local()),
96 nsfw: Some(group.sensitive.unwrap_or(false)),
100 public_key: Some(group.public_key.public_key_pem.clone()),
101 last_refreshed_at: Some(naive_now()),
102 icon: Some(group.icon.clone().map(|i| i.url.into())),
103 banner: Some(group.image.clone().map(|i| i.url.into())),
104 followers_url: Some(group.followers.clone().into()),
105 inbox_url: Some(group.inbox.clone().into()),
106 shared_inbox_url: Some(shared_inbox),
111 #[async_trait::async_trait(?Send)]
112 impl ToApub for Community {
113 type ApubType = Group;
115 async fn to_apub(&self, _pool: &DbPool) -> Result<Group, LemmyError> {
116 let source = self.description.clone().map(|bio| Source {
118 media_type: MediaTypeMarkdown::Markdown,
120 let icon = self.icon.clone().map(|url| ImageObject {
121 kind: ImageType::Image,
124 let image = self.banner.clone().map(|url| ImageObject {
125 kind: ImageType::Image,
130 context: lemmy_context(),
131 kind: GroupType::Group,
133 preferred_username: self.name.clone(),
134 name: self.title.clone(),
135 content: self.description.as_ref().map(|b| markdown_to_html(b)),
136 media_type: self.description.as_ref().map(|_| MediaTypeHtml::Html),
140 sensitive: Some(self.nsfw),
141 moderators: Some(generate_moderators_url(&self.actor_id)?.into()),
142 inbox: self.inbox_url.clone().into(),
143 outbox: self.get_outbox_url()?,
144 followers: self.followers_url.clone().into(),
145 endpoints: Endpoints {
146 shared_inbox: self.shared_inbox_url.clone().map(|s| s.into()),
149 public_key: self.get_public_key()?,
150 published: convert_datetime(self.published),
151 updated: self.updated.map(convert_datetime),
152 unparsed: Default::default(),
157 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
160 self.actor_id.to_owned().into(),
167 #[async_trait::async_trait(?Send)]
168 impl FromApub for Community {
169 type ApubType = Group;
171 /// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
174 context: &LemmyContext,
175 expected_domain: &Url,
176 request_counter: &mut i32,
177 ) -> Result<Community, LemmyError> {
178 fetch_community_mods(context, group, request_counter).await?;
179 let form = Group::from_apub_to_form(group, expected_domain).await?;
181 let community = blocking(context.pool(), move |conn| Community::upsert(conn, &form)).await??;