From 020b9b8cdd61fa5a5b21b59ba25fbce2a6cf96cc Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 15 May 2020 20:23:20 -0400 Subject: [PATCH 1/2] Post thumbnail and user icons federating. --- server/src/apub/fetcher.rs | 3 +++ server/src/apub/mod.rs | 2 +- server/src/apub/post.rs | 26 +++++++++++++++++++++++++- server/src/apub/user.rs | 21 ++++++++++++++++++++- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 994e75f2b..ebc6902dc 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -130,6 +130,9 @@ pub fn get_or_fetch_and_upsert_remote_user( if !u.local && u .last_refreshed_at + // TODO it won't pick up new avatars, summaries etc until a day after. + // Both user and community need an "update" action pushed to other servers + // to fix this .lt(&(naive_now() - chrono::Duration::days(1))) { debug!("Fetching and updating from remote user: {}", apub_id); diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index 9d4a60106..51f88df26 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -21,7 +21,7 @@ use activitystreams::{ endpoint::EndpointProperties, ext::{Ext, Extensible}, link::Mention, - object::{properties::ObjectProperties, Note, Page, Tombstone}, + object::{properties::ObjectProperties, AnyImage, Image, Note, Page, Tombstone}, public, BaseBox, }; use actix_web::body::Body; diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 3ea2040dc..f2942b84a 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -53,6 +53,20 @@ impl ToApub for Post { oprops.set_url_xsd_any_uri(u.to_owned())?; } + if let Some(thumbnail_url) = &self.thumbnail_url { + let full_url = format!( + "{}://{}/pictshare/{}", + get_apub_protocol_string(), + Settings::get().hostname, + thumbnail_url + ); + + let mut image = Image::new(); + image.object_props.set_url_xsd_any_uri(full_url)?; + let any_image = AnyImage::from_concrete(image)?; + oprops.set_image_any_image(any_image)?; + } + if let Some(u) = self.updated { oprops.set_updated(convert_datetime(u))?; } @@ -87,6 +101,16 @@ impl FromApub for PostForm { let community_actor_id = &oprops.get_to_xsd_any_uri().unwrap().to_string(); let community = get_or_fetch_and_upsert_remote_community(&community_actor_id, &conn)?; + let thumbnail_url = match oprops.get_image_any_image() { + Some(any_image) => any_image + .to_owned() + .into_concrete::()? + .object_props + .get_url_xsd_any_uri() + .map(|u| u.to_string()), + None => None, + }; + Ok(PostForm { name: oprops.get_summary_xsd_string().unwrap().to_string(), url: oprops.get_url_xsd_any_uri().map(|u| u.to_string()), @@ -107,7 +131,7 @@ impl FromApub for PostForm { embed_title: None, // -> attachment? or fetch the embed locally embed_description: None, embed_html: None, - thumbnail_url: None, + thumbnail_url, ap_id: oprops.get_id().unwrap().to_string(), local: false, }) diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 61661de47..395dda9cb 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -27,6 +27,15 @@ impl ToApub for User_ { oprops.set_name_xsd_string(i.to_owned())?; } + if let Some(avatar_url) = &self.avatar { + let mut image = Image::new(); + image + .object_props + .set_url_xsd_any_uri(avatar_url.to_owned())?; + let any_image = AnyImage::from_concrete(image)?; + oprops.set_icon_any_image(any_image)?; + } + let mut endpoint_props = EndpointProperties::default(); endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?; @@ -152,6 +161,16 @@ impl FromApub for UserForm { let aprops = &person.base.extension; let public_key: &PublicKey = &person.extension.public_key; + let avatar = match oprops.get_icon_any_image() { + Some(any_image) => any_image + .to_owned() + .into_concrete::()? + .object_props + .get_url_xsd_any_uri() + .map(|u| u.to_string()), + None => None, + }; + Ok(UserForm { name: oprops.get_name_xsd_string().unwrap().to_string(), preferred_username: aprops.get_preferred_username().map(|u| u.to_string()), @@ -159,7 +178,7 @@ impl FromApub for UserForm { admin: false, banned: false, email: None, - avatar: None, // -> icon, image + avatar, updated: oprops .get_updated() .map(|u| u.as_ref().to_owned().naive_local()), From f88180650df8c8808cf0f9cd53053e49eb46fb54 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 15 May 2020 23:40:36 -0400 Subject: [PATCH 2/2] Adding federated embeds via the preview field. --- server/src/apub/post.rs | 57 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index f2942b84a..d32ba3ae1 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -51,6 +51,32 @@ impl ToApub for Post { let url = self.url.as_ref().filter(|u| !u.is_empty()); if let Some(u) = url { oprops.set_url_xsd_any_uri(u.to_owned())?; + + // Embeds + let mut page_preview = Page::new(); + page_preview + .object_props + .set_url_xsd_any_uri(u.to_owned())?; + + if let Some(embed_title) = &self.embed_title { + page_preview + .object_props + .set_name_xsd_string(embed_title.to_owned())?; + } + + if let Some(embed_description) = &self.embed_description { + page_preview + .object_props + .set_summary_xsd_string(embed_description.to_owned())?; + } + + if let Some(embed_html) = &self.embed_html { + page_preview + .object_props + .set_content_xsd_string(embed_html.to_owned())?; + } + + oprops.set_preview_base_box(page_preview)?; } if let Some(thumbnail_url) = &self.thumbnail_url { @@ -111,9 +137,30 @@ impl FromApub for PostForm { None => None, }; + let url = oprops.get_url_xsd_any_uri().map(|u| u.to_string()); + let (embed_title, embed_description, embed_html) = match oprops.get_preview_base_box() { + Some(preview) => { + let preview_page = preview.to_owned().into_concrete::()?; + let name = preview_page + .object_props + .get_name_xsd_string() + .map(|n| n.to_string()); + let summary = preview_page + .object_props + .get_summary_xsd_string() + .map(|s| s.to_string()); + let content = preview_page + .object_props + .get_content_xsd_string() + .map(|c| c.to_string()); + (name, summary, content) + } + None => (None, None, None), + }; + Ok(PostForm { name: oprops.get_summary_xsd_string().unwrap().to_string(), - url: oprops.get_url_xsd_any_uri().map(|u| u.to_string()), + url, body: oprops.get_content_xsd_string().map(|c| c.to_string()), creator_id: creator.id, community_id: community.id, @@ -127,10 +174,10 @@ impl FromApub for PostForm { .map(|u| u.as_ref().to_owned().naive_local()), deleted: None, nsfw: ext.sensitive, - stickied: None, // -> put it in "featured" collection of the community - embed_title: None, // -> attachment? or fetch the embed locally - embed_description: None, - embed_html: None, + stickied: None, // -> put it in "featured" collection of the community + embed_title, + embed_description, + embed_html, thumbnail_url, ap_id: oprops.get_id().unwrap().to_string(), local: false,