From bfe56f614d054933ba7c8000af3913e345190bed Mon Sep 17 00:00:00 2001 From: viridian Date: Sun, 11 Feb 2024 10:32:18 +0100 Subject: [PATCH] Parse actor json --- .gitignore | 1 + Cargo.lock | 89 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 +++++ src/ap/mod.rs | 67 +++++++++++++++++++++++++++++++ src/main.rs | 4 ++ test-json.json | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 278 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/ap/mod.rs create mode 100644 src/main.rs create mode 100644 test-json.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..68bd447 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,89 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" + +[[package]] +name = "serde" +version = "1.0.196" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.196" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.113" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "yaskey" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f529015 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "nushtyu" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/src/ap/mod.rs b/src/ap/mod.rs new file mode 100644 index 0000000..b576098 --- /dev/null +++ b/src/ap/mod.rs @@ -0,0 +1,67 @@ +use serde_json; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize,Debug)] +pub struct Actor { + #[serde(rename = "type")] + pub object_type: ObjectType, + pub name: String, // The username + #[serde(rename = "preferredUsername")] + pub preferred_username: String, // The @ handle + pub following: String, // Link to list of users which are followed by them + pub followers: String, // Link to list of users which follow them + pub inbox: String, // Link to collection where notes will be posted to + pub outbox: String, // Link to notes by this user + pub summary: Option, // Bio + pub icon: Option, // Link to pfp +} + +#[derive(Serialize, Deserialize,Debug,PartialEq)] +pub struct Icon { + #[serde(rename = "type")] + media_type: String, + #[serde(rename = "mediaType")] + media_mime_type: String, + url: String, + +} +#[derive(Serialize, Deserialize,Debug,PartialEq)] +pub enum ObjectType { + Person, + Like, + Note, + Tombstone, +} + +pub enum Object { + Person(Actor), + Like, + Note, + Tombstone, +} + +pub fn json_to_actor(input: &String)->Actor { + let return_data: Actor = serde_json::from_str(&input).unwrap(); + return_data +} +#[cfg(test)] +mod tests { + + use super::*; + use std::fs::read_to_string; + #[test] + fn parse_json_into_actor() { + let input_string = read_to_string("test-json.json").unwrap(); + let actor: Actor = json_to_actor(&input_string); + + assert_eq!(actor.object_type,ObjectType::Person); + assert_eq!(actor.name,"Sebastian Jambor".to_string()); + assert_eq!(actor.preferred_username,"crepels"); + assert_eq!(actor.following,"https://mastodon.social/users/crepels/following".to_string()); + assert_eq!(actor.followers,"https://mastodon.social/users/crepels/followers".to_string()); + assert_eq!(actor.inbox,"https://mastodon.social/users/crepels/inbox".to_string()); + assert_eq!(actor.outbox,"https://mastodon.social/users/crepels/outbox".to_string()); + assert_eq!(actor.summary,Some("

I created a systemd playground to help people learn systemd.

".to_string())); + assert_eq!(actor.icon,Some(Icon{media_type: "Image".to_string(), media_mime_type: "image/png".to_string(), url: "https://files.mastodon.social/accounts/avatars/108/227/485/389/961/502/original/1213d525278ae01d.png".to_string()})) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6168b96 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,4 @@ +mod ap; +fn main() { + println!("This does nothing look at ap/mod.rs and run cargo test to see it in action"); +} diff --git a/test-json.json b/test-json.json new file mode 100644 index 0000000..ff863ac --- /dev/null +++ b/test-json.json @@ -0,0 +1,107 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "toot": "http://joinmastodon.org/ns#", + "featured": { + "@id": "toot:featured", + "@type": "@id" + }, + "featuredTags": { + "@id": "toot:featuredTags", + "@type": "@id" + }, + "alsoKnownAs": { + "@id": "as:alsoKnownAs", + "@type": "@id" + }, + "movedTo": { + "@id": "as:movedTo", + "@type": "@id" + }, + "schema": "http://schema.org#", + "PropertyValue": "schema:PropertyValue", + "value": "schema:value", + "discoverable": "toot:discoverable", + "Device": "toot:Device", + "Ed25519Signature": "toot:Ed25519Signature", + "Ed25519Key": "toot:Ed25519Key", + "Curve25519Key": "toot:Curve25519Key", + "EncryptedMessage": "toot:EncryptedMessage", + "publicKeyBase64": "toot:publicKeyBase64", + "deviceId": "toot:deviceId", + "claim": { + "@type": "@id", + "@id": "toot:claim" + }, + "fingerprintKey": { + "@type": "@id", + "@id": "toot:fingerprintKey" + }, + "identityKey": { + "@type": "@id", + "@id": "toot:identityKey" + }, + "devices": { + "@type": "@id", + "@id": "toot:devices" + }, + "messageFranking": "toot:messageFranking", + "messageType": "toot:messageType", + "cipherText": "toot:cipherText", + "suspended": "toot:suspended", + "memorial": "toot:memorial", + "indexable": "toot:indexable", + "focalPoint": { + "@container": "@list", + "@id": "toot:focalPoint" + } + } + ], + "id": "https://mastodon.social/users/crepels", + "type": "Person", + "following": "https://mastodon.social/users/crepels/following", + "followers": "https://mastodon.social/users/crepels/followers", + "inbox": "https://mastodon.social/users/crepels/inbox", + "outbox": "https://mastodon.social/users/crepels/outbox", + "featured": "https://mastodon.social/users/crepels/collections/featured", + "featuredTags": "https://mastodon.social/users/crepels/collections/tags", + "preferredUsername": "crepels", + "name": "Sebastian Jambor", + "summary": "

I created a systemd playground to help people learn systemd.

", + "url": "https://mastodon.social/@crepels", + "manuallyApprovesFollowers": false, + "discoverable": true, + "indexable": true, + "published": "2022-05-01T00:00:00Z", + "memorial": false, + "devices": "https://mastodon.social/users/crepels/collections/devices", + "publicKey": { + "id": "https://mastodon.social/users/crepels#main-key", + "owner": "https://mastodon.social/users/crepels", + "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzCg7ogX8utFGjeOs7EeX\nKLUixqzlyrCFobv+Pcqhmw8WrMYnm3jrbUWkjcOUHaepAFAgauns+Tr5XwmAsNkx\nYLqyVipartwf8/tdiHZAhHgOdvtfd3dXHDpZKyeQfOtdt2AxftMihg9wmCyupAJC\naLNbyx4S9Q+aDVkaaHiFvhTNWHNR4rf3OKvQixiopry0G9oCCkUxWs+z4RT8Rx+R\nOLteufRh6WOn7YgqeMcJ4hZ/DcQZD3JtdJfEwUDftEW3xkoI/T1IA2QYAr3m4xHJ\nhv3TONr6VD/Bx5RPUsBDvXOlN5hbadClOQURybuyUBe1D+Md6SYHSQ4Q/x0y6L9+\nhQIDAQAB\n-----END PUBLIC KEY-----\n" + }, + "tag": [], + "attachment": [ + { + "type": "PropertyValue", + "name": "Blog", + "value": "https://seb.jambor.dev" + }, + { + "type": "PropertyValue", + "name": "systemd by example", + "value": "https://systemd-by-example.com/" + } + ], + "endpoints": { + "sharedInbox": "https://mastodon.social/inbox" + }, + "icon": { + "type": "Image", + "mediaType": "image/png", + "url": "https://files.mastodon.social/accounts/avatars/108/227/485/389/961/502/original/1213d525278ae01d.png" + } +}