Plume/src/activity_pub/activity.rs
2018-05-01 20:57:30 +01:00

70 lines
1.9 KiB
Rust

use chrono;
use diesel::PgConnection;
use serde_json;
use activity_pub::actor::Actor;
use activity_pub::object::Object;
#[derive(Clone)]
pub enum Activity {
Create(Payload),
Accept(Payload),
Follow(Payload)
}
impl Activity {
pub fn serialize(&self) -> serde_json::Value {
json!({
"type": self.get_type(),
"actor": self.payload().by,
"object": self.payload().object,
"published": self.payload().date.to_rfc3339()
})
}
pub fn get_type(&self) -> String {
match self {
Activity::Accept(_) => String::from("Accept"),
Activity::Create(_) => String::from("Create"),
Activity::Follow(_) => String::from("Follow")
}
}
pub fn payload(&self) -> Payload {
match self {
Activity::Accept(p) => p.clone(),
Activity::Create(p) => p.clone(),
Activity::Follow(p) => p.clone()
}
}
pub fn create<T: Object, U: Actor>(by: &U, obj: T, conn: &PgConnection) -> Activity {
Activity::Create(Payload::new(serde_json::Value::String(by.compute_id(conn)), obj.serialize(conn)))
}
pub fn accept<A: Actor>(by: &A, what: String, conn: &PgConnection) -> Activity {
Activity::Accept(Payload::new(serde_json::Value::String(by.compute_id(conn)), serde_json::Value::String(what)))
}
pub fn follow<A: Actor, B: Actor>(by: &A, obj: &B, conn: &PgConnection) -> Activity {
Activity::Follow(Payload::new(serde_json::Value::String(by.compute_id(conn)), serde_json::Value::String(obj.compute_id(conn))))
}
}
#[derive(Clone)]
pub struct Payload {
by: serde_json::Value,
object: serde_json::Value,
date: chrono::DateTime<chrono::Utc>
}
impl Payload {
pub fn new(by: serde_json::Value, obj: serde_json::Value) -> Payload {
Payload {
by: by,
object: obj,
date: chrono::Utc::now()
}
}
}