From b65a51a1efaf75b5cbbacb848cbbc7a54b6da83d Mon Sep 17 00:00:00 2001 From: aprilthepink Date: Fri, 17 May 2024 13:24:41 +0200 Subject: [PATCH] feat: Update API domain variable name --- Cargo.lock | 9 ++++ Cargo.toml | 9 ++++ src/lysand/objects.rs | 107 ++++++++++++++++++++++++++++++++++-------- src/lysand/superx.rs | 20 ++++++++ src/lysand/test.rs | 11 +++++ src/main.rs | 4 +- 6 files changed, 139 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0d4dfd4..eb62c0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -457,6 +457,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "atomic" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59bdb34bc650a32731b31bd8f0829cc15d24a708ee31559e0bb34f2bc320cba" + [[package]] name = "autocfg" version = "1.2.0" @@ -1917,6 +1923,7 @@ dependencies = [ "tokio", "tracing", "url", + "uuid", "vcpkg", ] @@ -3784,7 +3791,9 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0" dependencies = [ + "atomic", "getrandom", + "rand", "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 973d159..dc6dad7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,15 @@ features = [ "sqlx-sqlite","sqlx-mysql","with-chrono" ] +[dependencies.uuid] +version = "1.8.0" +features = [ + "v4", + "v7", + "fast-rng", # Use a faster (but still sufficiently random) RNG + "serde", +] + [build-dependencies] vcpkg = "0.2.15" diff --git a/src/lysand/objects.rs b/src/lysand/objects.rs index 4ed13de..08d09ee 100644 --- a/src/lysand/objects.rs +++ b/src/lysand/objects.rs @@ -13,6 +13,7 @@ use time::{ OffsetDateTime, }; use url::Url; +use uuid::Uuid; const FORMAT: Iso8601<6651332276412969266533270467398074368> = Iso8601::< { @@ -49,6 +50,25 @@ pub enum LysandType { ServerMetadata, } +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum CategoryType { + Microblog, + Forum, + Blog, + Image, + Video, + Audio, + Messaging +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum VisibilityType { + Public, + Unlisted, + Followers, + Direct +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub enum LysandExtensions { #[serde(rename = "org.lysand:microblogging/Announce")] @@ -116,7 +136,7 @@ impl<'de> Deserialize<'de> for ContentFormat { #[derive(Debug, Serialize, Deserialize, Clone)] struct FieldKV { - key: ContentFormat, + name: ContentFormat, value: ContentFormat, } @@ -135,25 +155,74 @@ pub struct ContentEntry { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct User { - public_key: PublicKey, + pub public_key: PublicKey, #[serde(rename = "type")] - rtype: LysandType, - id: String, - uri: Url, + pub rtype: LysandType, + pub id: Uuid, + pub uri: Url, #[serde(with = "iso_lysand")] - created_at: OffsetDateTime, - display_name: Option, + pub created_at: OffsetDateTime, + pub display_name: Option, // TODO bio: Option, - inbox: Url, - outbox: Url, - featured: Url, - followers: Url, - following: Url, - likes: Url, - dislikes: Url, - username: String, - bio: Option, - avatar: Option, - header: Option, - fields: Option>, + pub inbox: Url, + pub outbox: Url, + pub featured: Url, + pub followers: Url, + pub following: Url, + pub likes: Url, + pub dislikes: Url, + pub username: String, + pub bio: Option, + pub avatar: Option, + pub header: Option, + pub fields: Option>, } + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct DeviceInfo { + name: String, + version: String, + url: Url, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct LinkPreview { + description: String, + title: String, + link: Url, + image: Option, + icon: Option +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Note { + #[serde(rename = "type")] + pub rtype: LysandType, + pub id: Uuid, + pub uri: Url, + pub author: Url, + #[serde(with = "iso_lysand")] + pub created_at: OffsetDateTime, + pub category: Option, + pub content: Option, + pub device: Option, + pub previews: Option>, + pub group: Option, + pub attachments: Option>, + pub replies_to: Option, + pub quotes: Option, + pub mentions: Option>, + pub subject: Option, + pub is_sensitive: Option, + pub visibility: Option, + //TODO extensions +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Outbox { + pub first: Url, + pub last: Url, + pub next: Option, + pub prev: Option, + pub items: Vec, +} \ No newline at end of file diff --git a/src/lysand/superx.rs b/src/lysand/superx.rs index d8d8f92..a7843de 100644 --- a/src/lysand/superx.rs +++ b/src/lysand/superx.rs @@ -22,6 +22,26 @@ pub async fn serialize_lysand_type( Ok(data) } +pub async fn deserialize_note(data: String) -> anyhow::Result { + let post: super::objects::Note = serde_json::from_str(&data)?; + Ok(post) +} + +pub async fn serialize_note(post: super::objects::Note) -> anyhow::Result { + let data = serde_json::to_string(&SortAlphabetically(&post))?; + Ok(data) +} + +pub async fn deserialize_outbox(data: String) -> anyhow::Result { + let outbox: super::objects::Outbox = serde_json::from_str(&data)?; + Ok(outbox) +} + +pub async fn serialize_outbox(outbox: super::objects::Outbox) -> anyhow::Result { + let data = serde_json::to_string(&SortAlphabetically(&outbox))?; + Ok(data) +} + #[inline] pub fn request_client() -> reqwest::Client { reqwest::Client::builder() diff --git a/src/lysand/test.rs b/src/lysand/test.rs index 50b3a5b..733b3c4 100644 --- a/src/lysand/test.rs +++ b/src/lysand/test.rs @@ -22,5 +22,16 @@ pub async fn main() -> anyhow::Result<()> { let user_json = serde_json::to_string_pretty(&SortAlphabetically(&user))?; println!("{}", user_json); + let response_outbox = client + .get(user.outbox.as_str()) + .send() + .await?; + + let outbox_json = response_outbox.text().await?; + let outbox = super::superx::deserialize_outbox(outbox_json).await?; + + println!("\n\n\nOutbox: "); + print!("{:#?}", outbox); + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 6b08c78..66dbcf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -151,8 +151,8 @@ async fn main() -> actix_web::Result<(), anyhow::Error> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); //TODO remove this - //lysand::test::main().await?; - //return Ok(()); + lysand::test::main().await?; + return Ok(()); let ap_id = Url::parse(&format!( "https://{}/{}",