From 08a797c986ba92736f12761b18980f557dd156d4 Mon Sep 17 00:00:00 2001 From: vpzomtrrfrt Date: Tue, 16 Aug 2022 03:12:43 -0700 Subject: [PATCH] Accept Image objects in attachments (#2394) --- .../lotide/activities/create_page_image.json | 26 +++++++++++++++++++ crates/apub/src/objects/post.rs | 7 +++-- crates/apub/src/protocol/activities/mod.rs | 1 + crates/apub/src/protocol/objects/page.rs | 23 +++++++++++++--- 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 crates/apub/assets/lotide/activities/create_page_image.json diff --git a/crates/apub/assets/lotide/activities/create_page_image.json b/crates/apub/assets/lotide/activities/create_page_image.json new file mode 100644 index 000000000..ca00fdc1a --- /dev/null +++ b/crates/apub/assets/lotide/activities/create_page_image.json @@ -0,0 +1,26 @@ +{ + "actor": "http://ltthostname.local:3334/apub/users/3", + "object": { + "@context": "https://www.w3.org/ns/activitystreams", + "id": "http://ltthostname.local:3334/apub/posts/46", + "type": "Note", + "name": "image", + "to": "http://localhost:8536/c/elsewhere", + "cc": "https://www.w3.org/ns/activitystreams#Public", + "attributedTo": "http://ltthostname.local:3334/apub/users/3", + "attachment": [ + { + "type": "Image", + "url": "http://ltthostname.local:3334/api/stable/posts/46/href" + } + ], + "sensitive": false, + "published": "2022-08-06T18:35:01.043072+00:00", + "summary": "image" + }, + "to": "http://localhost:8536/c/elsewhere", + "cc": "https://www.w3.org/ns/activitystreams#Public", + "@context": "https://www.w3.org/ns/activitystreams", + "id": "http://ltthostname.local:3334/apub/posts/46/create", + "type": "Create" +} diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 67be5c568..bc1b44f54 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -158,8 +158,11 @@ impl ApubObject for ApubPost { let form = if !page.is_mod_action(context).await? { let url = if let Some(attachment) = page.attachment.first() { - // url as sent by Lemmy (new) - Some(attachment.href.clone()) + Some(match attachment { + // url as sent by Lemmy (new) + Attachment::Link(link) => link.href.clone(), + Attachment::Image(image) => image.url.clone(), + }) } else if page.kind == PageType::Video { // we cant display videos directly, so insert a link to external video page Some(page.id.inner().clone()) diff --git a/crates/apub/src/protocol/activities/mod.rs b/crates/apub/src/protocol/activities/mod.rs index 82c5215b4..3b8c9e20c 100644 --- a/crates/apub/src/protocol/activities/mod.rs +++ b/crates/apub/src/protocol/activities/mod.rs @@ -52,6 +52,7 @@ mod tests { #[test] fn test_parse_lotide_activities() { test_json::("assets/lotide/activities/create_page.json").unwrap(); + test_json::("assets/lotide/activities/create_page_image.json").unwrap(); test_json::("assets/lotide/activities/create_note_reply.json").unwrap(); } diff --git a/crates/apub/src/protocol/objects/page.rs b/crates/apub/src/protocol/objects/page.rs index 8aa5a49a0..9a47324ef 100644 --- a/crates/apub/src/protocol/objects/page.rs +++ b/crates/apub/src/protocol/objects/page.rs @@ -13,7 +13,7 @@ use activitypub_federation::{ }, traits::{ActivityHandler, ApubObject}, }; -use activitystreams_kinds::link::LinkType; +use activitystreams_kinds::{link::LinkType, object::ImageType}; use chrono::{DateTime, FixedOffset}; use itertools::Itertools; use lemmy_db_schema::newtypes::DbUrl; @@ -66,11 +66,26 @@ pub struct Page { #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -pub(crate) struct Attachment { +pub(crate) struct Link { pub(crate) href: Url, pub(crate) r#type: LinkType, } +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct Image { + #[serde(rename = "type")] + pub(crate) kind: ImageType, + pub(crate) url: Url, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(untagged)] +pub(crate) enum Attachment { + Link(Link), + Image(Image), +} + #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(untagged)] pub(crate) enum AttributedTo { @@ -174,10 +189,10 @@ impl Page { impl Attachment { pub(crate) fn new(url: DbUrl) -> Attachment { - Attachment { + Attachment::Link(Link { href: url.into(), r#type: Default::default(), - } + }) } }