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;
|
|
|
|
|
2018-04-24 16:52:47 +02:00
|
|
|
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 {
|
2018-05-01 20:02:29 +02:00
|
|
|
Create(Payload),
|
|
|
|
Accept(Payload)
|
2018-04-29 22:23:44 +02:00
|
|
|
}
|
2018-04-29 19:49:56 +02:00
|
|
|
impl Activity {
|
|
|
|
pub fn serialize(&self) -> serde_json::Value {
|
2018-05-01 20:02:29 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn payload(&self) -> Payload {
|
2018-04-29 22:23:44 +02:00
|
|
|
match self {
|
2018-05-01 20:02:29 +02:00
|
|
|
Activity::Accept(p) => p.clone(),
|
|
|
|
Activity::Create(p) => p.clone()
|
2018-04-29 22:23:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create<T: Object, U: Actor>(by: &U, obj: T, conn: &PgConnection) -> Activity {
|
2018-05-01 20:02:29 +02:00
|
|
|
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)))
|
2018-04-29 19:49:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 22:23:44 +02:00
|
|
|
#[derive(Clone)]
|
2018-05-01 20:02:29 +02:00
|
|
|
pub struct Payload {
|
2018-04-29 22:23:44 +02:00
|
|
|
by: serde_json::Value,
|
2018-04-30 18:50:35 +02:00
|
|
|
object: serde_json::Value,
|
|
|
|
date: chrono::DateTime<chrono::Utc>
|
2018-04-24 16:52:47 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:02:29 +02:00
|
|
|
impl Payload {
|
|
|
|
pub fn new(by: serde_json::Value, obj: serde_json::Value) -> Payload {
|
|
|
|
Payload {
|
2018-04-24 16:52:47 +02:00
|
|
|
by: by,
|
2018-04-30 18:50:35 +02:00
|
|
|
object: obj,
|
|
|
|
date: chrono::Utc::now()
|
2018-04-24 16:52:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-29 17:40:10 +02:00
|
|
|
|