From 7876b95de52b4dc83402ee06858273eb8af0d835 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 16 Mar 2024 03:27:36 +0100 Subject: [PATCH] feat: recreated models, now with sea_orm old ones from mastodon's blog weren't that useful since all objects/activities are traits in the end --- src/model/activity.rs | 45 --------------------------------- src/model/actor.rs | 57 ------------------------------------------ src/model/like.rs | 0 src/model/mod.rs | 8 +++--- src/model/object.rs | 57 ------------------------------------------ src/model/relation.rs | 0 src/model/status.rs | 0 src/model/user.rs | 26 +++++++++++++++++++ src/model/vote.rs | 0 src/model/webfinger.rs | 40 ----------------------------- 10 files changed, 31 insertions(+), 202 deletions(-) delete mode 100644 src/model/actor.rs create mode 100644 src/model/like.rs delete mode 100644 src/model/object.rs create mode 100644 src/model/relation.rs create mode 100644 src/model/status.rs create mode 100644 src/model/user.rs create mode 100644 src/model/vote.rs delete mode 100644 src/model/webfinger.rs diff --git a/src/model/activity.rs b/src/model/activity.rs index 947d029..e69de29 100644 --- a/src/model/activity.rs +++ b/src/model/activity.rs @@ -1,45 +0,0 @@ -use serde::{Serialize, Deserialize}; - -use super::object::Object; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Activity { - #[serde(rename = "@context")] - context: String, - - id: String, - - #[serde(rename = "type")] - activity_type: ActivityType, - - actor: String, - - object: Object, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub enum ActivityType { - Create, -} - -#[cfg(test)] -mod test { - use super::{Activity, ActivityType}; - use crate::model::object::Object; - - #[test] - fn activity_serializes_as_expected() { - let activity = Activity { - context: "https://www.w3.org/ns/activitystreams".into(), - id: "https://my-example.com/create-hello-world".into(), - activity_type: ActivityType::Create, - actor: "https://my-example.com/actor".into(), - object: Object::default(), - }; - - let serialized_activity = serde_json::to_string(&activity).unwrap(); - let expected_serialized_activity = "{\"@context\":\"https://www.w3.org/ns/activitystreams\",\"id\":\"https://my-example.com/create-hello-world\",\"type\":\"Create\",\"actor\":\"https://my-example.com/actor\",\"object\":{\"id\":\"https://my-example.com/hello-world\",\"type\":\"Note\",\"published\":\"2018-06-23T17:17:11Z\",\"attributedTo\":\"https://my-example.com/actor\",\"inReplyTo\":\"https://mastodon.social/@Gargron/100254678717223630\",\"content\":\"

Hello world

\",\"to\":\"https://www.w3.org/ns/activitystreams#Public\"}}"; - - assert_eq!(serialized_activity, expected_serialized_activity); - } -} diff --git a/src/model/actor.rs b/src/model/actor.rs deleted file mode 100644 index 87db2ca..0000000 --- a/src/model/actor.rs +++ /dev/null @@ -1,57 +0,0 @@ -use serde::{Serialize, Deserialize}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Actor { - #[serde(rename = "@context")] - pub context: Vec, // note: must be @context - pub id: String, - #[serde(rename = "type")] - pub actor_type: ActorType, - #[serde(rename = "preferredUsername")] - pub preferred_username: String, - pub inbox: String, - #[serde(rename = "publicKey")] - pub public_key: PublicKey, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct PublicKey { - pub id: String, - pub owner: String, - #[serde(rename = "publicKeyPem")] - pub public_key_pem: String, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub enum ActorType { - Person, -} - -#[cfg(test)] -mod test { - use super::{Actor, ActorType, PublicKey}; - - #[test] - fn actor_serializes_as_expected() { - let actor = Actor { - context: vec![ - "https://www.w3.org/ns/activitystreams".into(), - "https://w3id.org/security/v1".into() - ], - id: "https://my-example.com/actor".into(), - actor_type: ActorType::Person, - preferred_username: "alice".into(), - inbox: "https://my-example.com/inbox".into(), - public_key: PublicKey { - id: "https://my-example.com/actor#main-key".into(), - owner: "https://my-example.com/actor".into(), - public_key_pem: "-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----".into(), - }, - }; - - let serialized_actor = serde_json::to_string(&actor).unwrap(); - let expected_serialized_actor = "{\"@context\":[\"https://www.w3.org/ns/activitystreams\",\"https://w3id.org/security/v1\"],\"id\":\"https://my-example.com/actor\",\"type\":\"Person\",\"preferredUsername\":\"alice\",\"inbox\":\"https://my-example.com/inbox\",\"publicKey\":{\"id\":\"https://my-example.com/actor#main-key\",\"owner\":\"https://my-example.com/actor\",\"publicKeyPem\":\"-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----\"}}"; - - assert_eq!(expected_serialized_actor, serialized_actor); - } -} diff --git a/src/model/like.rs b/src/model/like.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/model/mod.rs b/src/model/mod.rs index 7754fd8..464b35d 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1,4 +1,6 @@ + +pub mod user; +pub mod status; pub mod activity; -pub mod actor; -pub mod webfinger; -pub mod object; +pub mod like; +pub mod relation; diff --git a/src/model/object.rs b/src/model/object.rs deleted file mode 100644 index 882b51a..0000000 --- a/src/model/object.rs +++ /dev/null @@ -1,57 +0,0 @@ -use chrono::{Utc, DateTime}; -use serde::{Serialize, Deserialize}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Object { - id: String, - - #[serde(rename = "type")] - object_type: ObjectType, - - published: DateTime, - - #[serde(rename = "attributedTo")] - attributed_to: String, - - #[serde(rename = "inReplyTo")] - in_reply_to: String, - - content: String, - - to: String, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub enum ObjectType { - Note, -} - -#[cfg(test)] -impl Default for Object { - fn default() -> Self { - Object { - id: "https://my-example.com/hello-world".into(), - object_type: ObjectType::Note, - published: DateTime::parse_from_rfc3339("2018-06-23T17:17:11Z").unwrap().into(), - attributed_to: "https://my-example.com/actor".into(), - in_reply_to: "https://mastodon.social/@Gargron/100254678717223630".into(), - content: "

Hello world

".into(), - to: "https://www.w3.org/ns/activitystreams#Public".into(), - } - } -} - -#[cfg(test)] -mod test { - use super::Object; - - #[test] - fn object_serializes_as_expected() { - let object = Object::default(); - - let serialized_object = serde_json::to_string(&object).unwrap(); - let expected_serialized_object = "{\"id\":\"https://my-example.com/hello-world\",\"type\":\"Note\",\"published\":\"2018-06-23T17:17:11Z\",\"attributedTo\":\"https://my-example.com/actor\",\"inReplyTo\":\"https://mastodon.social/@Gargron/100254678717223630\",\"content\":\"

Hello world

\",\"to\":\"https://www.w3.org/ns/activitystreams#Public\"}"; - - assert_eq!(serialized_object, expected_serialized_object); - } -} diff --git a/src/model/relation.rs b/src/model/relation.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/model/status.rs b/src/model/status.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/model/user.rs b/src/model/user.rs new file mode 100644 index 0000000..341695c --- /dev/null +++ b/src/model/user.rs @@ -0,0 +1,26 @@ + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "users")] +pub struct Model { + #[sea_orm(primary_key)] + /// must be user@instance.org, even if local! TODO bad design... + pub id: String, + pub name: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} + +impl crate::activitystream::Object for Model { + fn id(&self) -> Option<&str> { + Some(&self.id) + } + + fn name (&self) -> Option<&str> { + self.name.as_deref() + } +} diff --git a/src/model/vote.rs b/src/model/vote.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/model/webfinger.rs b/src/model/webfinger.rs deleted file mode 100644 index 54c1cad..0000000 --- a/src/model/webfinger.rs +++ /dev/null @@ -1,40 +0,0 @@ -use serde::{Serialize, Deserialize}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Webfinger { - subject: String, - links: Vec, -} - - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct WebfingerLink { - rel: String, - #[serde(rename = "type")] - link_type: String, - href: String, -} - -#[cfg(test)] -mod test { - use super::{Webfinger, WebfingerLink}; - - #[test] - fn webfinger_serializes_as_expected() { - let webfinger = Webfinger { - subject: "acct:alice@my-example.com".into(), - links: vec![ - WebfingerLink { - rel: "self".into(), - link_type: "application/activity+json".into(), - href: "https://my-example.com/actor".into(), - }, - ], - }; - - let serialized_webfinger = serde_json::to_string(&webfinger).unwrap(); - let expected_serialized_webfinger = "{\"subject\":\"acct:alice@my-example.com\",\"links\":[{\"rel\":\"self\",\"type\":\"application/activity+json\",\"href\":\"https://my-example.com/actor\"}]}"; - - assert_eq!(serialized_webfinger, expected_serialized_webfinger); - } -}