Actually serialize posts in outbox

This commit is contained in:
Bat 2018-04-30 17:50:35 +01:00
parent bde25478e5
commit 0fd63eb886
4 changed files with 40 additions and 9 deletions

View File

@ -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<T: Object, U: Actor>(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<chrono::Utc>
}
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()
}
}
}

View File

@ -13,6 +13,7 @@ pub mod webfinger;
pub type ActivityPub = Content<Json>;
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!([

View File

@ -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 {

View File

@ -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<User> {
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::<User>(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::<Vec<String>>(),
"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 ]
})
}
}