From d6bd072ea18253aa7d4dd3531e8126db414a5de1 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 22 Mar 2021 15:08:06 +0100 Subject: [PATCH] Make federation compatible with Lemmy v0.9.9 --- crates/apub/src/objects/comment.rs | 7 +++++- crates/apub/src/objects/community.rs | 37 +++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index 536190f0..9ed74658 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -26,6 +26,7 @@ use lemmy_db_queries::{Crud, DbPool}; use lemmy_db_schema::{ source::{ comment::{Comment, CommentForm}, + community::Community, person::Person, post::Post, }, @@ -52,6 +53,9 @@ impl ToApub for Comment { let post_id = self.post_id; let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??; + let community_id = post.community_id; + let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; + // Add a vector containing some important info to the "in_reply_to" field // [post_ap_id, Option(parent_comment_ap_id)] let mut in_reply_to_vec = vec![post.ap_id.into_inner()]; @@ -67,7 +71,8 @@ impl ToApub for Comment { .set_many_contexts(lemmy_context()?) .set_id(self.ap_id.to_owned().into_inner()) .set_published(convert_datetime(self.published)) - .set_to(public()) + // NOTE: included community id for compatibility with lemmy v0.9.9 + .set_many_tos(vec![community.actor_id.into_inner(), public()]) .set_many_in_reply_tos(in_reply_to_vec) .set_attributed_to(creator.actor_id.into_inner()); diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index a20af20d..d7e42c4a 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -23,11 +23,13 @@ use activitystreams::{ }; use activitystreams_ext::Ext2; use anyhow::Context; +use lemmy_api_structs::blocking; use lemmy_db_queries::DbPool; use lemmy_db_schema::{ naive_now, source::community::{Community, CommunityForm}, }; +use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView; use lemmy_utils::{ location_info, utils::{check_slurs, check_slurs_opt, convert_datetime}, @@ -40,13 +42,25 @@ use url::Url; impl ToApub for Community { type ApubType = GroupExt; - async fn to_apub(&self, _pool: &DbPool) -> Result { + async fn to_apub(&self, pool: &DbPool) -> Result { + let id = self.id; + let moderators = blocking(pool, move |conn| { + CommunityModeratorView::for_community(&conn, id) + }) + .await??; + let moderators: Vec = moderators + .into_iter() + .map(|m| m.moderator.actor_id.into_inner()) + .collect(); + let mut group = ApObject::new(Group::new()); group .set_many_contexts(lemmy_context()?) .set_id(self.actor_id.to_owned().into()) .set_name(self.title.to_owned()) - .set_published(convert_datetime(self.published)); + .set_published(convert_datetime(self.published)) + // NOTE: included attritubed_to field for compatibility with lemmy v0.9.9 + .set_many_attributed_tos(moderators); if let Some(u) = self.updated.to_owned() { group.set_updated(convert_datetime(u)); @@ -127,9 +141,24 @@ impl FromApubToForm for CommunityForm { _mod_action_allowed: bool, ) -> Result { let moderator_uris = fetch_community_mods(context, group, request_counter).await?; - let creator_uri = moderator_uris.first().context(location_info!())?; + let creator = if let Some(creator_uri) = moderator_uris.first() { + get_or_fetch_and_upsert_person(creator_uri, context, request_counter) + } else { + // NOTE: code for compatibility with lemmy v0.9.9 + let creator_uri = group + .inner + .attributed_to() + .map(|a| a.as_many()) + .flatten() + .map(|a| a.first()) + .flatten() + .map(|a| a.as_xsd_any_uri()) + .flatten() + .context(location_info!())?; + get_or_fetch_and_upsert_person(creator_uri, context, request_counter) + } + .await?; - let creator = get_or_fetch_and_upsert_person(creator_uri, context, request_counter).await?; let name = group .inner .preferred_username() -- 2.44.1