From 45962918683290be0a0ed9091d23f86f20a6d33e Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 14 Jan 2024 10:20:11 +0100 Subject: Additional stream tag types and flags + optional fields. --- src/bin/zot/main.rs | 7 ++++++- src/stream/actor.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++---- src/stream/streamitem.rs | 15 ++++++++------ src/stream/tag.rs | 24 +++++++++++++++++++++- 4 files changed, 87 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/bin/zot/main.rs b/src/bin/zot/main.rs index 209e097..83ae916 100644 --- a/src/bin/zot/main.rs +++ b/src/bin/zot/main.rs @@ -117,7 +117,12 @@ async fn main() -> Result<(), Box<(dyn std::error::Error + 'static)>> { } } ("stream", Some(_)) => { - let s = Stream::from_json(&z.channel_stream().await?)?; + let json = z.channel_stream().await?; + + std::fs::write("channel_stream.json", &json) + .expect("Unable to write channel_stream.json file"); + + let s = Stream::from_json(&json)?; for item in s.items { if item.is_post() { if item.title.len() > 0 { diff --git a/src/stream/actor.rs b/src/stream/actor.rs index 88090cf..4e2bbe4 100644 --- a/src/stream/actor.rs +++ b/src/stream/actor.rs @@ -13,8 +13,8 @@ use url::Url; #[derive(Debug, Deserialize, PartialEq)] pub struct Actor { name: String, - address: String, - url: Url, + address: Option, + url: Option, id: String, id_sig: String, key: String, @@ -44,8 +44,53 @@ mod test { let actor: Actor = serde_json::from_str(&json).unwrap(); assert_eq!("Benjamin Franklin", actor.name); - assert_eq!("ben@example.com", actor.address); - assert_eq!("https://example.com/channel/ben", actor.url.to_string()); + assert_eq!("ben@example.com", &actor.address.unwrap()); + assert_eq!("https://example.com/channel/ben", actor.url.unwrap().to_string()); assert_eq!("rUpgk2qbvnWLoKIXOlZlwlqI5vk8C4NgudFNjbcmnOBjFSXU34TObkZEClaPSfKnpFZpg87tANtko7WGs7QRvA", actor.id); } + + #[test] + fn test_parsing_actor_without_address_from_json() { + let json = r#" + { + "name": "Benjamin Franklin", + "url": "https://example.com/channel/ben", + "network": "zot6", + "photo": { + "mimetype": "image/jpeg", + "src": "https://example.com/photo/profile/m/2" + }, + "id": "rUpgk2qbvnWLoKIXOlZlwlqI5vk8C4NgudFNjbcmnOBjFSXU34TObkZEClaPSfKnpFZpg87tANtko7WGs7QRvA", + "id_sig": "sha256.ZD8uwYmUEG_d02Y...", + "key": "-----BEGIN PUBLIC KEY-----\n....\n-----END PUBLIC KEY-----\n" + }"#; + + let actor: Actor = serde_json::from_str(&json).unwrap(); + + assert_eq!("Benjamin Franklin", actor.name); + assert_eq!(None, actor.address); + assert_eq!("https://example.com/channel/ben", actor.url.unwrap().to_string()); + assert_eq!("rUpgk2qbvnWLoKIXOlZlwlqI5vk8C4NgudFNjbcmnOBjFSXU34TObkZEClaPSfKnpFZpg87tANtko7WGs7QRvA", actor.id); + } + + #[test] + fn test_parsing_actor_without_url_from_json() { + let json = r#" + { + "name":"Diaspora User", + "address":"someone@diaspora.example.com", + "network":"diaspora", + "photo":{"mimetype":"image\/jpeg", "src":"https:\/\/example.com\/photo\/c992335849828915b5c963b985f68f46-5"}, + "id":"349ab82134669a3b", + "id_sig":"", + "key":"-----BEGIN PUBLIC KEY-----\n\n-----END PUBLIC KEY-----\n" + } + "#; + + let actor: Actor = serde_json::from_str(&json).unwrap(); + + assert_eq!("Diaspora User", actor.name); + assert_eq!("someone@diaspora.example.com", actor.address.unwrap()); + assert_eq!(None, actor.url); + } } diff --git a/src/stream/streamitem.rs b/src/stream/streamitem.rs index 3040dea..51382fc 100644 --- a/src/stream/streamitem.rs +++ b/src/stream/streamitem.rs @@ -17,20 +17,23 @@ use url::Url; use uuid::Uuid; #[derive(Debug, Deserialize, PartialEq)] +#[serde(rename_all="snake_case")] pub enum StreamItemType { - #[serde(rename="activity")] Activity, } #[derive(Debug, Deserialize, PartialEq)] +#[serde(rename_all="snake_case")] pub enum StreamItemEncoding { - #[serde(rename="zot")] Zot, } #[derive(Debug, Deserialize, PartialEq)] +#[serde(rename_all="snake_case")] pub enum StreamItemFlag { - #[serde(rename="thread_parent")] + Direct, + Notshown, + Private, ThreadParent, } @@ -69,8 +72,8 @@ pub struct StreamItem { pub author: Actor, pub signature: String, - pub flags: Vec, - pub tags: Vec, + pub flags: Option>, + pub tags: Option>, } impl StreamItem { @@ -178,6 +181,6 @@ mod test { assert_eq!("2023-12-12 17:00:42", &item.edited.to_string()); assert_eq!("0000-00-00 00:00:00", &item.expires.to_string()); assert_eq!("2023-12-19 09:01:15", &item.commented.to_string()); - assert_eq!(StreamItemFlag::ThreadParent, item.flags[0]); + assert_eq!(StreamItemFlag::ThreadParent, item.flags.unwrap()[0]); } } diff --git a/src/stream/tag.rs b/src/stream/tag.rs index 0d2ee23..36874c2 100644 --- a/src/stream/tag.rs +++ b/src/stream/tag.rs @@ -12,13 +12,19 @@ use url::Url; #[derive(Debug, Deserialize, PartialEq)] pub enum TagType { - #[serde(rename = "hashtag")] + #[serde(alias = "hashtag")] Hashtag, + + #[serde(alias = "mention")] + Mention, } #[derive(Debug, Deserialize, PartialEq)] pub struct Tag { + #[serde(alias = "name")] tag: String, + + #[serde(alias = "href")] url: Url, #[serde(rename = "type")] @@ -44,4 +50,20 @@ mod test { assert_eq!("https://example.com/search?tag=hubzilla", tag.url.to_string()); assert_eq!(TagType::Hashtag, tag.tag_type); } + + #[test] + fn test_parsing_mention_from_json() { + let json = r#" + { + "type": "Mention", + "href": "https://example.com/channel/ben", + "name": "@ben@example.com" + } + "#; + + let tag: Tag = serde_json::from_str(json).unwrap(); + assert_eq!(TagType::Mention, tag.tag_type); + assert_eq!("https://example.com/channel/ben", tag.url.to_string()); + assert_eq!("@ben@example.com", tag.tag); + } } -- cgit v1.2.3