From ba07b2cb9d43f0a46795174b6beae4dfd514217b Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 23 Apr 2024 20:58:37 +0200 Subject: [PATCH] fix(apb): correct node order when iterating use a VecDeque under the hood to be able to pop_front() --- apb/src/node.rs | 12 ++++++------ src/routes/activitypub/object/mod.rs | 2 +- src/server/context.rs | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apb/src/node.rs b/apb/src/node.rs index 68ea1fa..fa1c7d7 100644 --- a/apb/src/node.rs +++ b/apb/src/node.rs @@ -1,7 +1,7 @@ /// ActivityPub object node, representing either nothing, something, a link to something or /// multiple things pub enum Node { - Array(Vec), // TODO would be cool to make it Box<[T]> so that Node is just a ptr + Array(std::collections::VecDeque), // TODO would be cool to make it Box<[T]> so that Node is just a ptr Object(Box), Link(Box), // TODO feature flag to toggle these maybe? Empty, @@ -23,7 +23,7 @@ impl Iterator for Node { let x = match self { Self::Empty => return None, Self::Link(_) => return None, - Self::Array(arr) => return arr.pop(), // TODO weird that we iter in reverse + Self::Array(arr) => return arr.pop_front(), // TODO weird that we iter in reverse Self::Object(x) => *x.clone(), // TODO needed because next() on object can't get value without owning }; *self = Self::Empty; @@ -37,7 +37,7 @@ impl Node { match self { Node::Empty | Node::Link(_) => None, Node::Object(x) => Some(x), - Node::Array(v) => v.last(), // TODO so it's coherent with next(), still weird tho! + Node::Array(v) => v.get(0), } } @@ -46,7 +46,7 @@ impl Node { match self { Node::Empty | Node::Link(_) => None, Node::Object(x) => Some(*x), - Node::Array(mut v) => v.pop(), // TODO so it's coherent with next(), still weird tho! + Node::Array(mut v) => v.pop_front(), } } @@ -124,7 +124,7 @@ impl Node { } pub fn array(values: Vec) -> Self { - Node::Array(values) + Node::Array(values.into()) } #[cfg(feature = "fetch")] @@ -165,7 +165,7 @@ impl From for Node { fn from(value: serde_json::Value) -> Self { match value { serde_json::Value::String(uri) => Node::Link(Box::new(uri)), - serde_json::Value::Array(arr) => Node::Array(arr), + serde_json::Value::Array(arr) => Node::Array(arr.into()), serde_json::Value::Object(_) => match value.get("href") { None => Node::Object(Box::new(value)), Some(_) => Node::Link(Box::new(value)), diff --git a/src/routes/activitypub/object/mod.rs b/src/routes/activitypub/object/mod.rs index d47ef4d..bca2bda 100644 --- a/src/routes/activitypub/object/mod.rs +++ b/src/routes/activitypub/object/mod.rs @@ -50,7 +50,7 @@ pub async fn view( Ok(JsonLD( object.object.ap() .set_replies(apb::Node::object(replies)) - .set_attachment(apb::Node::Array(attachments)) + .set_attachment(apb::Node::array(attachments)) .ld_context() )) } diff --git a/src/server/context.rs b/src/server/context.rs index 112f59b..f3df64d 100644 --- a/src/server/context.rs +++ b/src/server/context.rs @@ -237,7 +237,7 @@ impl Context { .set_collection_type(Some(apb::CollectionType::OrderedCollectionPage)) .set_part_of(apb::Node::link(id.replace("/page", ""))) .set_next(apb::Node::link(format!("{id}?offset={}", offset+limit))) - .set_ordered_items(apb::Node::Array(items)) + .set_ordered_items(apb::Node::array(items)) } pub async fn dispatch(&self, uid: &str, activity_targets: Vec, aid: &str, oid: Option<&str>) -> crate::Result<()> {