From bde25478e5afc630c8990cab17498efde294ace8 Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 29 Apr 2018 21:23:44 +0100 Subject: [PATCH] Start filling user outbox --- src/activity_pub/activity.rs | 32 ++++++++++++++++++++++---------- src/activity_pub/actor.rs | 10 +++++----- src/activity_pub/object.rs | 6 +++++- src/models/post_authors.rs | 6 +++++- src/models/posts.rs | 8 ++++++++ src/models/users.rs | 15 +++++++++++---- 6 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/activity_pub/activity.rs b/src/activity_pub/activity.rs index 458cc2f..bbbdb97 100644 --- a/src/activity_pub/activity.rs +++ b/src/activity_pub/activity.rs @@ -1,26 +1,38 @@ +use diesel::PgConnection; use serde_json; use activity_pub::actor::Actor; use activity_pub::object::Object; #[derive(Clone)] -pub struct Activity {} +pub enum Activity { + Create(CreatePayload) +} impl Activity { pub fn serialize(&self) -> serde_json::Value { - json!({}) + match self { + Activity::Create(data) => json!({ + "type": "Create", + "by": data.by, + "object": data.object + }) + } + } + + pub fn create(by: &U, obj: T, conn: &PgConnection) -> Activity { + Activity::Create(CreatePayload::new(serde_json::Value::String(by.compute_id(conn)), obj.serialize())) } } -#[allow(dead_code)] -pub struct Create<'a, T, U> where T: Actor + 'static, U: Object { - by: &'a T, - object: U +#[derive(Clone)] +pub struct CreatePayload { + by: serde_json::Value, + object: serde_json::Value } -impl<'a, T: Actor, U: Object> Create<'a, T, U> { - #[allow(dead_code)] - pub fn new(by: &T, obj: U) -> Create { - Create { +impl CreatePayload { + pub fn new(by: serde_json::Value, obj: serde_json::Value) -> CreatePayload { + CreatePayload { by: by, object: obj } diff --git a/src/activity_pub/actor.rs b/src/activity_pub/actor.rs index ec54dd5..0503117 100644 --- a/src/activity_pub/actor.rs +++ b/src/activity_pub/actor.rs @@ -1,7 +1,7 @@ use diesel::PgConnection; use activity_pub::{activity_pub, ActivityPub, context}; -use activity_pub::activity::Create; +// use activity_pub::activity::Create; use activity_pub::object::{Attribuable, Object}; use models::instance::Instance; @@ -66,8 +66,8 @@ pub trait Actor { ) } - fn create(&self, obj: T) -> Create where T: Object + Attribuable, Self: Actor + Sized { - obj.set_attribution::(self); - Create::::new(self, obj) - } + // fn create(&self, obj: T) -> Create where T: Object + Attribuable, Self: Actor + Sized { + // obj.set_attribution::(self); + // Create::::new(self, obj) + // } } diff --git a/src/activity_pub/object.rs b/src/activity_pub/object.rs index db2aaae..6cfac0d 100644 --- a/src/activity_pub/object.rs +++ b/src/activity_pub/object.rs @@ -1,6 +1,10 @@ +use serde_json; + use activity_pub::actor::Actor; -pub trait Object {} +pub trait Object { + fn serialize(&self) -> serde_json::Value; +} pub trait Attribuable { fn set_attribution(&self, by: &T) where T: Actor; diff --git a/src/models/post_authors.rs b/src/models/post_authors.rs index 783b2e0..1220deb 100644 --- a/src/models/post_authors.rs +++ b/src/models/post_authors.rs @@ -1,8 +1,12 @@ use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; +use models::posts::Post; +use models::users::User; use schema::post_authors; -#[derive(Queryable, Identifiable)] +#[derive(Queryable, Identifiable, Associations)] +#[belongs_to(Post)] +#[belongs_to(User, foreign_key = "author_id")] pub struct PostAuthor { pub id: i32, pub post_id: i32, diff --git a/src/models/posts.rs b/src/models/posts.rs index ba94d8d..96d325a 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -1,5 +1,7 @@ use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; +use serde_json; +use activity_pub::object::Object; use schema::posts; #[derive(Queryable, Identifiable)] @@ -48,3 +50,9 @@ impl Post { .into_iter().nth(0) } } + +impl Object for Post { + fn serialize(&self) -> serde_json::Value { + json!({}) + } +} diff --git a/src/models/users.rs b/src/models/users.rs index f09b30b..ac2430e 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -1,5 +1,6 @@ use bcrypt; -use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; +use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, BelongingToDsl, PgConnection}; +use diesel::dsl::any; use rocket::request::{self, FromRequest, Request}; use rocket::outcome::IntoOutcome; @@ -9,6 +10,8 @@ use activity_pub::outbox::Outbox; use activity_pub::webfinger::Webfinger; use db_conn::DbConn; use models::instance::Instance; +use models::post_authors::PostAuthor; +use models::posts::Post; use schema::users; pub const AUTH_COOKIE: &'static str = "user_id"; @@ -98,11 +101,15 @@ impl User { } pub fn outbox(&self, conn: &PgConnection) -> Outbox { - Outbox::new(self.compute_outbox(conn), self.get_activities()) + Outbox::new(self.compute_outbox(conn), self.get_activities(conn)) } - fn get_activities(&self) -> Vec { - vec![] + fn get_activities(&self, conn: &PgConnection) -> Vec { + use schema::posts; + use schema::post_authors; + let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id); + let posts = posts::table.filter(posts::id.eq(any(posts_by_self))).load::(conn).unwrap(); + posts.into_iter().map(|p| Activity::create(self, p, conn)).collect::>() } }