2 check_is_apub_id_valid,
3 context::lemmy_context,
4 fetcher::community::{fetch_community_outbox, update_community_mods},
5 generate_moderators_url,
7 objects::{create_tombstone, ImageObject, Source},
10 use activitystreams::{
11 actor::{kind::GroupType, Endpoints},
13 chrono::NaiveDateTime,
14 object::{kind::ImageType, Tombstone},
15 primitives::OneOrMany,
18 use chrono::{DateTime, FixedOffset};
19 use itertools::Itertools;
20 use lemmy_api_common::blocking;
22 signatures::PublicKey,
23 traits::{ActorType, ApubObject, FromApub, ToApub},
24 values::{MediaTypeHtml, MediaTypeMarkdown},
25 verify::verify_domains_match,
27 use lemmy_db_schema::{
29 source::community::{Community, CommunityForm},
32 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
34 settings::structs::Settings,
35 utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
38 use lemmy_websocket::LemmyContext;
40 use serde::{Deserialize, Serialize};
41 use serde_with::skip_serializing_none;
45 #[skip_serializing_none]
46 #[derive(Clone, Debug, Deserialize, Serialize)]
47 #[serde(rename_all = "camelCase")]
49 #[serde(rename = "@context")]
50 context: OneOrMany<AnyBase>,
51 #[serde(rename = "type")]
54 /// username, set at account creation and can never be changed
55 preferred_username: String,
56 /// title (can be changed at any time)
58 content: Option<String>,
59 media_type: Option<MediaTypeHtml>,
60 source: Option<Source>,
61 icon: Option<ImageObject>,
63 image: Option<ImageObject>,
65 sensitive: Option<bool>,
67 pub(crate) moderators: Option<Url>,
69 pub(crate) outbox: Url,
71 endpoints: Endpoints<Url>,
72 public_key: PublicKey,
73 published: Option<DateTime<FixedOffset>>,
74 updated: Option<DateTime<FixedOffset>>,
80 pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
81 verify_domains_match(&self.id, expected_domain)?;
84 pub(crate) async fn from_apub_to_form(
86 expected_domain: &Url,
88 ) -> Result<CommunityForm, LemmyError> {
89 let actor_id = Some(group.id(expected_domain)?.clone().into());
90 let name = group.preferred_username.clone();
91 let title = group.name.clone();
92 let description = group.source.clone().map(|s| s.content);
93 let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
95 let slur_regex = &settings.slur_regex();
96 check_slurs(&name, slur_regex)?;
97 check_slurs(&title, slur_regex)?;
98 check_slurs_opt(&description, slur_regex)?;
105 published: group.published.map(|u| u.naive_local()),
106 updated: group.updated.map(|u| u.naive_local()),
108 nsfw: Some(group.sensitive.unwrap_or(false)),
112 public_key: Some(group.public_key.public_key_pem.clone()),
113 last_refreshed_at: Some(naive_now()),
114 icon: Some(group.icon.clone().map(|i| i.url.into())),
115 banner: Some(group.image.clone().map(|i| i.url.into())),
116 followers_url: Some(group.followers.clone().into()),
117 inbox_url: Some(group.inbox.clone().into()),
118 shared_inbox_url: Some(shared_inbox),
123 #[derive(Clone, Debug)]
124 pub struct ApubCommunity(Community);
126 impl Deref for ApubCommunity {
127 type Target = Community;
128 fn deref(&self) -> &Self::Target {
133 impl From<Community> for ApubCommunity {
134 fn from(c: Community) -> Self {
135 ApubCommunity { 0: c }
139 #[async_trait::async_trait(?Send)]
140 impl ApubObject for ApubCommunity {
141 type DataType = LemmyContext;
143 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
144 Some(self.last_refreshed_at)
147 async fn read_from_apub_id(
149 context: &LemmyContext,
150 ) -> Result<Option<Self>, LemmyError> {
152 blocking(context.pool(), move |conn| {
153 Community::read_from_apub_id(conn, object_id)
160 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
161 blocking(context.pool(), move |conn| {
162 Community::update_deleted(conn, self.id, true)
169 impl ActorType for ApubCommunity {
170 fn is_local(&self) -> bool {
173 fn actor_id(&self) -> Url {
174 self.actor_id.to_owned().into()
176 fn name(&self) -> String {
179 fn public_key(&self) -> Option<String> {
180 self.public_key.to_owned()
182 fn private_key(&self) -> Option<String> {
183 self.private_key.to_owned()
186 fn inbox_url(&self) -> Url {
187 self.inbox_url.clone().into()
190 fn shared_inbox_url(&self) -> Option<Url> {
191 self.shared_inbox_url.clone().map(|s| s.into_inner())
195 #[async_trait::async_trait(?Send)]
196 impl ToApub for ApubCommunity {
197 type ApubType = Group;
198 type TombstoneType = Tombstone;
199 type DataType = DbPool;
201 async fn to_apub(&self, _pool: &DbPool) -> Result<Group, LemmyError> {
202 let source = self.description.clone().map(|bio| Source {
204 media_type: MediaTypeMarkdown::Markdown,
206 let icon = self.icon.clone().map(|url| ImageObject {
207 kind: ImageType::Image,
210 let image = self.banner.clone().map(|url| ImageObject {
211 kind: ImageType::Image,
216 context: lemmy_context(),
217 kind: GroupType::Group,
219 preferred_username: self.name.clone(),
220 name: self.title.clone(),
221 content: self.description.as_ref().map(|b| markdown_to_html(b)),
222 media_type: self.description.as_ref().map(|_| MediaTypeHtml::Html),
226 sensitive: Some(self.nsfw),
227 moderators: Some(generate_moderators_url(&self.actor_id)?.into()),
228 inbox: self.inbox_url.clone().into(),
229 outbox: generate_outbox_url(&self.actor_id)?.into(),
230 followers: self.followers_url.clone().into(),
231 endpoints: Endpoints {
232 shared_inbox: self.shared_inbox_url.clone().map(|s| s.into()),
235 public_key: self.get_public_key()?,
236 published: Some(convert_datetime(self.published)),
237 updated: self.updated.map(convert_datetime),
238 unparsed: Default::default(),
243 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
246 self.actor_id.to_owned().into(),
253 #[async_trait::async_trait(?Send)]
254 impl FromApub for ApubCommunity {
255 type ApubType = Group;
256 type DataType = LemmyContext;
258 /// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
261 context: &LemmyContext,
262 expected_domain: &Url,
263 request_counter: &mut i32,
264 ) -> Result<ApubCommunity, LemmyError> {
265 let form = Group::from_apub_to_form(group, expected_domain, &context.settings()).await?;
267 // Fetching mods and outbox is not necessary for Lemmy to work, so ignore errors. Besides,
268 // we need to ignore these errors so that tests can work entirely offline.
269 let community = blocking(context.pool(), move |conn| Community::upsert(conn, &form)).await??;
270 update_community_mods(group, &community, context, request_counter)
272 .map_err(|e| debug!("{}", e))
275 // TODO: doing this unconditionally might cause infinite loop for some reason
276 fetch_community_outbox(context, &group.outbox, request_counter)
278 .map_err(|e| debug!("{}", e))
285 #[async_trait::async_trait(?Send)]
286 impl CommunityType for Community {
287 fn followers_url(&self) -> Url {
288 self.followers_url.clone().into()
291 /// For a given community, returns the inboxes of all followers.
292 async fn get_follower_inboxes(
296 ) -> Result<Vec<Url>, LemmyError> {
299 let follows = blocking(pool, move |conn| {
300 CommunityFollowerView::for_community(conn, id)
303 let inboxes = follows
305 .filter(|f| !f.follower.local)
306 .map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
307 .map(|i| i.into_inner())
309 // Don't send to blocked instances
310 .filter(|inbox| check_is_apub_id_valid(inbox, false, settings).is_ok())
320 use crate::objects::tests::{file_to_json_object, init_context};
321 use assert_json_diff::assert_json_include;
322 use lemmy_db_schema::traits::Crud;
323 use serial_test::serial;
327 async fn test_fetch_lemmy_community() {
328 let context = init_context();
329 let mut json: Group = file_to_json_object("assets/lemmy-community.json");
330 let json_orig = json.clone();
331 // change these links so they dont fetch over the network
332 json.moderators = Some(Url::parse("https://lemmy.ml/c/announcements/not_moderators").unwrap());
333 json.outbox = Url::parse("https://lemmy.ml/c/announcements/not_outbox").unwrap();
335 let url = Url::parse("https://lemmy.ml/c/announcements").unwrap();
336 let mut request_counter = 0;
337 let community = ApubCommunity::from_apub(&json, &context, &url, &mut request_counter)
341 assert_eq!(community.actor_id.clone().into_inner(), url);
342 assert_eq!(community.title, "Announcements");
343 assert!(community.public_key.is_some());
344 assert!(!community.local);
345 assert_eq!(community.description.as_ref().unwrap().len(), 126);
346 // this makes two requests to the (intentionally) broken outbox/moderators collections
347 assert_eq!(request_counter, 2);
349 let to_apub = community.to_apub(context.pool()).await.unwrap();
350 assert_json_include!(actual: json_orig, expected: to_apub);
352 Community::delete(&*context.pool().get().unwrap(), community.id).unwrap();