2 check_is_apub_id_valid,
4 community_moderators::ApubCommunityModerators,
5 community_outbox::ApubCommunityOutbox,
8 context::lemmy_context,
9 fetcher::object_id::ObjectId,
10 generate_moderators_url,
12 objects::{get_summary_from_string_or_source, tombstone::Tombstone, ImageObject, Source},
15 use activitystreams::{
16 actor::{kind::GroupType, Endpoints},
18 chrono::NaiveDateTime,
19 object::kind::ImageType,
20 primitives::OneOrMany,
23 use chrono::{DateTime, FixedOffset};
24 use itertools::Itertools;
25 use lemmy_api_common::blocking;
27 signatures::PublicKey,
28 traits::{ActorType, ApubObject},
29 values::MediaTypeMarkdown,
30 verify::verify_domains_match,
32 use lemmy_db_schema::{
34 source::community::{Community, CommunityForm},
37 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
39 settings::structs::Settings,
40 utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
43 use lemmy_websocket::LemmyContext;
45 use serde::{Deserialize, Serialize};
46 use serde_with::skip_serializing_none;
50 #[skip_serializing_none]
51 #[derive(Clone, Debug, Deserialize, Serialize)]
52 #[serde(rename_all = "camelCase")]
54 #[serde(rename = "@context")]
55 context: OneOrMany<AnyBase>,
56 #[serde(rename = "type")]
59 /// username, set at account creation and can never be changed
60 preferred_username: String,
61 /// title (can be changed at any time)
63 summary: Option<String>,
64 source: Option<Source>,
65 icon: Option<ImageObject>,
67 image: Option<ImageObject>,
69 sensitive: Option<bool>,
71 pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
73 pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
75 endpoints: Endpoints<Url>,
76 public_key: PublicKey,
77 published: Option<DateTime<FixedOffset>>,
78 updated: Option<DateTime<FixedOffset>>,
84 pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
85 verify_domains_match(&self.id, expected_domain)?;
88 pub(crate) async fn from_apub_to_form(
90 expected_domain: &Url,
92 ) -> Result<CommunityForm, LemmyError> {
93 let actor_id = Some(group.id(expected_domain)?.clone().into());
94 let name = group.preferred_username.clone();
95 let title = group.name.clone();
96 let description = get_summary_from_string_or_source(&group.summary, &group.source);
97 let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into());
99 let slur_regex = &settings.slur_regex();
100 check_slurs(&name, slur_regex)?;
101 check_slurs(&title, slur_regex)?;
102 check_slurs_opt(&description, slur_regex)?;
109 published: group.published.map(|u| u.naive_local()),
110 updated: group.updated.map(|u| u.naive_local()),
112 nsfw: Some(group.sensitive.unwrap_or(false)),
116 public_key: Some(group.public_key.public_key_pem.clone()),
117 last_refreshed_at: Some(naive_now()),
118 icon: Some(group.icon.clone().map(|i| i.url.into())),
119 banner: Some(group.image.clone().map(|i| i.url.into())),
120 followers_url: Some(group.followers.clone().into()),
121 inbox_url: Some(group.inbox.clone().into()),
122 shared_inbox_url: Some(shared_inbox),
127 #[derive(Clone, Debug)]
128 pub struct ApubCommunity(Community);
130 impl Deref for ApubCommunity {
131 type Target = Community;
132 fn deref(&self) -> &Self::Target {
137 impl From<Community> for ApubCommunity {
138 fn from(c: Community) -> Self {
139 ApubCommunity { 0: c }
143 #[async_trait::async_trait(?Send)]
144 impl ApubObject for ApubCommunity {
145 type DataType = LemmyContext;
146 type ApubType = Group;
147 type TombstoneType = Tombstone;
149 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
150 Some(self.last_refreshed_at)
153 async fn read_from_apub_id(
155 context: &LemmyContext,
156 ) -> Result<Option<Self>, LemmyError> {
158 blocking(context.pool(), move |conn| {
159 Community::read_from_apub_id(conn, object_id)
166 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
167 blocking(context.pool(), move |conn| {
168 Community::update_deleted(conn, self.id, true)
174 async fn to_apub(&self, _context: &LemmyContext) -> Result<Group, LemmyError> {
175 let source = self.description.clone().map(|bio| Source {
177 media_type: MediaTypeMarkdown::Markdown,
179 let icon = self.icon.clone().map(|url| ImageObject {
180 kind: ImageType::Image,
183 let image = self.banner.clone().map(|url| ImageObject {
184 kind: ImageType::Image,
189 context: lemmy_context(),
190 kind: GroupType::Group,
192 preferred_username: self.name.clone(),
193 name: self.title.clone(),
194 summary: self.description.as_ref().map(|b| markdown_to_html(b)),
198 sensitive: Some(self.nsfw),
199 moderators: Some(ObjectId::<ApubCommunityModerators>::new(
200 generate_moderators_url(&self.actor_id)?.into_inner(),
202 inbox: self.inbox_url.clone().into(),
203 outbox: ObjectId::new(generate_outbox_url(&self.actor_id)?),
204 followers: self.followers_url.clone().into(),
205 endpoints: Endpoints {
206 shared_inbox: self.shared_inbox_url.clone().map(|s| s.into()),
209 public_key: self.get_public_key()?,
210 published: Some(convert_datetime(self.published)),
211 updated: self.updated.map(convert_datetime),
212 unparsed: Default::default(),
217 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
220 self.updated.unwrap_or(self.published),
224 /// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
227 context: &LemmyContext,
228 expected_domain: &Url,
229 request_counter: &mut i32,
230 ) -> Result<ApubCommunity, LemmyError> {
231 let form = Group::from_apub_to_form(group, expected_domain, &context.settings()).await?;
233 // Fetching mods and outbox is not necessary for Lemmy to work, so ignore errors. Besides,
234 // we need to ignore these errors so that tests can work entirely offline.
235 let community: ApubCommunity =
236 blocking(context.pool(), move |conn| Community::upsert(conn, &form))
239 let outbox_data = CommunityContext(community.clone(), context.clone());
243 .dereference(&outbox_data, request_counter)
245 .map_err(|e| debug!("{}", e))
248 if let Some(moderators) = &group.moderators {
250 .dereference(&outbox_data, request_counter)
252 .map_err(|e| debug!("{}", e))
260 impl ActorType for ApubCommunity {
261 fn is_local(&self) -> bool {
264 fn actor_id(&self) -> Url {
265 self.actor_id.to_owned().into()
267 fn name(&self) -> String {
270 fn public_key(&self) -> Option<String> {
271 self.public_key.to_owned()
273 fn private_key(&self) -> Option<String> {
274 self.private_key.to_owned()
277 fn inbox_url(&self) -> Url {
278 self.inbox_url.clone().into()
281 fn shared_inbox_url(&self) -> Option<Url> {
282 self.shared_inbox_url.clone().map(|s| s.into_inner())
286 #[async_trait::async_trait(?Send)]
287 impl CommunityType for Community {
288 fn followers_url(&self) -> Url {
289 self.followers_url.clone().into()
292 /// For a given community, returns the inboxes of all followers.
293 async fn get_follower_inboxes(
297 ) -> Result<Vec<Url>, LemmyError> {
300 let follows = blocking(pool, move |conn| {
301 CommunityFollowerView::for_community(conn, id)
304 let inboxes = follows
306 .filter(|f| !f.follower.local)
307 .map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
308 .map(|i| i.into_inner())
310 // Don't send to blocked instances
311 .filter(|inbox| check_is_apub_id_valid(inbox, false, settings).is_ok())
321 use crate::objects::tests::{file_to_json_object, init_context};
322 use assert_json_diff::assert_json_include;
323 use lemmy_db_schema::traits::Crud;
324 use serial_test::serial;
328 async fn test_parse_lemmy_community() {
329 let context = init_context();
330 let mut json: Group = file_to_json_object("assets/lemmy-community.json");
331 let json_orig = json.clone();
332 // change these links so they dont fetch over the network
333 json.moderators = Some(ObjectId::new(
334 Url::parse("https://enterprise.lemmy.ml/c/tenforward/not_moderators").unwrap(),
337 ObjectId::new(Url::parse("https://enterprise.lemmy.ml/c/tenforward/not_outbox").unwrap());
339 let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
340 let mut request_counter = 0;
341 let community = ApubCommunity::from_apub(&json, &context, &url, &mut request_counter)
345 assert_eq!(community.actor_id.clone().into_inner(), url);
346 assert_eq!(community.title, "Ten Forward");
347 assert!(community.public_key.is_some());
348 assert!(!community.local);
349 assert_eq!(community.description.as_ref().unwrap().len(), 132);
350 // this makes two requests to the (intentionally) broken outbox/moderators collections
351 assert_eq!(request_counter, 2);
353 let to_apub = community.to_apub(&context).await.unwrap();
354 assert_json_include!(actual: json_orig, expected: to_apub);
356 Community::delete(&*context.pool().get().unwrap(), community.id).unwrap();