From dfab0290e4b2ddc73d1593124a2cb3f6643cf04c Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 13 May 2018 18:39:18 +0100 Subject: [PATCH] Shared inbox endpoint --- src/activity_pub/inbox.rs | 2 +- src/main.rs | 1 + src/models/instance.rs | 10 ++++++++++ src/routes/instance.rs | 9 +++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/activity_pub/inbox.rs b/src/activity_pub/inbox.rs index 2eb36ccc..828242c2 100644 --- a/src/activity_pub/inbox.rs +++ b/src/activity_pub/inbox.rs @@ -12,7 +12,7 @@ use models::likes::*; use models::posts::*; use models::users::User; -pub trait Inbox: Actor + Sized { +pub trait Inbox { fn received(&self, conn: &PgConnection, act: serde_json::Value); fn save(&self, conn: &PgConnection, act: serde_json::Value) { diff --git a/src/main.rs b/src/main.rs index 2d16db20..e3b8880e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,6 +67,7 @@ fn main() { routes::instance::index, routes::instance::configure, routes::instance::post_config, + routes::instance::shared_inbox, routes::notifications::notifications, diff --git a/src/models/instance.rs b/src/models/instance.rs index 626a6dcd..4025788f 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -1,7 +1,9 @@ use chrono::NaiveDateTime; use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; +use serde_json; use std::iter::Iterator; +use activity_pub::inbox::Inbox; use models::users::User; use schema::{instances, users}; @@ -81,3 +83,11 @@ impl Instance { .len() > 0 } } + +impl Inbox for Instance { + fn received(&self, conn: &PgConnection, act: serde_json::Value) { + self.save(conn, act.clone()); + + // TODO: add to stream, or whatever needs to be done + } +} diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 6b4bbb45..77cf1d9e 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -4,6 +4,7 @@ use rocket_contrib::Template; use serde_json; use BASE_URL; +use activity_pub::inbox::Inbox; use db_conn::DbConn; use models::posts::Post; use models::users::User; @@ -65,3 +66,11 @@ fn post_config(conn: DbConn, data: Form) -> Redirect { Redirect::to("/users/new") } } + +#[post("/inbox", data = "")] +fn shared_inbox(conn: DbConn, data: String) -> String { + let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap(); + let instance = Instance::get_local(&*conn).unwrap(); + instance.received(&*conn, act); + String::from("") +}