From 0fd63eb886ec551d1a5518512d59d8f8037fc5c2 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 30 Apr 2018 17:50:35 +0100 Subject: [PATCH] Actually serialize posts in outbox --- src/activity_pub/activity.rs | 14 +++++++++----- src/activity_pub/mod.rs | 1 + src/activity_pub/object.rs | 3 ++- src/models/posts.rs | 31 ++++++++++++++++++++++++++++--- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/activity_pub/activity.rs b/src/activity_pub/activity.rs index bbbdb971..9c41e54e 100644 --- a/src/activity_pub/activity.rs +++ b/src/activity_pub/activity.rs @@ -1,3 +1,4 @@ +use chrono; use diesel::PgConnection; use serde_json; @@ -13,28 +14,31 @@ impl Activity { match self { Activity::Create(data) => json!({ "type": "Create", - "by": data.by, - "object": data.object + "actor": data.by, + "object": data.object, + "published": data.date.to_rfc3339() }) } } pub fn create(by: &U, obj: T, conn: &PgConnection) -> Activity { - Activity::Create(CreatePayload::new(serde_json::Value::String(by.compute_id(conn)), obj.serialize())) + Activity::Create(CreatePayload::new(serde_json::Value::String(by.compute_id(conn)), obj.serialize(conn))) } } #[derive(Clone)] pub struct CreatePayload { by: serde_json::Value, - object: serde_json::Value + object: serde_json::Value, + date: chrono::DateTime } impl CreatePayload { pub fn new(by: serde_json::Value, obj: serde_json::Value) -> CreatePayload { CreatePayload { by: by, - object: obj + object: obj, + date: chrono::Utc::now() } } } diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index a3a40c79..089bd7b4 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -13,6 +13,7 @@ pub mod webfinger; pub type ActivityPub = Content; pub const CONTEXT_URL: &'static str = "https://www.w3.org/ns/activitystreams"; +pub const PUBLIC_VISIBILTY: &'static str = "https://www.w3.org/ns/activitystreams#Public"; pub fn context() -> serde_json::Value { json!([ diff --git a/src/activity_pub/object.rs b/src/activity_pub/object.rs index 6cfac0d3..ff5338b8 100644 --- a/src/activity_pub/object.rs +++ b/src/activity_pub/object.rs @@ -1,9 +1,10 @@ +use diesel::PgConnection; use serde_json; use activity_pub::actor::Actor; pub trait Object { - fn serialize(&self) -> serde_json::Value; + fn serialize(&self, conn: &PgConnection) -> serde_json::Value; } pub trait Attribuable { diff --git a/src/models/posts.rs b/src/models/posts.rs index 96d325a6..d9f3711f 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -1,7 +1,12 @@ -use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; +use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl}; +use diesel::dsl::any; use serde_json; +use activity_pub::PUBLIC_VISIBILTY; +use activity_pub::actor::Actor; use activity_pub::object::Object; +use models::users::User; +use models::post_authors::PostAuthor; use schema::posts; #[derive(Queryable, Identifiable)] @@ -49,10 +54,30 @@ impl Post { .expect("Error loading post by slug") .into_iter().nth(0) } + + pub fn get_authors(&self, conn: &PgConnection) -> Vec { + use schema::users; + use schema::post_authors; + let author_list = PostAuthor::belonging_to(self).select(post_authors::author_id); + users::table.filter(users::id.eq(any(author_list))).load::(conn).unwrap() + } } impl Object for Post { - fn serialize(&self) -> serde_json::Value { - json!({}) + fn serialize(&self, conn: &PgConnection) -> serde_json::Value { + json!({ + "type": "Article", + "attributedTo": self.get_authors(conn).into_iter().map(|a| a.compute_id(conn)).collect::>(), + "content": self.content, + // TODO: "image": "image", + // TODO: "preview": "preview", + // TODO: "published": "published", + // TODO: "replies": "replies", + // TODO: "summary": "summary", + "tag": [], + // TODO: "updated": "updated", + // TODO: "url": "url", + "to": [ PUBLIC_VISIBILTY ] + }) } }