1
0
Fork 0
forked from alemi/upub

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
This commit is contained in:
əlemi 2024-03-16 03:27:36 +01:00
parent e8a7e4e31c
commit 7876b95de5
Signed by: alemi
GPG key ID: A4895B84D311642C
10 changed files with 31 additions and 202 deletions

View file

@ -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\":\"<p>Hello world</p>\",\"to\":\"https://www.w3.org/ns/activitystreams#Public\"}}";
assert_eq!(serialized_activity, expected_serialized_activity);
}
}

View file

@ -1,57 +0,0 @@
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Actor {
#[serde(rename = "@context")]
pub context: Vec<String>, // 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);
}
}

0
src/model/like.rs Normal file
View file

View file

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

View file

@ -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<Utc>,
#[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: "<p>Hello world</p>".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\":\"<p>Hello world</p>\",\"to\":\"https://www.w3.org/ns/activitystreams#Public\"}";
assert_eq!(serialized_object, expected_serialized_object);
}
}

0
src/model/relation.rs Normal file
View file

0
src/model/status.rs Normal file
View file

26
src/model/user.rs Normal file
View file

@ -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<String>,
}
#[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()
}
}

0
src/model/vote.rs Normal file
View file

View file

@ -1,40 +0,0 @@
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Webfinger {
subject: String,
links: Vec<WebfingerLink>,
}
#[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);
}
}