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),
|
2018-05-01 21:57:30 +02:00
|
|
|
Accept(Payload),
|
|
|
|
Follow(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"),
|
2018-05-01 21:57:30 +02:00
|
|
|
Activity::Create(_) => String::from("Create"),
|
|
|
|
Activity::Follow(_) => String::from("Follow")
|
2018-05-01 20:02:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
2018-05-01 21:57:30 +02:00
|
|
|
Activity::Create(p) => p.clone(),
|
|
|
|
Activity::Follow(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-05-01 21:57:30 +02:00
|
|
|
|
|
|
|
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))))
|
|
|
|
}
|
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
|
|
|
|