From c2f3224e0eb6b069cc83cedd054372bc8f90b6d9 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 21 Mar 2024 20:36:56 +0100 Subject: [PATCH] fix: context <-> conversation damn you mastodon! --- src/activitystream/object/mod.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/activitystream/object/mod.rs b/src/activitystream/object/mod.rs index 3e053f6b..665a5473 100644 --- a/src/activitystream/object/mod.rs +++ b/src/activitystream/object/mod.rs @@ -101,7 +101,6 @@ impl Object for serde_json::Value { getter! { attributed_to::attributedTo -> node impl Actor } getter! { audience -> node impl Actor } getter! { content -> &str } - getter! { context -> node impl Object } getter! { name -> &str } getter! { end_time::endTime -> chrono::DateTime } getter! { generator -> node impl Actor } @@ -123,6 +122,17 @@ impl Object for serde_json::Value { getter! { media_type -> &str } getter! { duration -> &str } getter! { url -> node impl super::Link } + + // TODO Mastodon doesn't use a "context" field on the object but makes up a new one!! + fn context(&self) -> Node { + match self.get("context") { + Some(x) => Node::from(x.clone()), + None => match self.get("conversation") { + Some(x) => Node::from(x.clone()), + None => Node::empty(), + } + } + } } impl ObjectMut for serde_json::Value { @@ -131,7 +141,6 @@ impl ObjectMut for serde_json::Value { setter! { attributed_to::attributedTo -> node impl Actor } setter! { audience -> node impl Actor } setter! { content -> &str } - setter! { context -> node impl Object } setter! { name -> &str } setter! { end_time::endTime -> chrono::DateTime } setter! { generator -> node impl Actor } @@ -154,4 +163,17 @@ impl ObjectMut for serde_json::Value { setter! { duration -> &str } setter! { url -> node impl super::Link } + // TODO Mastodon doesn't use a "context" field on the object but makes up a new one!! + fn set_context(mut self, ctx: Node) -> Self { + if let Some(conversation) = ctx.id() { + crate::activitystream::macros::set_maybe_value( + &mut self, "conversation", Some(serde_json::Value::String(conversation.to_string())), + ); + } + crate::activitystream::macros::set_maybe_node( + &mut self, "context", ctx + ); + self + } + }