From 03df88e1869ccee1540fe270e528b97b63d3d960 Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 1 May 2018 15:00:29 +0100 Subject: [PATCH] WIP: inboxes --- src/activity_pub/inbox.rs | 29 +++++++++++++++++++++++++++++ src/activity_pub/mod.rs | 1 + src/main.rs | 1 + src/models/users.rs | 8 ++++++++ src/routes/user.rs | 9 +++++++++ 5 files changed, 48 insertions(+) create mode 100644 src/activity_pub/inbox.rs diff --git a/src/activity_pub/inbox.rs b/src/activity_pub/inbox.rs new file mode 100644 index 00000000..343aa6b4 --- /dev/null +++ b/src/activity_pub/inbox.rs @@ -0,0 +1,29 @@ +use diesel::PgConnection; +use serde_json; + +use models::posts::{Post, NewPost}; + +pub trait Inbox { + fn received(&self, conn: &PgConnection, act: serde_json::Value); + + fn save(&self, conn: &PgConnection, act: serde_json::Value) { + match act["type"].as_str().unwrap() { + "Create" => { + match act["object"]["type"].as_str().unwrap() { + "Article" => { + Post::insert(conn, NewPost { + blog_id: 0, + slug: String::from(""), + title: String::from(""), + content: act["object"]["content"].as_str().unwrap().to_string(), + published: true, + license: String::from("CC-0") + }); + }, + x => println!("Received a new {}, but didn't saved it", x) + } + }, + x => println!("Received unknow activity type: {}", x) + } + } +} diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index 089bd7b4..03bcf958 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -5,6 +5,7 @@ use serde_json; pub mod activity; pub mod actor; +pub mod inbox; pub mod object; pub mod outbox; pub mod sign; diff --git a/src/main.rs b/src/main.rs index e009dff5..91e81730 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,6 +57,7 @@ fn main() { routes::user::details, routes::user::activity_details, routes::user::outbox, + routes::user::inbox, routes::user::new, routes::user::create, diff --git a/src/models/users.rs b/src/models/users.rs index 369801e5..4e95644e 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -11,6 +11,7 @@ use serde_json; use activity_pub::activity::Activity; use activity_pub::actor::{ActorType, Actor}; +use activity_pub::inbox::Inbox; use activity_pub::outbox::Outbox; use activity_pub::webfinger::{Webfinger, resolve}; use db_conn::DbConn; @@ -226,6 +227,13 @@ impl Actor for User { } } +impl Inbox for User { + fn received(&self, conn: &PgConnection, act: serde_json::Value) { + self.save(conn, act); + // TODO: add to stream or create notification, or whatever needs to be done + } +} + impl Webfinger for User { fn webfinger_subject(&self, conn: &PgConnection) -> String { format!("acct:{}@{}", self.username, self.get_instance(conn).public_domain) diff --git a/src/routes/user.rs b/src/routes/user.rs index b7cc42cf..1aa75002 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use activity_pub::ActivityPub; use activity_pub::actor::Actor; +use activity_pub::inbox::Inbox; use activity_pub::outbox::Outbox; use db_conn::DbConn; use models::instance::Instance; @@ -68,3 +69,11 @@ fn outbox(name: String, conn: DbConn) -> Outbox { let user = User::find_local(&*conn, name).unwrap(); user.outbox(&*conn) } + +#[post("/@//inbox", data = "")] +fn inbox(name: String, conn: DbConn, data: String) -> String { + let user = User::find_local(&*conn, name).unwrap(); + let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap(); + user.received(&*conn, act); + String::from("") +}