-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7267a3
commit 3bdc68a
Showing
10 changed files
with
345 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.