Plume/src/activity_pub/activity.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2018-04-30 18:50:35 +02:00
use chrono;
2018-04-29 22:23:44 +02:00
use diesel::PgConnection;
2018-04-29 19:49:56 +02:00
use serde_json;
use activity_pub::actor::Actor;
use activity_pub::object::Object;
2018-04-29 19:49:56 +02:00
#[derive(Clone)]
2018-04-29 22:23:44 +02:00
pub enum Activity {
Create(CreatePayload)
}
2018-04-29 19:49:56 +02:00
impl Activity {
pub fn serialize(&self) -> serde_json::Value {
2018-04-29 22:23:44 +02:00
match self {
Activity::Create(data) => json!({
"type": "Create",
2018-04-30 18:50:35 +02:00
"actor": data.by,
"object": data.object,
"published": data.date.to_rfc3339()
2018-04-29 22:23:44 +02:00
})
}
}
pub fn create<T: Object, U: Actor>(by: &U, obj: T, conn: &PgConnection) -> Activity {
2018-04-30 18:50:35 +02:00
Activity::Create(CreatePayload::new(serde_json::Value::String(by.compute_id(conn)), obj.serialize(conn)))
2018-04-29 19:49:56 +02:00
}
}
2018-04-29 22:23:44 +02:00
#[derive(Clone)]
pub struct CreatePayload {
by: serde_json::Value,
2018-04-30 18:50:35 +02:00
object: serde_json::Value,
date: chrono::DateTime<chrono::Utc>
}
2018-04-29 22:23:44 +02:00
impl CreatePayload {
pub fn new(by: serde_json::Value, obj: serde_json::Value) -> CreatePayload {
CreatePayload {
by: by,
2018-04-30 18:50:35 +02:00
object: obj,
date: chrono::Utc::now()
}
}
}
2018-04-29 17:40:10 +02:00