2018-11-24 12:44:17 +01:00
|
|
|
use activitypub::{actor::Group, collection::OrderedCollection, Actor, CustomObject, Object};
|
2018-09-27 23:06:40 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2018-05-19 09:39:59 +02:00
|
|
|
use openssl::{
|
|
|
|
hash::MessageDigest,
|
|
|
|
pkey::{PKey, Private},
|
|
|
|
rsa::Rsa,
|
2018-11-24 12:44:17 +01:00
|
|
|
sign::{Signer, Verifier},
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-11-24 12:44:17 +01:00
|
|
|
use reqwest::{
|
|
|
|
header::{HeaderValue, ACCEPT},
|
|
|
|
Client,
|
|
|
|
};
|
|
|
|
use serde_json;
|
|
|
|
use url::Url;
|
2018-06-18 23:50:40 +02:00
|
|
|
use webfinger::*;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
use instance::*;
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::activity_pub::{
|
2018-11-24 12:44:17 +01:00
|
|
|
ap_accept_header,
|
2018-10-20 15:03:59 +02:00
|
|
|
inbox::{Deletable, WithInbox},
|
2018-11-24 12:44:17 +01:00
|
|
|
sign, ActivityStream, ApSignature, Id, IntoId, PublicKey,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-10-20 15:03:59 +02:00
|
|
|
use posts::Post;
|
2018-11-24 12:44:17 +01:00
|
|
|
use safe_string::SafeString;
|
2018-04-24 11:21:39 +02:00
|
|
|
use schema::blogs;
|
2018-10-20 15:03:59 +02:00
|
|
|
use users::User;
|
2018-11-24 12:44:17 +01:00
|
|
|
use {Connection, BASE_URL, USE_HTTPS};
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-06-21 22:39:44 +02:00
|
|
|
pub type CustomGroup = CustomObject<ApSignature, Group>;
|
2018-04-23 12:29:27 +02:00
|
|
|
|
2018-05-18 10:04:40 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize, Deserialize, 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-09-27 23:06:40 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
2018-05-03 21:11:04 +02:00
|
|
|
pub ap_url: String,
|
|
|
|
pub private_key: Option<String>,
|
2018-11-24 12:44:17 +01:00
|
|
|
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>,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub public_key: String,
|
2018-04-23 12:29:27 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:42:17 +02:00
|
|
|
const BLOG_PREFIX: &'static str = "~";
|
|
|
|
|
2018-04-23 12:29:27 +02:00
|
|
|
impl Blog {
|
2018-06-18 15:57:38 +02:00
|
|
|
insert!(blogs, NewBlog);
|
2018-06-18 15:44:23 +02:00
|
|
|
get!(blogs);
|
2018-06-21 23:07:04 +02:00
|
|
|
find_by!(blogs, find_by_ap_url, ap_url as String);
|
|
|
|
find_by!(blogs, find_by_name, actor_id as String, instance_id as i32);
|
2018-04-23 12:29:27 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_instance(&self, conn: &Connection) -> Instance {
|
2018-10-20 08:44:33 +02:00
|
|
|
Instance::get(conn, self.instance_id).expect("Blog::get_instance: instance not found error")
|
2018-06-21 19:53:57 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn list_authors(&self, conn: &Connection) -> Vec<User> {
|
2018-07-18 23:08:49 +02:00
|
|
|
use schema::blog_authors;
|
|
|
|
use schema::users;
|
2018-11-24 12:44:17 +01:00
|
|
|
let authors_ids = blog_authors::table
|
|
|
|
.filter(blog_authors::blog_id.eq(self.id))
|
|
|
|
.select(blog_authors::author_id);
|
|
|
|
users::table
|
|
|
|
.filter(users::id.eq_any(authors_ids))
|
2018-07-18 23:08:49 +02:00
|
|
|
.load::<User>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Blog::list_authors: author loading error")
|
2018-07-18 23:08:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
pub fn find_for_author(conn: &Connection, author: &User) -> Vec<Blog> {
|
2018-06-10 19:55:08 +02:00
|
|
|
use schema::blog_authors;
|
2018-11-24 12:44:17 +01:00
|
|
|
let author_ids = blog_authors::table
|
|
|
|
.filter(blog_authors::author_id.eq(author.id))
|
|
|
|
.select(blog_authors::blog_id);
|
|
|
|
blogs::table
|
|
|
|
.filter(blogs::id.eq_any(author_ids))
|
2018-06-10 19:55:08 +02:00
|
|
|
.load::<Blog>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Blog::find_for_author: blog loading error")
|
2018-06-10 19:55:08 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn find_local(conn: &Connection, name: String) -> Option<Blog> {
|
2018-05-13 19:00:47 +02:00
|
|
|
Blog::find_by_name(conn, name, Instance::local_id(conn))
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn find_by_fqn(conn: &Connection, fqn: String) -> Option<Blog> {
|
2018-11-24 12:44:17 +01:00
|
|
|
if fqn.contains("@") {
|
|
|
|
// remote blog
|
|
|
|
match Instance::find_by_domain(
|
|
|
|
conn,
|
|
|
|
String::from(
|
|
|
|
fqn.split("@")
|
|
|
|
.last()
|
|
|
|
.expect("Blog::find_by_fqn: unreachable"),
|
|
|
|
),
|
|
|
|
) {
|
|
|
|
Some(instance) => match Blog::find_by_name(
|
|
|
|
conn,
|
|
|
|
String::from(
|
|
|
|
fqn.split("@")
|
|
|
|
.nth(0)
|
|
|
|
.expect("Blog::find_by_fqn: unreachable"),
|
|
|
|
),
|
|
|
|
instance.id,
|
|
|
|
) {
|
|
|
|
Some(u) => Some(u),
|
|
|
|
None => Blog::fetch_from_webfinger(conn, fqn),
|
2018-05-13 19:00:47 +02:00
|
|
|
},
|
2018-11-24 12:44:17 +01:00
|
|
|
None => Blog::fetch_from_webfinger(conn, fqn),
|
2018-05-13 19:00:47 +02:00
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
} else {
|
|
|
|
// local blog
|
2018-05-13 19:00:47 +02:00
|
|
|
Blog::find_local(conn, fqn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
fn fetch_from_webfinger(conn: &Connection, acct: String) -> Option<Blog> {
|
2018-06-26 16:16:59 +02:00
|
|
|
match resolve(acct.clone(), *USE_HTTPS) {
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(wf) => wf
|
|
|
|
.links
|
|
|
|
.into_iter()
|
|
|
|
.find(|l| l.mime_type == Some(String::from("application/activity+json")))
|
|
|
|
.and_then(|l| {
|
|
|
|
Blog::fetch_from_url(
|
|
|
|
conn,
|
|
|
|
l.href
|
|
|
|
.expect("Blog::fetch_from_webfinger: href not found error"),
|
|
|
|
)
|
|
|
|
}),
|
2018-05-13 19:00:47 +02:00
|
|
|
Err(details) => {
|
2018-06-18 23:50:40 +02:00
|
|
|
println!("{:?}", details);
|
2018-05-13 19:00:47 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
fn fetch_from_url(conn: &Connection, url: String) -> Option<Blog> {
|
2018-05-13 19:00:47 +02:00
|
|
|
let req = Client::new()
|
|
|
|
.get(&url[..])
|
2018-11-24 12:44:17 +01:00
|
|
|
.header(
|
|
|
|
ACCEPT,
|
|
|
|
HeaderValue::from_str(
|
|
|
|
&ap_accept_header()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "),
|
|
|
|
).expect("Blog::fetch_from_url: accept_header generation error"),
|
|
|
|
)
|
2018-05-13 19:00:47 +02:00
|
|
|
.send();
|
|
|
|
match req {
|
|
|
|
Ok(mut res) => {
|
2018-11-24 12:44:17 +01:00
|
|
|
let text = &res
|
|
|
|
.text()
|
|
|
|
.expect("Blog::fetch_from_url: body reading error");
|
|
|
|
let ap_sign: ApSignature =
|
|
|
|
serde_json::from_str(text).expect("Blog::fetch_from_url: body parsing error");
|
|
|
|
let mut json: CustomGroup =
|
|
|
|
serde_json::from_str(text).expect("Blog::fetch_from_url: body parsing error");
|
2018-06-23 16:00:35 +02:00
|
|
|
json.custom_props = ap_sign; // without this workaround, publicKey is not correctly deserialized
|
2018-11-24 12:44:17 +01:00
|
|
|
Some(Blog::from_activity(
|
|
|
|
conn,
|
|
|
|
json,
|
|
|
|
Url::parse(url.as_ref())
|
|
|
|
.expect("Blog::fetch_from_url: url parsing error")
|
|
|
|
.host_str()
|
|
|
|
.expect("Blog::fetch_from_url: host extraction error")
|
|
|
|
.to_string(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Err(_) => None,
|
2018-05-13 19:00:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
fn from_activity(conn: &Connection, acct: CustomGroup, inst: String) -> Blog {
|
2018-05-13 19:00:47 +02:00
|
|
|
let instance = match Instance::find_by_domain(conn, inst.clone()) {
|
|
|
|
Some(instance) => instance,
|
|
|
|
None => {
|
2018-11-24 12:44:17 +01:00
|
|
|
Instance::insert(
|
|
|
|
conn,
|
|
|
|
NewInstance {
|
|
|
|
public_domain: inst.clone(),
|
|
|
|
name: inst.clone(),
|
|
|
|
local: false,
|
|
|
|
// We don't really care about all the following for remote instances
|
|
|
|
long_description: SafeString::new(""),
|
|
|
|
short_description: SafeString::new(""),
|
|
|
|
default_license: String::new(),
|
|
|
|
open_registrations: true,
|
|
|
|
short_description_html: String::new(),
|
|
|
|
long_description_html: String::new(),
|
|
|
|
},
|
|
|
|
)
|
2018-05-13 19:00:47 +02:00
|
|
|
}
|
|
|
|
};
|
2018-11-24 12:44:17 +01:00
|
|
|
Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog {
|
|
|
|
actor_id: acct
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.preferred_username_string()
|
|
|
|
.expect("Blog::from_activity: preferredUsername error"),
|
|
|
|
title: acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.name_string()
|
|
|
|
.expect("Blog::from_activity: name error"),
|
|
|
|
outbox_url: acct
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.outbox_string()
|
|
|
|
.expect("Blog::from_activity: outbox error"),
|
|
|
|
inbox_url: acct
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.inbox_string()
|
|
|
|
.expect("Blog::from_activity: inbox error"),
|
|
|
|
summary: acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.summary_string()
|
|
|
|
.expect("Blog::from_activity: summary error"),
|
|
|
|
instance_id: instance.id,
|
|
|
|
ap_url: acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.id_string()
|
|
|
|
.expect("Blog::from_activity: id error"),
|
|
|
|
public_key: acct
|
|
|
|
.custom_props
|
|
|
|
.public_key_publickey()
|
|
|
|
.expect("Blog::from_activity: publicKey error")
|
|
|
|
.public_key_pem_string()
|
|
|
|
.expect("Blog::from_activity: publicKey.publicKeyPem error"),
|
|
|
|
private_key: None,
|
|
|
|
},
|
|
|
|
)
|
2018-05-13 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn into_activity(&self, _conn: &Connection) -> CustomGroup {
|
2018-06-21 23:07:04 +02:00
|
|
|
let mut blog = Group::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
blog.ap_actor_props
|
|
|
|
.set_preferred_username_string(self.actor_id.clone())
|
|
|
|
.expect("Blog::into_activity: preferredUsername error");
|
|
|
|
blog.object_props
|
|
|
|
.set_name_string(self.title.clone())
|
|
|
|
.expect("Blog::into_activity: name error");
|
|
|
|
blog.ap_actor_props
|
|
|
|
.set_outbox_string(self.outbox_url.clone())
|
|
|
|
.expect("Blog::into_activity: outbox error");
|
|
|
|
blog.ap_actor_props
|
|
|
|
.set_inbox_string(self.inbox_url.clone())
|
|
|
|
.expect("Blog::into_activity: inbox error");
|
|
|
|
blog.object_props
|
|
|
|
.set_summary_string(self.summary.clone())
|
|
|
|
.expect("Blog::into_activity: summary error");
|
|
|
|
blog.object_props
|
|
|
|
.set_id_string(self.ap_url.clone())
|
|
|
|
.expect("Blog::into_activity: id error");
|
2018-06-21 23:07:04 +02:00
|
|
|
|
|
|
|
let mut public_key = PublicKey::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
public_key
|
|
|
|
.set_id_string(format!("{}#main-key", self.ap_url))
|
|
|
|
.expect("Blog::into_activity: publicKey.id error");
|
|
|
|
public_key
|
|
|
|
.set_owner_string(self.ap_url.clone())
|
|
|
|
.expect("Blog::into_activity: publicKey.owner error");
|
|
|
|
public_key
|
|
|
|
.set_public_key_pem_string(self.public_key.clone())
|
|
|
|
.expect("Blog::into_activity: publicKey.publicKeyPem error");
|
2018-06-21 23:07:04 +02:00
|
|
|
let mut ap_signature = ApSignature::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
ap_signature
|
|
|
|
.set_public_key_publickey(public_key)
|
|
|
|
.expect("Blog::into_activity: publicKey error");
|
2018-06-21 23:07:04 +02:00
|
|
|
|
|
|
|
CustomGroup::new(blog, ap_signature)
|
2018-06-21 19:09:18 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn update_boxes(&self, conn: &Connection) {
|
2018-06-21 19:42:17 +02:00
|
|
|
let instance = self.get_instance(conn);
|
2018-04-23 15:12:59 +02:00
|
|
|
if self.outbox_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
2018-11-24 12:44:17 +01:00
|
|
|
.set(blogs::outbox_url.eq(instance.compute_box(
|
|
|
|
BLOG_PREFIX,
|
|
|
|
self.actor_id.clone(),
|
|
|
|
"outbox",
|
|
|
|
)))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("Blog::update_boxes: outbox update error");
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.inbox_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
2018-11-24 12:44:17 +01:00
|
|
|
.set(blogs::inbox_url.eq(instance.compute_box(
|
|
|
|
BLOG_PREFIX,
|
|
|
|
self.actor_id.clone(),
|
|
|
|
"inbox",
|
|
|
|
)))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("Blog::update_boxes: inbox update error");
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
2018-05-01 20:02:29 +02:00
|
|
|
|
|
|
|
if self.ap_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
2018-06-21 19:42:17 +02:00
|
|
|
.set(blogs::ap_url.eq(instance.compute_box(BLOG_PREFIX, self.actor_id.clone(), "")))
|
2018-11-24 12:44:17 +01:00
|
|
|
.execute(conn)
|
|
|
|
.expect("Blog::update_boxes: ap_url update error");
|
2018-05-01 20:02:29 +02:00
|
|
|
}
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
2018-04-29 19:49:56 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn outbox(&self, conn: &Connection) -> ActivityStream<OrderedCollection> {
|
2018-05-16 20:20:44 +02:00
|
|
|
let mut coll = OrderedCollection::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
coll.collection_props.items = serde_json::to_value(self.get_activities(conn))
|
|
|
|
.expect("Blog::outbox: activity serialization error");
|
|
|
|
coll.collection_props
|
|
|
|
.set_total_items_u64(self.get_activities(conn).len() as u64)
|
|
|
|
.expect("Blog::outbox: count serialization error");
|
2018-05-16 20:20:44 +02:00
|
|
|
ActivityStream::new(coll)
|
2018-04-29 19:49:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
fn get_activities(&self, _conn: &Connection) -> Vec<serde_json::Value> {
|
2018-04-29 19:49:56 +02:00
|
|
|
vec![]
|
|
|
|
}
|
2018-05-03 21:11:04 +02:00
|
|
|
|
|
|
|
pub fn get_keypair(&self) -> PKey<Private> {
|
2018-11-24 12:44:17 +01:00
|
|
|
PKey::from_rsa(
|
|
|
|
Rsa::private_key_from_pem(
|
|
|
|
self.private_key
|
|
|
|
.clone()
|
|
|
|
.expect("Blog::get_keypair: private key not found error")
|
|
|
|
.as_ref(),
|
|
|
|
).expect("Blog::get_keypair: pem parsing error"),
|
|
|
|
).expect("Blog::get_keypair: private key deserialization error")
|
2018-05-03 21:11:04 +02:00
|
|
|
}
|
2018-06-18 23:50:40 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn webfinger(&self, conn: &Connection) -> Webfinger {
|
2018-06-18 23:50:40 +02:00
|
|
|
Webfinger {
|
2018-11-24 12:44:17 +01:00
|
|
|
subject: format!(
|
|
|
|
"acct:{}@{}",
|
|
|
|
self.actor_id,
|
|
|
|
self.get_instance(conn).public_domain
|
|
|
|
),
|
2018-06-21 16:48:54 +02:00
|
|
|
aliases: vec![self.ap_url.clone()],
|
2018-06-18 23:50:40 +02:00
|
|
|
links: vec![
|
|
|
|
Link {
|
|
|
|
rel: String::from("http://webfinger.net/rel/profile-page"),
|
|
|
|
mime_type: None,
|
2018-07-26 21:35:35 +02:00
|
|
|
href: Some(self.ap_url.clone()),
|
2018-11-24 12:44:17 +01:00
|
|
|
template: None,
|
2018-06-18 23:50:40 +02:00
|
|
|
},
|
|
|
|
Link {
|
|
|
|
rel: String::from("http://schemas.google.com/g/2010#updates-from"),
|
|
|
|
mime_type: Some(String::from("application/atom+xml")),
|
2018-11-24 12:44:17 +01:00
|
|
|
href: Some(self.get_instance(conn).compute_box(
|
|
|
|
BLOG_PREFIX,
|
|
|
|
self.actor_id.clone(),
|
|
|
|
"feed.atom",
|
|
|
|
)),
|
|
|
|
template: None,
|
2018-06-18 23:50:40 +02:00
|
|
|
},
|
|
|
|
Link {
|
|
|
|
rel: String::from("self"),
|
|
|
|
mime_type: Some(String::from("application/activity+json")),
|
2018-07-26 21:35:35 +02:00
|
|
|
href: Some(self.ap_url.clone()),
|
2018-11-24 12:44:17 +01:00
|
|
|
template: None,
|
|
|
|
},
|
|
|
|
],
|
2018-06-18 23:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-21 19:23:01 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn from_url(conn: &Connection, url: String) -> Option<Blog> {
|
2018-06-21 23:07:04 +02:00
|
|
|
Blog::find_by_ap_url(conn, url.clone()).or_else(|| {
|
2018-10-20 08:44:33 +02:00
|
|
|
// The requested blog was not in the DB
|
2018-06-21 23:07:04 +02:00
|
|
|
// We try to fetch it if it is remote
|
2018-11-24 12:44:17 +01:00
|
|
|
if Url::parse(url.as_ref())
|
|
|
|
.expect("Blog::from_url: ap_url parsing error")
|
|
|
|
.host_str()
|
|
|
|
.expect("Blog::from_url: host extraction error") != BASE_URL.as_str()
|
|
|
|
{
|
2018-06-23 13:14:03 +02:00
|
|
|
Blog::fetch_from_url(conn, url)
|
2018-06-21 23:07:04 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2018-06-21 19:23:01 +02:00
|
|
|
}
|
2018-07-25 21:22:42 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_fqn(&self, conn: &Connection) -> String {
|
2018-07-25 21:22:42 +02:00
|
|
|
if self.instance_id == Instance::local_id(conn) {
|
|
|
|
self.actor_id.clone()
|
|
|
|
} else {
|
2018-11-24 12:44:17 +01:00
|
|
|
format!(
|
|
|
|
"{}@{}",
|
|
|
|
self.actor_id,
|
|
|
|
self.get_instance(conn).public_domain
|
|
|
|
)
|
2018-07-25 21:22:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn to_json(&self, conn: &Connection) -> serde_json::Value {
|
2018-10-20 08:44:33 +02:00
|
|
|
let mut json = serde_json::to_value(self).expect("Blog::to_json: serialization error");
|
2018-07-26 19:00:23 +02:00
|
|
|
json["fqn"] = json!(self.get_fqn(conn));
|
2018-07-25 21:22:42 +02:00
|
|
|
json
|
|
|
|
}
|
2018-10-20 15:03:59 +02:00
|
|
|
|
|
|
|
pub fn delete(&self, conn: &Connection) {
|
|
|
|
for post in Post::get_for_blog(conn, &self) {
|
2018-11-24 12:44:17 +01:00
|
|
|
post.delete(conn);
|
2018-10-20 15:03:59 +02:00
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
diesel::delete(self)
|
|
|
|
.execute(conn)
|
|
|
|
.expect("Blog::delete: blog deletion error");
|
2018-10-20 15:03:59 +02:00
|
|
|
}
|
2018-04-23 12:29:27 +02:00
|
|
|
}
|
2018-04-23 14:00:11 +02:00
|
|
|
|
2018-05-19 00:04:30 +02:00
|
|
|
impl IntoId for Blog {
|
|
|
|
fn into_id(self) -> Id {
|
2018-05-18 10:04:40 +02:00
|
|
|
Id::new(self.ap_url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Object for Blog {}
|
|
|
|
impl Actor for Blog {}
|
|
|
|
|
|
|
|
impl WithInbox for Blog {
|
|
|
|
fn get_inbox_url(&self) -> String {
|
|
|
|
self.inbox_url.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_shared_inbox_url(&self) -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
2018-07-18 15:49:13 +02:00
|
|
|
|
|
|
|
fn is_local(&self) -> bool {
|
|
|
|
self.instance_id == 0
|
|
|
|
}
|
2018-05-18 10:04:40 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 21:11:04 +02:00
|
|
|
impl sign::Signer for Blog {
|
2018-06-21 17:25:32 +02:00
|
|
|
fn get_key_id(&self) -> String {
|
2018-06-21 16:48:54 +02:00
|
|
|
format!("{}#main-key", self.ap_url)
|
2018-05-03 21:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sign(&self, to_sign: String) -> Vec<u8> {
|
|
|
|
let key = self.get_keypair();
|
2018-11-24 12:44:17 +01:00
|
|
|
let mut signer =
|
|
|
|
Signer::new(MessageDigest::sha256(), &key).expect("Blog::sign: initialization error");
|
|
|
|
signer
|
|
|
|
.update(to_sign.as_bytes())
|
|
|
|
.expect("Blog::sign: content insertion error");
|
|
|
|
signer
|
|
|
|
.sign_to_vec()
|
|
|
|
.expect("Blog::sign: finalization error")
|
2018-05-03 21:11:04 +02:00
|
|
|
}
|
2018-09-28 23:18:01 +02:00
|
|
|
|
|
|
|
fn verify(&self, data: String, signature: Vec<u8>) -> bool {
|
2018-11-24 12:44:17 +01:00
|
|
|
let key = PKey::from_rsa(
|
|
|
|
Rsa::public_key_from_pem(self.public_key.as_ref())
|
|
|
|
.expect("Blog::verify: pem parsing error"),
|
|
|
|
).expect("Blog::verify: deserialization error");
|
|
|
|
let mut verifier = Verifier::new(MessageDigest::sha256(), &key)
|
|
|
|
.expect("Blog::verify: initialization error");
|
|
|
|
verifier
|
|
|
|
.update(data.as_bytes())
|
|
|
|
.expect("Blog::verify: content insertion error");
|
|
|
|
verifier
|
|
|
|
.verify(&signature)
|
|
|
|
.expect("Blog::verify: finalization error")
|
2018-09-28 23:18:01 +02:00
|
|
|
}
|
2018-05-03 21:11:04 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 15:12:59 +02:00
|
|
|
impl NewBlog {
|
|
|
|
pub fn new_local(
|
|
|
|
actor_id: String,
|
|
|
|
title: String,
|
|
|
|
summary: String,
|
2018-11-24 12:44:17 +01:00
|
|
|
instance_id: i32,
|
2018-04-23 15:12:59 +02:00
|
|
|
) -> 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(""),
|
2018-10-20 08:44:33 +02:00
|
|
|
public_key: String::from_utf8(pub_key).expect("NewBlog::new_local: public key error"),
|
2018-11-24 12:44:17 +01:00
|
|
|
private_key: Some(
|
|
|
|
String::from_utf8(priv_key).expect("NewBlog::new_local: private key error"),
|
|
|
|
),
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
|
|
|
use blog_authors::*;
|
|
|
|
use diesel::Connection;
|
|
|
|
use instance::tests as instance_tests;
|
|
|
|
use tests::db;
|
|
|
|
use users::tests as usersTests;
|
|
|
|
use Connection as Conn;
|
|
|
|
|
|
|
|
pub(crate) fn fill_database(conn: &Conn) -> Vec<Blog> {
|
|
|
|
instance_tests::fill_database(conn);
|
|
|
|
let users = usersTests::fill_database(conn);
|
|
|
|
let blogs = vec![
|
|
|
|
NewBlog::new_local(
|
|
|
|
"BlogName".to_owned(),
|
|
|
|
"Blog name".to_owned(),
|
|
|
|
"This is a small blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
NewBlog::new_local(
|
|
|
|
"MyBlog".to_owned(),
|
|
|
|
"My blog".to_owned(),
|
|
|
|
"Welcome to my blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
NewBlog::new_local(
|
|
|
|
"WhyILikePlume".to_owned(),
|
|
|
|
"Why I like Plume".to_owned(),
|
|
|
|
"In this blog I will explay you why I like Plume so much".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
].into_iter()
|
|
|
|
.map(|nb| Blog::insert(conn, nb))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blogs[0].id,
|
|
|
|
author_id: users[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blogs[0].id,
|
|
|
|
author_id: users[1].id,
|
|
|
|
is_owner: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blogs[1].id,
|
|
|
|
author_id: users[1].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blogs[2].id,
|
|
|
|
author_id: users[2].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
blogs
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_instance() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
fill_database(conn);
|
|
|
|
|
|
|
|
let blog = Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(blog.get_instance(conn).id, Instance::local_id(conn));
|
|
|
|
// TODO add tests for remote instance
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn authors() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let user = usersTests::fill_database(conn);
|
|
|
|
fill_database(conn);
|
|
|
|
|
|
|
|
let blog = vec![
|
|
|
|
Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
|
|
|
"Blog".to_owned(),
|
|
|
|
"Blog".to_owned(),
|
|
|
|
"I've named my blog Blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[0].id,
|
|
|
|
author_id: user[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[0].id,
|
|
|
|
author_id: user[1].id,
|
|
|
|
is_owner: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[1].id,
|
|
|
|
author_id: user[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
blog[0]
|
|
|
|
.list_authors(conn)
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.id == user[0].id)
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
blog[0]
|
|
|
|
.list_authors(conn)
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.id == user[1].id)
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
blog[1]
|
|
|
|
.list_authors(conn)
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.id == user[0].id)
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
!blog[1]
|
|
|
|
.list_authors(conn)
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.id == user[1].id)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
Blog::find_for_author(conn, &user[0])
|
|
|
|
.iter()
|
|
|
|
.any(|b| b.id == blog[0].id)
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
Blog::find_for_author(conn, &user[1])
|
|
|
|
.iter()
|
|
|
|
.any(|b| b.id == blog[0].id)
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
Blog::find_for_author(conn, &user[0])
|
|
|
|
.iter()
|
|
|
|
.any(|b| b.id == blog[1].id)
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
!Blog::find_for_author(conn, &user[1])
|
|
|
|
.iter()
|
|
|
|
.any(|b| b.id == blog[1].id)
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_local() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
fill_database(conn);
|
|
|
|
|
|
|
|
let blog = Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Blog::find_local(conn, "SomeName".to_owned()).unwrap().id,
|
|
|
|
blog.id
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_fqn() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
fill_database(conn);
|
|
|
|
|
|
|
|
let blog = Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(blog.get_fqn(conn), "SomeName");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let blogs = fill_database(conn);
|
|
|
|
|
|
|
|
blogs[0].delete(conn);
|
|
|
|
assert!(Blog::get(conn, blogs[0].id).is_none());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete_via_user() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let user = usersTests::fill_database(conn);
|
|
|
|
fill_database(conn);
|
|
|
|
|
|
|
|
let blog = vec![
|
|
|
|
Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
|
|
|
"Blog".to_owned(),
|
|
|
|
"Blog".to_owned(),
|
|
|
|
"I've named my blog Blog".to_owned(),
|
|
|
|
Instance::local_id(conn),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[0].id,
|
|
|
|
author_id: user[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[0].id,
|
|
|
|
author_id: user[1].id,
|
|
|
|
is_owner: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[1].id,
|
|
|
|
author_id: user[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
user[0].delete(conn);
|
|
|
|
assert!(Blog::get(conn, blog[0].id).is_some());
|
|
|
|
assert!(Blog::get(conn, blog[1].id).is_none());
|
|
|
|
user[1].delete(conn);
|
|
|
|
assert!(Blog::get(conn, blog[0].id).is_none());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|