Parse actor json

This commit is contained in:
viridian 2024-02-11 10:32:18 +01:00
commit bfe56f614d
Signed by: viridian
GPG key ID: DCD4DF95CE23FE8C
6 changed files with 278 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

89
Cargo.lock generated Normal file
View file

@ -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",
]

10
Cargo.toml Normal file
View file

@ -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"

67
src/ap/mod.rs Normal file
View file

@ -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<String>, // Bio
pub icon: Option<Icon>, // 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("<p>I created a systemd playground to help people learn systemd.</p>".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()}))
}
}

4
src/main.rs Normal file
View file

@ -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");
}

107
test-json.json Normal file
View file

@ -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": "<p>I created a systemd playground to help people learn systemd.</p>",
"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": "<a href=\"https://seb.jambor.dev\" target=\"_blank\" rel=\"nofollow noopener noreferrer me\" translate=\"no\"><span class=\"invisible\">https://</span><span class=\"\">seb.jambor.dev</span><span class=\"invisible\"></span></a>"
},
{
"type": "PropertyValue",
"name": "systemd by example",
"value": "<a href=\"https://systemd-by-example.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer me\" translate=\"no\"><span class=\"invisible\">https://</span><span class=\"\">systemd-by-example.com/</span><span class=\"invisible\"></span></a>"
}
],
"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"
}
}