Plume/src/activity_pub/inbox.rs

101 lines
4.5 KiB
Rust
Raw Normal View History

2018-05-01 14:00:29 +00:00
use diesel::PgConnection;
use serde_json;
2018-05-02 20:44:03 +00:00
use activity_pub::activity;
2018-05-01 18:02:29 +00:00
use activity_pub::actor::Actor;
use activity_pub::outbox::broadcast;
use activity_pub::sign::*;
2018-05-01 18:02:29 +00:00
use models::blogs::Blog;
2018-05-10 10:52:56 +00:00
use models::comments::*;
2018-05-10 16:07:23 +00:00
use models::follows::*;
use models::likes::*;
use models::posts::*;
2018-05-01 18:02:29 +00:00
use models::users::User;
2018-05-01 14:00:29 +00:00
2018-05-13 17:39:18 +00:00
pub trait Inbox {
2018-05-01 14:00:29 +00: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 {
2018-05-10 10:52:56 +00:00
blog_id: 0, // TODO
slug: String::from(""), // TODO
title: String::from(""), // TODO
2018-05-01 14:00:29 +00:00
content: act["object"]["content"].as_str().unwrap().to_string(),
published: true,
2018-05-10 10:52:56 +00:00
license: String::from("CC-0"),
ap_url: act["object"]["url"].as_str().unwrap().to_string()
2018-05-01 14:00:29 +00:00
});
},
2018-05-10 10:52:56 +00:00
"Note" => {
let previous_comment = Comment::find_by_ap_url(conn, act["object"]["inReplyTo"].as_str().unwrap().to_string());
2018-05-10 10:52:56 +00:00
Comment::insert(conn, NewComment {
content: act["object"]["content"].as_str().unwrap().to_string(),
spoiler_text: act["object"]["summary"].as_str().unwrap_or("").to_string(),
ap_url: Some(act["object"]["id"].as_str().unwrap().to_string()),
in_response_to_id: previous_comment.clone().map(|c| c.id),
post_id: previous_comment
.map(|c| c.post_id)
.unwrap_or_else(|| Post::find_by_ap_url(conn, act["object"]["inReplyTo"].as_str().unwrap().to_string()).unwrap().id),
2018-05-10 10:52:56 +00:00
author_id: User::from_url(conn, act["actor"].as_str().unwrap().to_string()).unwrap().id,
sensitive: act["object"]["sensitive"].as_bool().unwrap_or(false)
});
}
2018-05-01 14:00:29 +00:00
x => println!("Received a new {}, but didn't saved it", x)
}
},
2018-05-01 18:02:29 +00:00
"Follow" => {
2018-05-02 20:44:03 +00:00
let follow_act = activity::Follow::deserialize(act.clone());
2018-05-01 18:02:29 +00: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 20:44:03 +00:00
Some(u) => self.accept_follow(conn, &from, &u, &follow_act, from.id, u.id),
2018-05-01 18:02:29 +00:00
None => {
2018-05-02 20:44:03 +00: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 18:02:29 +00:00
}
};
}
2018-05-10 16:07:23 +00:00
"Like" => {
let liker = User::from_url(conn, act["actor"].as_str().unwrap().to_string());
let post = Post::find_by_ap_url(conn, act["object"].as_str().unwrap().to_string());
2018-05-10 16:07:23 +00:00
Like::insert(conn, NewLike {
post_id: post.unwrap().id,
2018-05-13 10:44:05 +00:00
user_id: liker.unwrap().id,
ap_url: act["id"].as_str().unwrap().to_string()
2018-05-10 16:07:23 +00:00
});
},
2018-05-13 10:44:05 +00:00
"Undo" => {
match act["object"]["type"].as_str().unwrap() {
"Like" => {
let like = Like::find_by_ap_url(conn, act["object"]["id"].as_str().unwrap().to_string()).unwrap();
like.delete(conn);
}
x => println!("Wanted to Undo a {}, but it is not supported yet", x)
}
},
2018-05-01 14:00:29 +00:00
x => println!("Received unknow activity type: {}", x)
}
}
2018-05-01 18:02:29 +00:00
fn accept_follow<A: Actor + Signer, B: Actor + Clone, T: activity::Activity>(
&self,
conn: &PgConnection,
from: &A,
target: &B,
follow: &T,
from_id: i32,
target_id: i32
) {
2018-05-01 18:02:29 +00:00
Follow::insert(conn, NewFollow {
follower_id: from_id,
following_id: target_id
});
2018-05-02 20:44:03 +00:00
let accept = activity::Accept::new(target, follow, conn);
broadcast(conn, from, accept, vec![target.clone()]);
2018-05-01 18:02:29 +00:00
}
2018-05-01 14:00:29 +00:00
}