Plume/src/activity_pub/inbox.rs

67 lines
2.4 KiB
Rust
Raw Normal View History

2018-05-01 16:00:29 +02:00
use diesel::PgConnection;
use serde_json;
2018-05-02 22:44:03 +02:00
use activity_pub::activity;
2018-05-01 20:02:29 +02:00
use activity_pub::actor::Actor;
use activity_pub::sign::*;
2018-05-01 20:02:29 +02:00
use models::blogs::Blog;
use models::follows::{Follow, NewFollow};
2018-05-01 16:00:29 +02:00
use models::posts::{Post, NewPost};
2018-05-01 20:02:29 +02:00
use models::users::User;
2018-05-01 16:00:29 +02:00
2018-05-01 20:02:29 +02:00
pub trait Inbox: Actor + Sized {
2018-05-01 16:00:29 +02:00
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)
}
},
2018-05-01 20:02:29 +02:00
"Follow" => {
2018-05-02 22:44:03 +02:00
let follow_act = activity::Follow::deserialize(act.clone());
2018-05-01 20:02:29 +02:00
let from = User::from_url(conn, act["actor"].as_str().unwrap().to_string()).unwrap();
match User::from_url(conn, act["object"].as_str().unwrap().to_string()) {
2018-05-02 22:44:03 +02:00
Some(u) => self.accept_follow(conn, &from, &u, &follow_act, from.id, u.id),
2018-05-01 20:02:29 +02:00
None => {
2018-05-02 22:44:03 +02:00
let blog = Blog::from_url(conn, follow_act.get_target_id()).unwrap();
self.accept_follow(conn, &from, &blog, &follow_act, from.id, blog.id)
2018-05-01 20:02:29 +02:00
}
};
// TODO: notification
}
2018-05-01 16:00:29 +02:00
x => println!("Received unknow activity type: {}", x)
}
}
2018-05-01 20:02:29 +02:00
fn accept_follow<A: Actor, B: Actor + Signer, T: activity::Activity>(
&self,
conn: &PgConnection,
from: &A,
target: &B,
follow: &T,
from_id: i32,
target_id: i32
) {
2018-05-01 20:02:29 +02:00
Follow::insert(conn, NewFollow {
follower_id: from_id,
following_id: target_id
});
2018-05-02 22:44:03 +02:00
let accept = activity::Accept::new(target, follow, conn);
from.send_to_inbox(conn, target, accept)
2018-05-01 20:02:29 +02:00
}
2018-05-01 16:00:29 +02:00
}