Skip to content

Commit

Permalink
Accept Image objects in attachments (#2394)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpzomtrrfrt authored Aug 16, 2022
1 parent 2f9d877 commit 08a797c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
26 changes: 26 additions & 0 deletions crates/apub/assets/lotide/activities/create_page_image.json
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 5 additions & 2 deletions crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
23 changes: 19 additions & 4 deletions crates/apub/src/protocol/objects/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
}
})
}
}

Expand Down

0 comments on commit 08a797c

Please sign in to comment.