]> Untitled Git - lemmy.git/commitdiff
Accept Image objects in attachments (#2394)
authorvpzomtrrfrt <vpzomtrrfrt@gmail.com>
Tue, 16 Aug 2022 10:12:43 +0000 (03:12 -0700)
committerGitHub <noreply@github.com>
Tue, 16 Aug 2022 10:12:43 +0000 (10:12 +0000)
crates/apub/assets/lotide/activities/create_page_image.json [new file with mode: 0644]
crates/apub/src/objects/post.rs
crates/apub/src/protocol/activities/mod.rs
crates/apub/src/protocol/objects/page.rs

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 (file)
index 0000000..ca00fdc
--- /dev/null
@@ -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"
+}
index 67be5c568b58e7934647dae3f7805a2ff07aa4dd..bc1b44f54a4ef656cf38e06611f835f5a976dba4 100644 (file)
@@ -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())
index 82c5215b4410e1a787ff2943fbdba9b1a8a08287..3b8c9e20ceec8a9d60b9bc153eaee941a6f7fe14 100644 (file)
@@ -52,6 +52,7 @@ mod tests {
   #[test]
   fn test_parse_lotide_activities() {
     test_json::<CreateOrUpdatePost>("assets/lotide/activities/create_page.json").unwrap();
+    test_json::<CreateOrUpdatePost>("assets/lotide/activities/create_page_image.json").unwrap();
     test_json::<CreateOrUpdateComment>("assets/lotide/activities/create_note_reply.json").unwrap();
   }
 
index 8aa5a49a03c9702d789bd63098341bf5638f11d0..9a47324ef586541c4a3b21eb88f73226c5f65bcd 100644 (file)
@@ -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(),
-    }
+    })
   }
 }