Skip to content

Commit

Permalink
Remove utoipa and bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision committed Nov 28, 2024
1 parent a7267a3 commit 3bdc68a
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 83 deletions.
54 changes: 0 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ license = "MIT"
documentation = "https://github.com/pubky/pubky-app-specs"

[dependencies]
bytes = "^1.7.0"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
utoipa = "5.2.0"
url = "2.5.4"
base32 = "0.5.1"
blake3 = "1.5.4"
3 changes: 1 addition & 2 deletions src/bookmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ impl Validatable for PubkyAppBookmark {
mod tests {
use super::*;
use crate::traits::Validatable;
use bytes::Bytes;

#[test]
fn test_create_bookmark_id() {
Expand Down Expand Up @@ -107,7 +106,7 @@ mod tests {
let bookmark = PubkyAppBookmark::new(uri.clone());
let id = bookmark.create_id();

let blob = Bytes::from(bookmark_json);
let blob = bookmark_json.as_bytes();
let bookmark_parsed = <PubkyAppBookmark as Validatable>::try_from(&blob, &id).unwrap();

assert_eq!(bookmark_parsed.uri, uri);
Expand Down
3 changes: 1 addition & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ impl Validatable for PubkyAppFile {
mod tests {
use super::*;
use crate::traits::Validatable;
use bytes::Bytes;

#[test]
fn test_new() {
Expand Down Expand Up @@ -136,7 +135,7 @@ mod tests {
);
let id = file.create_id();

let blob = Bytes::from(file_json);
let blob = file_json.as_bytes();
let file_parsed = <PubkyAppFile as Validatable>::try_from(&blob, &id).unwrap();

assert_eq!(file_parsed.name, "example.png");
Expand Down
73 changes: 69 additions & 4 deletions src/follow.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,86 @@
use crate::traits::Validatable;
use crate::{
common::timestamp,
traits::{HasPubkyIdPath, Validatable},
APP_PATH,
};
use serde::{Deserialize, Serialize};

/// Represents raw homeserver follow object with timestamp
///
/// On follow objects, the main data is encoded in the path
///
/// URI: /pub/pubky.app/follows/:user_id
///
/// Example URI:
///
/// `/pub/pubky.app/follows/pxnu33x7jtpx9ar1ytsi4yxbp6a5o36gwhffs8zoxmbuptici1jy``
/// `/pub/pubky.app/follows/pxnu33x7jtpx9ar1ytsi4yxbp6a5o36gwhffs8zoxmbuptici1jy`
///
#[derive(Serialize, Deserialize, Default)]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct PubkyAppFollow {
created_at: i64,
}

impl PubkyAppFollow {
/// Creates a new `PubkyAppFollow` instance.
pub fn new() -> Self {
let created_at = timestamp();
Self { created_at }
}
}

impl Validatable for PubkyAppFollow {
fn validate(&self, _id: &str) -> Result<(), String> {
// TODO: additional follow validation? E.g, validate `created_at` ?
// TODO: additional follow validation? E.g., validate `created_at`?
Ok(())
}
}

impl HasPubkyIdPath for PubkyAppFollow {
fn create_path(&self, pubky_id: &str) -> String {
format!("{}follows/{}", APP_PATH, pubky_id)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::traits::Validatable;

#[test]
fn test_new() {
let follow = PubkyAppFollow::new();
// Check that created_at is recent
let now = timestamp();
assert!(follow.created_at <= now && follow.created_at >= now - 1_000_000);
// within 1 second
}

#[test]
fn test_create_path_with_id() {
let mute = PubkyAppFollow::new();
let path = mute.create_path("user_id123");
assert_eq!(path, "/pub/pubky.app/follows/user_id123");
}

#[test]
fn test_validate() {
let follow = PubkyAppFollow::new();
let result = follow.validate("some_user_id");
assert!(result.is_ok());
}

#[test]
fn test_try_from_valid() {
let follow_json = r#"
{
"created_at": 1627849723
}
"#;

let blob = follow_json.as_bytes();
let follow_parsed =
<PubkyAppFollow as Validatable>::try_from(&blob, "some_user_id").unwrap();

assert_eq!(follow_parsed.created_at, 1627849723);
}
}
70 changes: 66 additions & 4 deletions src/mute.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,83 @@
use crate::traits::Validatable;
use crate::{
common::timestamp,
traits::{HasPubkyIdPath, Validatable},
APP_PATH,
};
use serde::{Deserialize, Serialize};

/// Represents raw homeserver Mute object with timestamp
/// URI: /pub/pubky.app/mutes/:user_id
///
/// Example URI:
///
/// `/pub/pubky.app/mutes/pxnu33x7jtpx9ar1ytsi4yxbp6a5o36gwhffs8zoxmbuptici1jy``
/// `/pub/pubky.app/mutes/pxnu33x7jtpx9ar1ytsi4yxbp6a5o36gwhffs8zoxmbuptici1jy`
///
#[derive(Serialize, Deserialize, Default)]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct PubkyAppMute {
created_at: i64,
}

impl PubkyAppMute {
/// Creates a new `PubkyAppMute` instance.
pub fn new() -> Self {
let created_at = timestamp();
Self { created_at }
}
}

impl Validatable for PubkyAppMute {
fn validate(&self, _id: &str) -> Result<(), String> {
// TODO: additional Mute validation? E.g, validate `created_at` ?
// TODO: additional Mute validation? E.g., validate `created_at` ?
Ok(())
}
}

impl HasPubkyIdPath for PubkyAppMute {
fn create_path(&self, pubky_id: &str) -> String {
format!("{}mutes/{}", APP_PATH, pubky_id)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::common::timestamp;
use crate::traits::Validatable;

#[test]
fn test_new() {
let mute = PubkyAppMute::new();
// Check that created_at is recent
let now = timestamp();
assert!(mute.created_at <= now && mute.created_at >= now - 1_000_000);
// within 1 second
}

#[test]
fn test_create_path_with_id() {
let mute = PubkyAppMute::new();
let path = mute.create_path("user_id123");
assert_eq!(path, "/pub/pubky.app/mutes/user_id123");
}

#[test]
fn test_validate() {
let mute = PubkyAppMute::new();
let result = mute.validate("some_user_id");
assert!(result.is_ok());
}

#[test]
fn test_try_from_valid() {
let mute_json = r#"
{
"created_at": 1627849723
}
"#;

let blob = mute_json.as_bytes();
let mute_parsed = <PubkyAppMute as Validatable>::try_from(&blob, "some_user_id").unwrap();

assert_eq!(mute_parsed.created_at, 1627849723);
}
}
8 changes: 3 additions & 5 deletions src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use crate::{
use serde::{Deserialize, Serialize};
use std::fmt;
use url::Url;
use utoipa::ToSchema;

// Validation
const MAX_SHORT_CONTENT_LENGTH: usize = 1000;
const MAX_LONG_CONTENT_LENGTH: usize = 50000;

/// Represents the type of pubky-app posted data
/// Used primarily to best display the content in UI
#[derive(Serialize, Deserialize, ToSchema, Default, Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum PubkyAppPostKind {
#[default]
Expand Down Expand Up @@ -173,7 +172,6 @@ impl Validatable for PubkyAppPost {
mod tests {
use super::*;
use crate::traits::Validatable;
use bytes::Bytes;

#[test]
fn test_create_id() {
Expand Down Expand Up @@ -297,7 +295,7 @@ mod tests {
)
.create_id();

let blob = Bytes::from(post_json);
let blob = post_json.as_bytes();
let post = <PubkyAppPost as Validatable>::try_from(&blob, &id).unwrap();

assert_eq!(post.content, "Hello World!");
Expand All @@ -320,7 +318,7 @@ mod tests {
let id = PubkyAppPost::new(content.clone(), PubkyAppPostKind::Short, None, None, None)
.create_id();

let blob = Bytes::from(post_json);
let blob = post_json.as_bytes();
let post = <PubkyAppPost as Validatable>::try_from(&blob, &id).unwrap();

assert_eq!(post.content, "empty"); // After sanitization
Expand Down
5 changes: 2 additions & 3 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ impl Validatable for PubkyAppTag {
mod tests {
use super::*;
use crate::{traits::Validatable, APP_PATH};
use bytes::Bytes;

#[test]
fn test_create_id() {
Expand Down Expand Up @@ -221,7 +220,7 @@ mod tests {
)
.create_id();

let blob = Bytes::from(tag_json);
let blob = tag_json.as_bytes();
let tag = <PubkyAppTag as Validatable>::try_from(&blob, &id).unwrap();
assert_eq!(tag.uri, "pubky://user_pubky_id/pub/pubky.app/profile.json");
assert_eq!(tag.label, "cool tag"); // After sanitization
Expand All @@ -238,7 +237,7 @@ mod tests {
"#;

let id = "B55PGPFV1E5E0HQ2PB76EQGXPR";
let blob = Bytes::from(tag_json);
let blob = tag_json.as_bytes();
let result = <PubkyAppTag as Validatable>::try_from(&blob, &id);
assert!(result.is_err());
assert_eq!(
Expand Down
Loading

0 comments on commit 3bdc68a

Please sign in to comment.