Start filling user outbox

This commit is contained in:
Bat 2018-04-29 21:23:44 +01:00
parent dd9e845e66
commit bde25478e5
6 changed files with 56 additions and 21 deletions

View File

@ -1,26 +1,38 @@
use diesel::PgConnection;
use serde_json; use serde_json;
use activity_pub::actor::Actor; use activity_pub::actor::Actor;
use activity_pub::object::Object; use activity_pub::object::Object;
#[derive(Clone)] #[derive(Clone)]
pub struct Activity {} pub enum Activity {
Create(CreatePayload)
}
impl Activity { impl Activity {
pub fn serialize(&self) -> serde_json::Value { 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<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()))
} }
} }
#[allow(dead_code)] #[derive(Clone)]
pub struct Create<'a, T, U> where T: Actor + 'static, U: Object { pub struct CreatePayload {
by: &'a T, by: serde_json::Value,
object: U object: serde_json::Value
} }
impl<'a, T: Actor, U: Object> Create<'a, T, U> { impl CreatePayload {
#[allow(dead_code)] pub fn new(by: serde_json::Value, obj: serde_json::Value) -> CreatePayload {
pub fn new(by: &T, obj: U) -> Create<T, U> { CreatePayload {
Create {
by: by, by: by,
object: obj object: obj
} }

View File

@ -1,7 +1,7 @@
use diesel::PgConnection; use diesel::PgConnection;
use activity_pub::{activity_pub, ActivityPub, context}; use activity_pub::{activity_pub, ActivityPub, context};
use activity_pub::activity::Create; // use activity_pub::activity::Create;
use activity_pub::object::{Attribuable, Object}; use activity_pub::object::{Attribuable, Object};
use models::instance::Instance; use models::instance::Instance;
@ -66,8 +66,8 @@ pub trait Actor {
) )
} }
fn create<T>(&self, obj: T) -> Create<Self, T> where T: Object + Attribuable, Self: Actor + Sized { // fn create<T>(&self, obj: T) -> Create<Self, T> where T: Object + Attribuable, Self: Actor + Sized {
obj.set_attribution::<Self>(self); // obj.set_attribution::<Self>(self);
Create::<Self, T>::new(self, obj) // Create::<Self, T>::new(self, obj)
} // }
} }

View File

@ -1,6 +1,10 @@
use serde_json;
use activity_pub::actor::Actor; use activity_pub::actor::Actor;
pub trait Object {} pub trait Object {
fn serialize(&self) -> serde_json::Value;
}
pub trait Attribuable { pub trait Attribuable {
fn set_attribution<T>(&self, by: &T) where T: Actor; fn set_attribution<T>(&self, by: &T) where T: Actor;

View File

@ -1,8 +1,12 @@
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
use models::posts::Post;
use models::users::User;
use schema::post_authors; 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 struct PostAuthor {
pub id: i32, pub id: i32,
pub post_id: i32, pub post_id: i32,

View File

@ -1,5 +1,7 @@
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods};
use serde_json;
use activity_pub::object::Object;
use schema::posts; use schema::posts;
#[derive(Queryable, Identifiable)] #[derive(Queryable, Identifiable)]
@ -48,3 +50,9 @@ impl Post {
.into_iter().nth(0) .into_iter().nth(0)
} }
} }
impl Object for Post {
fn serialize(&self) -> serde_json::Value {
json!({})
}
}

View File

@ -1,5 +1,6 @@
use bcrypt; 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::request::{self, FromRequest, Request};
use rocket::outcome::IntoOutcome; use rocket::outcome::IntoOutcome;
@ -9,6 +10,8 @@ use activity_pub::outbox::Outbox;
use activity_pub::webfinger::Webfinger; use activity_pub::webfinger::Webfinger;
use db_conn::DbConn; use db_conn::DbConn;
use models::instance::Instance; use models::instance::Instance;
use models::post_authors::PostAuthor;
use models::posts::Post;
use schema::users; use schema::users;
pub const AUTH_COOKIE: &'static str = "user_id"; pub const AUTH_COOKIE: &'static str = "user_id";
@ -98,11 +101,15 @@ impl User {
} }
pub fn outbox(&self, conn: &PgConnection) -> Outbox { 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<Activity> { fn get_activities(&self, conn: &PgConnection) -> Vec<Activity> {
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::<Post>(conn).unwrap();
posts.into_iter().map(|p| Activity::create(self, p, conn)).collect::<Vec<Activity>>()
} }
} }