2018-05-13 19:00:47 +02:00
|
|
|
use reqwest::Client;
|
|
|
|
use reqwest::header::{Accept, qitem};
|
|
|
|
use reqwest::mime::Mime;
|
|
|
|
use serde_json;
|
|
|
|
use url::Url;
|
2018-04-30 19:46:27 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-04-24 11:21:39 +02:00
|
|
|
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
|
2018-05-03 21:11:04 +02:00
|
|
|
use openssl::hash::MessageDigest;
|
|
|
|
use openssl::pkey::{PKey, Private};
|
|
|
|
use openssl::rsa::Rsa;
|
|
|
|
use openssl::sign::Signer;
|
2018-05-02 23:36:13 +02:00
|
|
|
use std::sync::Arc;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-04-29 19:49:56 +02:00
|
|
|
use activity_pub::activity::Activity;
|
2018-04-24 11:21:39 +02:00
|
|
|
use activity_pub::actor::{Actor, ActorType};
|
2018-04-29 19:49:56 +02:00
|
|
|
use activity_pub::outbox::Outbox;
|
2018-05-03 21:11:04 +02:00
|
|
|
use activity_pub::sign;
|
2018-04-24 10:35:45 +02:00
|
|
|
use activity_pub::webfinger::*;
|
2018-04-24 11:21:39 +02:00
|
|
|
use models::instance::Instance;
|
|
|
|
use schema::blogs;
|
|
|
|
|
2018-04-23 12:29:27 +02:00
|
|
|
|
2018-05-13 16:39:55 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize, Clone)]
|
2018-04-23 12:29:27 +02:00
|
|
|
pub struct Blog {
|
|
|
|
pub id: i32,
|
|
|
|
pub actor_id: String,
|
|
|
|
pub title: String,
|
|
|
|
pub summary: String,
|
|
|
|
pub outbox_url: String,
|
|
|
|
pub inbox_url: String,
|
2018-04-30 19:46:27 +02:00
|
|
|
pub instance_id: i32,
|
2018-05-01 20:02:29 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
2018-05-03 21:11:04 +02:00
|
|
|
pub ap_url: String,
|
|
|
|
pub private_key: Option<String>,
|
|
|
|
pub public_key: String
|
2018-04-23 12:29:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "blogs"]
|
|
|
|
pub struct NewBlog {
|
|
|
|
pub actor_id: String,
|
|
|
|
pub title: String,
|
|
|
|
pub summary: String,
|
|
|
|
pub outbox_url: String,
|
|
|
|
pub inbox_url: String,
|
2018-05-01 20:02:29 +02:00
|
|
|
pub instance_id: i32,
|
2018-05-03 21:11:04 +02:00
|
|
|
pub ap_url: String,
|
|
|
|
pub private_key: Option<String>,
|
|
|
|
pub public_key: String
|
2018-04-23 12:29:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Blog {
|
|
|
|
pub fn insert (conn: &PgConnection, new: NewBlog) -> Blog {
|
|
|
|
diesel::insert_into(blogs::table)
|
|
|
|
.values(new)
|
|
|
|
.get_result(conn)
|
|
|
|
.expect("Error saving new blog")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(conn: &PgConnection, id: i32) -> Option<Blog> {
|
|
|
|
blogs::table.filter(blogs::id.eq(id))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Blog>(conn)
|
|
|
|
.expect("Error loading blog by id")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
|
2018-05-13 19:00:47 +02:00
|
|
|
pub fn find_by_name(conn: &PgConnection, name: String, instance_id: i32) -> Option<Blog> {
|
|
|
|
blogs::table.filter(blogs::actor_id.eq(name))
|
|
|
|
.filter(blogs::instance_id.eq(instance_id))
|
2018-04-23 12:29:27 +02:00
|
|
|
.limit(1)
|
|
|
|
.load::<Blog>(conn)
|
2018-05-13 19:00:47 +02:00
|
|
|
.expect("Error loading blog by name")
|
2018-04-23 12:29:27 +02:00
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
2018-04-23 15:12:59 +02:00
|
|
|
|
2018-05-13 19:00:47 +02:00
|
|
|
pub fn find_local(conn: &PgConnection, name: String) -> Option<Blog> {
|
|
|
|
Blog::find_by_name(conn, name, Instance::local_id(conn))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_fqn(conn: &PgConnection, fqn: String) -> Option<Blog> {
|
|
|
|
if fqn.contains("@") { // remote blog
|
|
|
|
match Instance::find_by_domain(conn, String::from(fqn.split("@").last().unwrap())) {
|
|
|
|
Some(instance) => {
|
|
|
|
match Blog::find_by_name(conn, String::from(fqn.split("@").nth(0).unwrap()), instance.id) {
|
|
|
|
Some(u) => Some(u),
|
|
|
|
None => Blog::fetch_from_webfinger(conn, fqn)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => Blog::fetch_from_webfinger(conn, fqn)
|
|
|
|
}
|
|
|
|
} else { // local blog
|
|
|
|
Blog::find_local(conn, fqn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_from_webfinger(conn: &PgConnection, acct: String) -> Option<Blog> {
|
|
|
|
match resolve(acct.clone()) {
|
|
|
|
Ok(url) => Blog::fetch_from_url(conn, url),
|
|
|
|
Err(details) => {
|
|
|
|
println!("{}", details);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_from_url(conn: &PgConnection, url: String) -> Option<Blog> {
|
|
|
|
let req = Client::new()
|
|
|
|
.get(&url[..])
|
|
|
|
.header(Accept(vec![qitem("application/activity+json".parse::<Mime>().unwrap())]))
|
|
|
|
.send();
|
|
|
|
match req {
|
|
|
|
Ok(mut res) => {
|
|
|
|
let json: serde_json::Value = serde_json::from_str(&res.text().unwrap()).unwrap();
|
|
|
|
Some(Blog::from_activity(conn, json, Url::parse(url.as_ref()).unwrap().host_str().unwrap().to_string()))
|
|
|
|
},
|
|
|
|
Err(_) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_activity(conn: &PgConnection, acct: serde_json::Value, inst: String) -> Blog {
|
|
|
|
let instance = match Instance::find_by_domain(conn, inst.clone()) {
|
|
|
|
Some(instance) => instance,
|
|
|
|
None => {
|
|
|
|
Instance::insert(conn, inst.clone(), inst.clone(), false)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Blog::insert(conn, NewBlog {
|
|
|
|
actor_id: acct["preferredUsername"].as_str().unwrap().to_string(),
|
|
|
|
title: acct["name"].as_str().unwrap().to_string(),
|
|
|
|
outbox_url: acct["outbox"].as_str().unwrap().to_string(),
|
|
|
|
inbox_url: acct["inbox"].as_str().unwrap().to_string(),
|
|
|
|
summary: acct["summary"].as_str().unwrap().to_string(),
|
|
|
|
instance_id: instance.id,
|
|
|
|
ap_url: acct["id"].as_str().unwrap().to_string(),
|
|
|
|
public_key: acct["publicKey"]["publicKeyPem"].as_str().unwrap_or("").to_string(),
|
|
|
|
private_key: None
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-23 15:12:59 +02:00
|
|
|
pub fn update_boxes(&self, conn: &PgConnection) {
|
|
|
|
if self.outbox_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(blogs::outbox_url.eq(self.compute_outbox(conn)))
|
|
|
|
.get_result::<Blog>(conn).expect("Couldn't update outbox URL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.inbox_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(blogs::inbox_url.eq(self.compute_inbox(conn)))
|
|
|
|
.get_result::<Blog>(conn).expect("Couldn't update inbox URL");
|
|
|
|
}
|
2018-05-01 20:02:29 +02:00
|
|
|
|
|
|
|
if self.ap_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(blogs::ap_url.eq(self.compute_id(conn)))
|
|
|
|
.get_result::<Blog>(conn).expect("Couldn't update AP URL");
|
|
|
|
}
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
2018-04-29 19:49:56 +02:00
|
|
|
|
2018-05-02 23:36:13 +02:00
|
|
|
pub fn outbox(&self, conn: &PgConnection) -> Outbox {
|
2018-04-29 19:49:56 +02:00
|
|
|
Outbox::new(self.compute_outbox(conn), self.get_activities(conn))
|
|
|
|
}
|
|
|
|
|
2018-05-02 23:36:13 +02:00
|
|
|
fn get_activities(&self, _conn: &PgConnection) -> Vec<Arc<Activity>> {
|
2018-04-29 19:49:56 +02:00
|
|
|
vec![]
|
|
|
|
}
|
2018-05-03 21:11:04 +02:00
|
|
|
|
|
|
|
pub fn get_keypair(&self) -> PKey<Private> {
|
|
|
|
PKey::from_rsa(Rsa::private_key_from_pem(self.private_key.clone().unwrap().as_ref()).unwrap()).unwrap()
|
|
|
|
}
|
2018-04-23 12:29:27 +02:00
|
|
|
}
|
2018-04-23 14:00:11 +02:00
|
|
|
|
|
|
|
impl Actor for Blog {
|
|
|
|
fn get_box_prefix() -> &'static str {
|
|
|
|
"~"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_actor_id(&self) -> String {
|
|
|
|
self.actor_id.to_string()
|
|
|
|
}
|
|
|
|
|
2018-05-03 17:34:16 +02:00
|
|
|
fn get_display_name(&self) -> String {
|
|
|
|
self.title.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_summary(&self) -> String {
|
|
|
|
self.summary.clone()
|
|
|
|
}
|
|
|
|
|
2018-04-23 14:00:11 +02:00
|
|
|
fn get_instance(&self, conn: &PgConnection) -> Instance {
|
|
|
|
Instance::get(conn, self.instance_id).unwrap()
|
|
|
|
}
|
2018-04-23 18:26:01 +02:00
|
|
|
|
|
|
|
fn get_actor_type () -> ActorType {
|
|
|
|
ActorType::Blog
|
|
|
|
}
|
2018-05-01 20:02:29 +02:00
|
|
|
|
2018-05-05 15:46:06 +02:00
|
|
|
fn get_inbox_url(&self) -> String {
|
|
|
|
self.inbox_url.clone()
|
|
|
|
}
|
|
|
|
|
2018-05-13 16:39:55 +02:00
|
|
|
fn get_shared_inbox_url(&self) -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-05-01 20:02:29 +02:00
|
|
|
fn from_url(conn: &PgConnection, url: String) -> Option<Blog> {
|
|
|
|
blogs::table.filter(blogs::ap_url.eq(url))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Blog>(conn)
|
|
|
|
.expect("Error loading blog from url")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
2018-04-23 14:00:11 +02:00
|
|
|
}
|
2018-04-23 15:12:59 +02:00
|
|
|
|
2018-04-24 10:35:45 +02:00
|
|
|
impl Webfinger for Blog {
|
|
|
|
fn webfinger_subject(&self, conn: &PgConnection) -> String {
|
|
|
|
format!("acct:{}@{}", self.actor_id, self.get_instance(conn).public_domain)
|
|
|
|
}
|
|
|
|
fn webfinger_aliases(&self, conn: &PgConnection) -> Vec<String> {
|
|
|
|
vec![self.compute_id(conn)]
|
|
|
|
}
|
|
|
|
fn webfinger_links(&self, conn: &PgConnection) -> Vec<Vec<(String, String)>> {
|
|
|
|
vec![
|
|
|
|
vec![
|
|
|
|
(String::from("rel"), String::from("http://webfinger.net/rel/profile-page")),
|
|
|
|
(String::from("href"), self.compute_id(conn))
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
(String::from("rel"), String::from("http://schemas.google.com/g/2010#updates-from")),
|
|
|
|
(String::from("type"), String::from("application/atom+xml")),
|
|
|
|
(String::from("href"), self.compute_box(conn, "feed.atom"))
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
(String::from("rel"), String::from("self")),
|
|
|
|
(String::from("type"), String::from("application/activity+json")),
|
|
|
|
(String::from("href"), self.compute_id(conn))
|
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 21:11:04 +02:00
|
|
|
impl sign::Signer for Blog {
|
|
|
|
fn get_key_id(&self, conn: &PgConnection) -> String {
|
|
|
|
format!("{}#main-key", self.compute_id(conn))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sign(&self, to_sign: String) -> Vec<u8> {
|
|
|
|
let key = self.get_keypair();
|
|
|
|
let mut signer = Signer::new(MessageDigest::sha256(), &key).unwrap();
|
|
|
|
signer.update(to_sign.as_bytes()).unwrap();
|
|
|
|
signer.sign_to_vec().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 15:12:59 +02:00
|
|
|
impl NewBlog {
|
|
|
|
pub fn new_local(
|
|
|
|
actor_id: String,
|
|
|
|
title: String,
|
|
|
|
summary: String,
|
|
|
|
instance_id: i32
|
|
|
|
) -> NewBlog {
|
2018-05-03 21:11:04 +02:00
|
|
|
let (pub_key, priv_key) = sign::gen_keypair();
|
2018-04-23 15:12:59 +02:00
|
|
|
NewBlog {
|
|
|
|
actor_id: actor_id,
|
|
|
|
title: title,
|
|
|
|
summary: summary,
|
|
|
|
outbox_url: String::from(""),
|
|
|
|
inbox_url: String::from(""),
|
2018-05-01 20:02:29 +02:00
|
|
|
instance_id: instance_id,
|
2018-05-03 21:11:04 +02:00
|
|
|
ap_url: String::from(""),
|
|
|
|
public_key: String::from_utf8(pub_key).unwrap(),
|
|
|
|
private_key: Some(String::from_utf8(priv_key).unwrap())
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|