2018-06-10 13:13:07 +02:00
|
|
|
use activitypub::{
|
2018-11-24 12:44:17 +01:00
|
|
|
actor::Person, collection::OrderedCollection, object::Image, Activity, Actor, CustomObject,
|
|
|
|
Endpoint, Object,
|
2018-05-16 20:20:44 +02:00
|
|
|
};
|
2018-04-24 11:21:39 +02:00
|
|
|
use bcrypt;
|
2018-11-24 12:44:17 +01:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
|
|
|
use diesel::{self, BelongingToDsl, 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,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
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-09-29 18:34:43 +02:00
|
|
|
inbox::{Deletable, WithInbox},
|
2018-11-24 12:44:17 +01:00
|
|
|
sign::{gen_keypair, Signer},
|
|
|
|
ActivityStream, ApSignature, Id, IntoId, PublicKey,
|
2018-06-23 18:36:11 +02:00
|
|
|
};
|
2018-05-19 09:39:59 +02:00
|
|
|
use reqwest::{
|
2018-11-24 12:44:17 +01:00
|
|
|
header::{HeaderValue, ACCEPT},
|
2018-05-19 09:39:59 +02:00
|
|
|
Client,
|
|
|
|
};
|
|
|
|
use rocket::{
|
2018-11-24 12:44:17 +01:00
|
|
|
outcome::IntoOutcome,
|
2018-05-19 09:39:59 +02:00
|
|
|
request::{self, FromRequest, Request},
|
|
|
|
};
|
2018-05-01 13:48:19 +02:00
|
|
|
use serde_json;
|
2018-12-06 18:54:16 +01:00
|
|
|
use std::cmp::PartialEq;
|
2018-05-01 20:02:29 +02:00
|
|
|
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 blogs::Blog;
|
|
|
|
use db_conn::DbConn;
|
2018-06-23 18:36:11 +02:00
|
|
|
use follows::Follow;
|
|
|
|
use instance::*;
|
2018-09-03 13:17:59 +02:00
|
|
|
use medias::Media;
|
2018-06-23 18:36:11 +02:00
|
|
|
use post_authors::PostAuthor;
|
|
|
|
use posts::Post;
|
2018-06-11 16:05:18 +02:00
|
|
|
use safe_string::SafeString;
|
2018-06-23 18:36:11 +02:00
|
|
|
use schema::users;
|
2018-12-02 17:37:51 +01:00
|
|
|
use search::Searcher;
|
2018-11-24 12:44:17 +01:00
|
|
|
use {ap_url, Connection, BASE_URL, USE_HTTPS};
|
2018-04-23 11:52:44 +02:00
|
|
|
|
2018-06-21 22:30:56 +02:00
|
|
|
pub type CustomPerson = CustomObject<ApSignature, Person>;
|
|
|
|
|
2018-06-20 23:51:47 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize, Deserialize, Clone, Debug)]
|
2018-04-22 20:13:12 +02:00
|
|
|
pub struct User {
|
|
|
|
pub id: i32,
|
|
|
|
pub username: String,
|
|
|
|
pub display_name: String,
|
|
|
|
pub outbox_url: String,
|
|
|
|
pub inbox_url: String,
|
|
|
|
pub is_admin: bool,
|
2018-06-11 16:05:18 +02:00
|
|
|
pub summary: SafeString,
|
2018-04-22 20:13:12 +02:00
|
|
|
pub email: Option<String>,
|
|
|
|
pub hashed_password: Option<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 19:12:01 +02:00
|
|
|
pub ap_url: String,
|
|
|
|
pub private_key: Option<String>,
|
2018-05-13 20:12:27 +02:00
|
|
|
pub public_key: String,
|
2018-07-27 12:53:21 +02:00
|
|
|
pub shared_inbox_url: Option<String>,
|
2018-09-03 13:17:59 +02:00
|
|
|
pub followers_endpoint: String,
|
|
|
|
pub avatar_id: Option<i32>,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub last_fetched_date: NaiveDateTime,
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "users"]
|
|
|
|
pub struct NewUser {
|
|
|
|
pub username: String,
|
|
|
|
pub display_name: String,
|
|
|
|
pub outbox_url: String,
|
|
|
|
pub inbox_url: String,
|
|
|
|
pub is_admin: bool,
|
2018-06-11 16:05:18 +02:00
|
|
|
pub summary: SafeString,
|
2018-04-22 20:13:12 +02:00
|
|
|
pub email: Option<String>,
|
|
|
|
pub hashed_password: Option<String>,
|
2018-05-01 20:02:29 +02:00
|
|
|
pub instance_id: i32,
|
2018-05-03 19:12:01 +02:00
|
|
|
pub ap_url: String,
|
|
|
|
pub private_key: Option<String>,
|
2018-05-13 20:12:27 +02:00
|
|
|
pub public_key: String,
|
2018-07-27 12:53:21 +02:00
|
|
|
pub shared_inbox_url: Option<String>,
|
2018-09-03 13:17:59 +02:00
|
|
|
pub followers_endpoint: String,
|
|
|
|
pub avatar_id: Option<i32>,
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub const AUTH_COOKIE: &str = "user_id";
|
|
|
|
const USER_PREFIX: &str = "@";
|
2018-06-21 19:42:17 +02:00
|
|
|
|
2018-04-22 20:13:12 +02:00
|
|
|
impl User {
|
2018-06-18 15:57:38 +02:00
|
|
|
insert!(users, NewUser);
|
2018-06-20 10:44:56 +02:00
|
|
|
get!(users);
|
2018-11-26 10:21:52 +01:00
|
|
|
find_by!(users, find_by_email, email as &str);
|
|
|
|
find_by!(users, find_by_name, username as &str, instance_id as i32);
|
|
|
|
find_by!(users, find_by_ap_url, ap_url as &str);
|
2018-06-18 15:57:38 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn one_by_instance(conn: &Connection) -> Vec<User> {
|
2018-11-24 12:44:17 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::instance_id.eq_any(users::table.select(users::instance_id).distinct()))
|
2018-09-27 23:06:40 +02:00
|
|
|
.load::<User>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("User::one_by_instance: loading error")
|
2018-09-09 13:19:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-02 17:37:51 +01:00
|
|
|
pub fn delete(&self, conn: &Connection, searcher: &Searcher) {
|
2018-09-29 18:34:43 +02:00
|
|
|
use schema::post_authors;
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
Blog::find_for_author(conn, self)
|
|
|
|
.iter()
|
2018-12-14 23:16:18 +01:00
|
|
|
.filter(|b| b.count_authors(conn) <= 1)
|
2018-12-02 17:37:51 +01:00
|
|
|
.for_each(|b| b.delete(conn, searcher));
|
2018-09-29 18:34:43 +02:00
|
|
|
// delete the posts if they is the only author
|
|
|
|
let all_their_posts_ids: Vec<i32> = post_authors::table
|
|
|
|
.filter(post_authors::author_id.eq(self.id))
|
|
|
|
.select(post_authors::post_id)
|
|
|
|
.load(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("User::delete: post loading error");
|
2018-09-29 18:34:43 +02:00
|
|
|
for post_id in all_their_posts_ids {
|
|
|
|
let has_other_authors = post_authors::table
|
|
|
|
.filter(post_authors::post_id.eq(post_id))
|
|
|
|
.filter(post_authors::author_id.ne(self.id))
|
|
|
|
.count()
|
|
|
|
.load(conn)
|
2018-11-24 12:44:17 +01:00
|
|
|
.expect("User::delete: count author error")
|
2018-11-26 10:21:52 +01:00
|
|
|
.first()
|
2018-11-24 12:44:17 +01:00
|
|
|
.unwrap_or(&0) > &0;
|
2018-09-29 18:34:43 +02:00
|
|
|
if !has_other_authors {
|
2018-11-24 12:44:17 +01:00
|
|
|
Post::get(conn, post_id)
|
|
|
|
.expect("User::delete: post not found error")
|
2018-12-02 17:37:51 +01:00
|
|
|
.delete(&(conn, searcher));
|
2018-09-29 18:34:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
diesel::delete(self)
|
|
|
|
.execute(conn)
|
|
|
|
.expect("User::delete: user deletion error");
|
2018-09-09 12:25:55 +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("User::get_instance: instance not found error")
|
2018-06-21 19:53:57 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn grant_admin_rights(&self, conn: &Connection) {
|
2018-05-13 13:53:58 +02:00
|
|
|
diesel::update(self)
|
|
|
|
.set(users::is_admin.eq(true))
|
2018-09-27 23:06:40 +02:00
|
|
|
.execute(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("User::grand_admin_rights: update error");
|
2018-05-13 13:53:58 +02:00
|
|
|
}
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
pub fn revoke_admin_rights(&self, conn: &Connection) {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(users::is_admin.eq(false))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("User::grand_admin_rights: update error");
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn update(&self, conn: &Connection, name: String, email: String, summary: String) -> User {
|
2018-05-12 17:30:14 +02:00
|
|
|
diesel::update(self)
|
|
|
|
.set((
|
|
|
|
users::display_name.eq(name),
|
|
|
|
users::email.eq(email),
|
|
|
|
users::summary.eq(summary),
|
2018-11-24 12:44:17 +01:00
|
|
|
))
|
|
|
|
.execute(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("User::update: update error");
|
|
|
|
User::get(conn, self.id).expect("User::update: get error")
|
2018-05-12 17:30:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-14 23:16:18 +01:00
|
|
|
pub fn count_local(conn: &Connection) -> i64 {
|
2018-11-24 12:44:17 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::instance_id.eq(Instance::local_id(conn)))
|
2018-12-14 23:16:18 +01:00
|
|
|
.count()
|
|
|
|
.get_result(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("User::count_local: loading error")
|
2018-06-10 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn find_local(conn: &Connection, username: &str) -> Option<User> {
|
2018-05-01 13:48:19 +02:00
|
|
|
User::find_by_name(conn, username, Instance::local_id(conn))
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn find_by_fqn(conn: &Connection, fqn: &str) -> Option<User> {
|
|
|
|
if fqn.contains('@') {
|
2018-11-24 12:44:17 +01:00
|
|
|
// remote user
|
|
|
|
match Instance::find_by_domain(
|
|
|
|
conn,
|
2018-11-26 10:21:52 +01:00
|
|
|
fqn.split('@')
|
|
|
|
.last()
|
|
|
|
.expect("User::find_by_fqn: host error"),
|
2018-11-24 12:44:17 +01:00
|
|
|
) {
|
|
|
|
Some(instance) => match User::find_by_name(
|
|
|
|
conn,
|
2018-11-26 10:21:52 +01:00
|
|
|
fqn.split('@')
|
|
|
|
.nth(0)
|
|
|
|
.expect("User::find_by_fqn: name error")
|
|
|
|
,
|
2018-11-24 12:44:17 +01:00
|
|
|
instance.id,
|
|
|
|
) {
|
|
|
|
Some(u) => Some(u),
|
|
|
|
None => User::fetch_from_webfinger(conn, fqn),
|
2018-05-01 13:48:19 +02:00
|
|
|
},
|
2018-11-24 12:44:17 +01:00
|
|
|
None => User::fetch_from_webfinger(conn, fqn),
|
2018-05-01 13:48:19 +02:00
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
} else {
|
|
|
|
// local user
|
2018-05-01 13:48:19 +02:00
|
|
|
User::find_local(conn, fqn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
fn fetch_from_webfinger(conn: &Connection, acct: &str) -> Option<User> {
|
|
|
|
match resolve(acct.to_owned(), *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| {
|
|
|
|
User::fetch_from_url(
|
|
|
|
conn,
|
2018-11-26 10:21:52 +01:00
|
|
|
&l.href
|
2018-11-24 12:44:17 +01:00
|
|
|
.expect("User::fetch_from_webginfer: href not found error"),
|
|
|
|
)
|
|
|
|
}),
|
2018-05-01 13:48:19 +02:00
|
|
|
Err(details) => {
|
2018-07-26 21:35:35 +02:00
|
|
|
println!("WF Error: {:?}", details);
|
2018-05-01 13:48:19 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
fn fetch(url: &str) -> Option<CustomPerson> {
|
2018-05-01 20:02:29 +02:00
|
|
|
let req = Client::new()
|
2018-11-26 10:21:52 +01:00
|
|
|
.get(url)
|
2018-11-24 12:44:17 +01:00
|
|
|
.header(
|
|
|
|
ACCEPT,
|
|
|
|
HeaderValue::from_str(
|
|
|
|
&ap_accept_header()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "),
|
|
|
|
).expect("User::fetch: accept header error"),
|
|
|
|
)
|
2018-05-01 20:02:29 +02:00
|
|
|
.send();
|
|
|
|
match req {
|
|
|
|
Ok(mut res) => {
|
2018-07-26 22:59:41 +02:00
|
|
|
if let Ok(text) = &res.text() {
|
|
|
|
if let Ok(ap_sign) = serde_json::from_str::<ApSignature>(text) {
|
|
|
|
if let Ok(mut json) = serde_json::from_str::<CustomPerson>(text) {
|
|
|
|
json.custom_props = ap_sign; // without this workaround, publicKey is not correctly deserialized
|
2018-09-03 20:53:20 +02:00
|
|
|
Some(json)
|
2018-11-24 12:44:17 +01:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 16:16:59 +02:00
|
|
|
Err(e) => {
|
2018-07-26 21:35:35 +02:00
|
|
|
println!("User fetch error: {:?}", e);
|
2018-06-26 16:16:59 +02:00
|
|
|
None
|
|
|
|
}
|
2018-05-01 20:02:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn fetch_from_url(conn: &Connection, url: &str) -> Option<User> {
|
2018-12-02 19:07:36 +01:00
|
|
|
User::fetch(url).and_then(|json| {
|
2018-11-24 12:44:17 +01:00
|
|
|
(User::from_activity(
|
|
|
|
conn,
|
2018-11-26 10:21:52 +01:00
|
|
|
&json,
|
|
|
|
Url::parse(url)
|
2018-11-24 12:44:17 +01:00
|
|
|
.expect("User::fetch_from_url: url error")
|
|
|
|
.host_str()
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::fetch_from_url: host error"),
|
2018-12-02 19:07:36 +01:00
|
|
|
).ok())
|
2018-11-24 12:44:17 +01:00
|
|
|
})
|
2018-09-03 20:53:20 +02:00
|
|
|
}
|
|
|
|
|
2018-12-02 19:07:36 +01:00
|
|
|
fn from_activity(conn: &Connection, acct: &CustomPerson, inst: &str) -> Result<User, ()> {
|
2018-11-26 10:21:52 +01:00
|
|
|
let instance = match Instance::find_by_domain(conn, inst) {
|
2018-05-01 13:48:19 +02:00
|
|
|
Some(instance) => instance,
|
|
|
|
None => {
|
2018-11-24 12:44:17 +01:00
|
|
|
Instance::insert(
|
|
|
|
conn,
|
|
|
|
NewInstance {
|
2018-11-26 10:21:52 +01:00
|
|
|
name: inst.to_owned(),
|
|
|
|
public_domain: inst.to_owned(),
|
2018-11-24 12:44:17 +01:00
|
|
|
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-01 13:48:19 +02:00
|
|
|
}
|
|
|
|
};
|
2018-09-03 13:17:59 +02:00
|
|
|
|
2018-12-02 19:07:36 +01:00
|
|
|
if acct.object.ap_actor_props.preferred_username_string()
|
|
|
|
.expect("User::from_activity: preferredUsername error")
|
|
|
|
.contains(&['<', '>', '&', '@', '\'', '"'][..]) {
|
|
|
|
return Err(());
|
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
let user = User::insert(
|
|
|
|
conn,
|
|
|
|
NewUser {
|
|
|
|
username: acct
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.preferred_username_string()
|
2018-12-02 19:07:36 +01:00
|
|
|
.unwrap(),
|
2018-11-24 12:44:17 +01:00
|
|
|
display_name: acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.name_string()
|
|
|
|
.expect("User::from_activity: name error"),
|
|
|
|
outbox_url: acct
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.outbox_string()
|
|
|
|
.expect("User::from_activity: outbox error"),
|
|
|
|
inbox_url: acct
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.inbox_string()
|
|
|
|
.expect("User::from_activity: inbox error"),
|
|
|
|
is_admin: false,
|
|
|
|
summary: SafeString::new(
|
|
|
|
&acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.summary_string()
|
2018-11-26 10:21:52 +01:00
|
|
|
.unwrap_or_default(),
|
2018-11-24 12:44:17 +01:00
|
|
|
),
|
|
|
|
email: None,
|
|
|
|
hashed_password: None,
|
|
|
|
instance_id: instance.id,
|
|
|
|
ap_url: acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.id_string()
|
|
|
|
.expect("User::from_activity: id error"),
|
|
|
|
public_key: acct
|
|
|
|
.custom_props
|
|
|
|
.public_key_publickey()
|
|
|
|
.expect("User::from_activity: publicKey error")
|
|
|
|
.public_key_pem_string()
|
|
|
|
.expect("User::from_activity: publicKey.publicKeyPem error"),
|
|
|
|
private_key: None,
|
|
|
|
shared_inbox_url: acct
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.endpoints_endpoint()
|
|
|
|
.and_then(|e| e.shared_inbox_string())
|
|
|
|
.ok(),
|
|
|
|
followers_endpoint: acct
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.followers_string()
|
|
|
|
.expect("User::from_activity: followers error"),
|
|
|
|
avatar_id: None,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let avatar = Media::save_remote(
|
|
|
|
conn,
|
|
|
|
acct.object
|
|
|
|
.object_props
|
|
|
|
.icon_image()
|
|
|
|
.expect("User::from_activity: icon error")
|
|
|
|
.object_props
|
|
|
|
.url_string()
|
|
|
|
.expect("User::from_activity: icon.url error"),
|
|
|
|
&user,
|
|
|
|
);
|
|
|
|
|
2018-12-02 19:07:36 +01:00
|
|
|
if let Ok(avatar) = avatar {
|
|
|
|
user.set_avatar(conn, avatar.id);
|
|
|
|
}
|
2018-09-03 13:17:59 +02:00
|
|
|
|
2018-12-02 19:07:36 +01:00
|
|
|
Ok(user)
|
2018-05-01 13:48:19 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn refetch(&self, conn: &Connection) {
|
2018-11-26 10:21:52 +01:00
|
|
|
User::fetch(&self.ap_url.clone()).map(|json| {
|
2018-11-24 12:44:17 +01:00
|
|
|
let avatar = Media::save_remote(
|
|
|
|
conn,
|
|
|
|
json.object
|
|
|
|
.object_props
|
|
|
|
.icon_image()
|
|
|
|
.expect("User::refetch: icon error")
|
|
|
|
.object_props
|
|
|
|
.url_string()
|
|
|
|
.expect("User::refetch: icon.url error"),
|
|
|
|
&self,
|
2018-12-02 19:07:36 +01:00
|
|
|
).ok();
|
2018-09-03 20:53:20 +02:00
|
|
|
|
|
|
|
diesel::update(self)
|
|
|
|
.set((
|
2018-11-24 12:44:17 +01:00
|
|
|
users::username.eq(json
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.preferred_username_string()
|
|
|
|
.expect("User::refetch: preferredUsername error")),
|
|
|
|
users::display_name.eq(json
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.name_string()
|
|
|
|
.expect("User::refetch: name error")),
|
|
|
|
users::outbox_url.eq(json
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.outbox_string()
|
|
|
|
.expect("User::refetch: outbox error")),
|
|
|
|
users::inbox_url.eq(json
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.inbox_string()
|
|
|
|
.expect("User::refetch: inbox error")),
|
|
|
|
users::summary.eq(SafeString::new(
|
|
|
|
&json
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.summary_string()
|
2018-11-26 10:21:52 +01:00
|
|
|
.unwrap_or_default(),
|
2018-11-24 12:44:17 +01:00
|
|
|
)),
|
|
|
|
users::followers_endpoint.eq(json
|
|
|
|
.object
|
|
|
|
.ap_actor_props
|
|
|
|
.followers_string()
|
|
|
|
.expect("User::refetch: followers error")),
|
2018-12-02 19:07:36 +01:00
|
|
|
users::avatar_id.eq(avatar.map(|a| a.id)),
|
2018-11-24 12:44:17 +01:00
|
|
|
users::last_fetched_date.eq(Utc::now().naive_utc()),
|
|
|
|
))
|
|
|
|
.execute(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("User::refetch: update error")
|
2018-09-03 20:53:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn hash_pass(pass: &str) -> String {
|
|
|
|
bcrypt::hash(pass, 10).expect("User::hash_pass: hashing error")
|
2018-04-23 11:52:44 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn auth(&self, pass: &str) -> bool {
|
2018-11-24 12:44:17 +01:00
|
|
|
if let Ok(valid) = bcrypt::verify(
|
2018-11-26 10:21:52 +01:00
|
|
|
pass,
|
2018-11-24 12:44:17 +01:00
|
|
|
self.hashed_password
|
|
|
|
.clone()
|
|
|
|
.expect("User::auth: no password error")
|
|
|
|
.as_str(),
|
|
|
|
) {
|
2018-06-25 15:38:39 +02:00
|
|
|
valid
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2018-04-23 11:52:44 +02:00
|
|
|
}
|
2018-04-23 15:12:59 +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-11-26 10:21:52 +01:00
|
|
|
if self.outbox_url.is_empty() {
|
2018-04-23 15:12:59 +02:00
|
|
|
diesel::update(self)
|
2018-11-24 12:44:17 +01:00
|
|
|
.set(users::outbox_url.eq(instance.compute_box(
|
|
|
|
USER_PREFIX,
|
2018-11-26 10:21:52 +01:00
|
|
|
&self.username,
|
2018-11-24 12:44:17 +01:00
|
|
|
"outbox",
|
|
|
|
)))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("User::update_boxes: outbox update error");
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
if self.inbox_url.is_empty() {
|
2018-04-23 15:12:59 +02:00
|
|
|
diesel::update(self)
|
2018-11-24 12:44:17 +01:00
|
|
|
.set(users::inbox_url.eq(instance.compute_box(
|
|
|
|
USER_PREFIX,
|
2018-11-26 10:21:52 +01:00
|
|
|
&self.username,
|
2018-11-24 12:44:17 +01:00
|
|
|
"inbox",
|
|
|
|
)))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("User::update_boxes: inbox update error");
|
2018-05-01 20:02:29 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
if self.ap_url.is_empty() {
|
2018-05-01 20:02:29 +02:00
|
|
|
diesel::update(self)
|
2018-11-26 10:21:52 +01:00
|
|
|
.set(users::ap_url.eq(instance.compute_box(USER_PREFIX, &self.username, "")))
|
2018-11-24 12:44:17 +01:00
|
|
|
.execute(conn)
|
|
|
|
.expect("User::update_boxes: ap_url update error");
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
2018-05-13 20:12:27 +02:00
|
|
|
|
|
|
|
if self.shared_inbox_url.is_none() {
|
|
|
|
diesel::update(self)
|
2018-11-26 10:21:52 +01:00
|
|
|
.set(users::shared_inbox_url.eq(ap_url(&format!(
|
2018-11-24 12:44:17 +01:00
|
|
|
"{}/inbox",
|
|
|
|
Instance::get_local(conn)
|
|
|
|
.expect("User::update_boxes: local instance not found error")
|
|
|
|
.public_domain
|
|
|
|
))))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("User::update_boxes: shared inbox update error");
|
2018-05-13 20:12:27 +02:00
|
|
|
}
|
2018-07-27 12:53:21 +02:00
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
if self.followers_endpoint.is_empty() {
|
2018-07-27 12:53:21 +02:00
|
|
|
diesel::update(self)
|
2018-11-24 12:44:17 +01:00
|
|
|
.set(users::followers_endpoint.eq(instance.compute_box(
|
|
|
|
USER_PREFIX,
|
2018-11-26 10:21:52 +01:00
|
|
|
&self.username,
|
2018-11-24 12:44:17 +01:00
|
|
|
"followers",
|
|
|
|
)))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("User::update_boxes: follower update error");
|
2018-07-27 12:53:21 +02:00
|
|
|
}
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
2018-04-29 20:01:42 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_local_page(conn: &Connection, (min, max): (i32, i32)) -> Vec<User> {
|
2018-11-24 12:44:17 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::instance_id.eq(Instance::local_id(conn)))
|
2018-09-09 12:25:55 +02:00
|
|
|
.order(users::username.asc())
|
|
|
|
.offset(min.into())
|
|
|
|
.limit((max - min).into())
|
|
|
|
.load::<User>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("User::get_local_page: loading error")
|
2018-09-09 12:25:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn outbox(&self, conn: &Connection) -> ActivityStream<OrderedCollection> {
|
2018-05-19 00:04:30 +02:00
|
|
|
let acts = self.get_activities(conn);
|
|
|
|
let n_acts = acts.len();
|
|
|
|
let mut coll = OrderedCollection::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
coll.collection_props.items =
|
|
|
|
serde_json::to_value(acts).expect("User::outbox: activity error");
|
|
|
|
coll.collection_props
|
|
|
|
.set_total_items_u64(n_acts as u64)
|
|
|
|
.expect("User::outbox: count error");
|
2018-05-16 20:20:44 +02:00
|
|
|
ActivityStream::new(coll)
|
2018-04-29 20:01:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 22:23:53 +02:00
|
|
|
pub fn fetch_outbox<T: Activity>(&self) -> Vec<T> {
|
|
|
|
let req = Client::new()
|
|
|
|
.get(&self.outbox_url[..])
|
2018-11-24 12:44:17 +01:00
|
|
|
.header(
|
|
|
|
ACCEPT,
|
|
|
|
HeaderValue::from_str(
|
|
|
|
&ap_accept_header()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "),
|
|
|
|
).expect("User::fetch_outbox: accept header error"),
|
|
|
|
)
|
2018-07-26 22:23:53 +02:00
|
|
|
.send();
|
|
|
|
match req {
|
|
|
|
Ok(mut res) => {
|
2018-10-20 08:44:33 +02:00
|
|
|
let text = &res.text().expect("User::fetch_outbox: body error");
|
2018-11-24 12:44:17 +01:00
|
|
|
let json: serde_json::Value =
|
|
|
|
serde_json::from_str(text).expect("User::fetch_outbox: parsing error");
|
|
|
|
json["items"]
|
|
|
|
.as_array()
|
2018-12-23 11:12:15 +01:00
|
|
|
.unwrap_or(&vec![])
|
2018-07-26 22:23:53 +02:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|j| serde_json::from_value(j.clone()).ok())
|
|
|
|
.collect::<Vec<T>>()
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
2018-07-26 22:23:53 +02:00
|
|
|
Err(e) => {
|
|
|
|
println!("User outbox fetch error: {:?}", e);
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 12:53:21 +02:00
|
|
|
pub fn fetch_followers_ids(&self) -> Vec<String> {
|
|
|
|
let req = Client::new()
|
|
|
|
.get(&self.followers_endpoint[..])
|
2018-11-24 12:44:17 +01:00
|
|
|
.header(
|
|
|
|
ACCEPT,
|
|
|
|
HeaderValue::from_str(
|
|
|
|
&ap_accept_header()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "),
|
|
|
|
).expect("User::fetch_followers_ids: accept header error"),
|
|
|
|
)
|
2018-07-27 12:53:21 +02:00
|
|
|
.send();
|
|
|
|
match req {
|
|
|
|
Ok(mut res) => {
|
2018-10-20 08:44:33 +02:00
|
|
|
let text = &res.text().expect("User::fetch_followers_ids: body error");
|
2018-11-24 12:44:17 +01:00
|
|
|
let json: serde_json::Value =
|
|
|
|
serde_json::from_str(text).expect("User::fetch_followers_ids: parsing error");
|
|
|
|
json["items"]
|
|
|
|
.as_array()
|
2018-12-23 11:12:15 +01:00
|
|
|
.unwrap_or(&vec![])
|
2018-07-27 12:53:21 +02:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|j| serde_json::from_value(j.clone()).ok())
|
|
|
|
.collect::<Vec<String>>()
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
2018-07-27 12:53:21 +02:00
|
|
|
Err(e) => {
|
|
|
|
println!("User followers fetch error: {:?}", e);
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
fn get_activities(&self, conn: &Connection) -> Vec<serde_json::Value> {
|
2018-04-29 22:23:44 +02:00
|
|
|
use schema::post_authors;
|
2018-11-24 12:44:17 +01:00
|
|
|
use schema::posts;
|
2018-04-29 22:23:44 +02:00
|
|
|
let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id);
|
2018-09-10 21:06:00 +02:00
|
|
|
let posts = posts::table
|
|
|
|
.filter(posts::published.eq(true))
|
2018-09-27 23:06:40 +02:00
|
|
|
.filter(posts::id.eq_any(posts_by_self))
|
2018-11-24 12:44:17 +01:00
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("User::get_activities: loading error");
|
|
|
|
posts
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| {
|
|
|
|
serde_json::to_value(p.create_activity(conn))
|
|
|
|
.expect("User::get_activities: creation error")
|
|
|
|
})
|
|
|
|
.collect::<Vec<serde_json::Value>>()
|
2018-04-29 20:01:42 +02:00
|
|
|
}
|
2018-05-01 15:23:23 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_fqn(&self, conn: &Connection) -> String {
|
2018-05-13 19:19:23 +02:00
|
|
|
if self.instance_id == Instance::local_id(conn) {
|
|
|
|
self.username.clone()
|
|
|
|
} else {
|
2018-11-24 12:44:17 +01:00
|
|
|
format!(
|
|
|
|
"{}@{}",
|
|
|
|
self.username,
|
|
|
|
self.get_instance(conn).public_domain
|
|
|
|
)
|
2018-05-13 19:19:23 +02:00
|
|
|
}
|
2018-05-13 13:53:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_followers(&self, conn: &Connection) -> Vec<User> {
|
2018-05-01 15:23:23 +02:00
|
|
|
use schema::follows;
|
|
|
|
let follows = Follow::belonging_to(self).select(follows::follower_id);
|
2018-11-24 12:44:17 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::id.eq_any(follows))
|
|
|
|
.load::<User>(conn)
|
|
|
|
.expect("User::get_followers: loading error")
|
2018-05-01 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
2018-12-14 23:16:18 +01:00
|
|
|
pub fn count_followers(&self, conn: &Connection) -> i64 {
|
|
|
|
use schema::follows;
|
|
|
|
let follows = Follow::belonging_to(self).select(follows::follower_id);
|
|
|
|
users::table
|
|
|
|
.filter(users::id.eq_any(follows))
|
|
|
|
.count()
|
|
|
|
.get_result(conn)
|
|
|
|
.expect("User::count_followers: counting error")
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_followers_page(&self, conn: &Connection, (min, max): (i32, i32)) -> Vec<User> {
|
2018-07-25 15:50:29 +02:00
|
|
|
use schema::follows;
|
|
|
|
let follows = Follow::belonging_to(self).select(follows::follower_id);
|
2018-11-24 12:44:17 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::id.eq_any(follows))
|
2018-07-25 15:50:29 +02:00
|
|
|
.offset(min.into())
|
|
|
|
.limit((max - min).into())
|
2018-11-24 12:44:17 +01:00
|
|
|
.load::<User>(conn)
|
|
|
|
.expect("User::get_followers_page: loading error")
|
2018-07-25 15:50:29 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_following(&self, conn: &Connection) -> Vec<User> {
|
2018-09-27 23:06:40 +02:00
|
|
|
use schema::follows::dsl::*;
|
|
|
|
let f = follows.filter(follower_id.eq(self.id)).select(following_id);
|
2018-11-24 12:44:17 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::id.eq_any(f))
|
|
|
|
.load::<User>(conn)
|
|
|
|
.expect("User::get_following: loading error")
|
2018-05-01 15:23:23 +02:00
|
|
|
}
|
2018-05-03 19:12:01 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn is_followed_by(&self, conn: &Connection, other_id: i32) -> bool {
|
2018-06-13 20:06:14 +02:00
|
|
|
use schema::follows;
|
2018-12-14 23:16:18 +01:00
|
|
|
follows::table
|
2018-06-13 20:06:14 +02:00
|
|
|
.filter(follows::follower_id.eq(other_id))
|
|
|
|
.filter(follows::following_id.eq(self.id))
|
2018-12-14 23:16:18 +01:00
|
|
|
.count()
|
|
|
|
.get_result::<i64>(conn)
|
|
|
|
.expect("User::is_followed_by: loading error") > 0
|
2018-06-13 20:06:14 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn is_following(&self, conn: &Connection, other_id: i32) -> bool {
|
2018-07-20 17:51:32 +02:00
|
|
|
use schema::follows;
|
2018-12-14 23:16:18 +01:00
|
|
|
follows::table
|
2018-07-20 17:51:32 +02:00
|
|
|
.filter(follows::follower_id.eq(self.id))
|
|
|
|
.filter(follows::following_id.eq(other_id))
|
2018-12-14 23:16:18 +01:00
|
|
|
.count()
|
|
|
|
.get_result::<i64>(conn)
|
|
|
|
.expect("User::is_following: loading error") > 0
|
2018-07-20 17:51:32 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn has_liked(&self, conn: &Connection, post: &Post) -> bool {
|
2018-05-12 22:56:57 +02:00
|
|
|
use schema::likes;
|
2018-12-14 23:16:18 +01:00
|
|
|
likes::table
|
2018-05-12 22:56:57 +02:00
|
|
|
.filter(likes::post_id.eq(post.id))
|
|
|
|
.filter(likes::user_id.eq(self.id))
|
2018-12-14 23:16:18 +01:00
|
|
|
.count()
|
|
|
|
.get_result::<i64>(conn)
|
|
|
|
.expect("User::has_liked: loading error") > 0
|
2018-05-12 22:56:57 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn has_reshared(&self, conn: &Connection, post: &Post) -> bool {
|
2018-05-19 11:51:10 +02:00
|
|
|
use schema::reshares;
|
2018-12-14 23:16:18 +01:00
|
|
|
reshares::table
|
2018-05-19 11:51:10 +02:00
|
|
|
.filter(reshares::post_id.eq(post.id))
|
|
|
|
.filter(reshares::user_id.eq(self.id))
|
2018-12-14 23:16:18 +01:00
|
|
|
.count()
|
|
|
|
.get_result::<i64>(conn)
|
|
|
|
.expect("User::has_reshared: loading error") > 0
|
2018-05-19 11:51:10 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn is_author_in(&self, conn: &Connection, blog: &Blog) -> bool {
|
2018-06-10 20:16:25 +02:00
|
|
|
use schema::blog_authors;
|
2018-12-14 23:16:18 +01:00
|
|
|
blog_authors::table
|
2018-11-24 12:44:17 +01:00
|
|
|
.filter(blog_authors::author_id.eq(self.id))
|
2018-06-10 20:16:25 +02:00
|
|
|
.filter(blog_authors::blog_id.eq(blog.id))
|
2018-12-14 23:16:18 +01:00
|
|
|
.count()
|
|
|
|
.get_result::<i64>(conn)
|
|
|
|
.expect("User::is_author_in: loading error") > 0
|
2018-06-10 20:16:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 19:12:01 +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("User::get_keypair: private key not found error")
|
|
|
|
.as_ref(),
|
|
|
|
).expect("User::get_keypair: pem parsing error"),
|
|
|
|
).expect("User::get_keypair: private key deserialization error")
|
2018-05-03 19:12:01 +02:00
|
|
|
}
|
2018-05-19 00:04:30 +02:00
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn to_activity(&self, conn: &Connection) -> CustomPerson {
|
2018-05-19 00:04:30 +02:00
|
|
|
let mut actor = Person::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.object_props
|
|
|
|
.set_id_string(self.ap_url.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: id error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.object_props
|
|
|
|
.set_name_string(self.display_name.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: name error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.object_props
|
|
|
|
.set_summary_string(self.summary.get().clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: summary error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.object_props
|
|
|
|
.set_url_string(self.ap_url.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: url error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.ap_actor_props
|
|
|
|
.set_inbox_string(self.inbox_url.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: inbox error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.ap_actor_props
|
|
|
|
.set_outbox_string(self.outbox_url.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: outbox error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.ap_actor_props
|
|
|
|
.set_preferred_username_string(self.username.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: preferredUsername error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.ap_actor_props
|
|
|
|
.set_followers_string(self.followers_endpoint.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: followers error");
|
2018-06-21 17:14:26 +02:00
|
|
|
|
|
|
|
let mut endpoints = Endpoint::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
endpoints
|
2018-11-26 10:21:52 +01:00
|
|
|
.set_shared_inbox_string(ap_url(&format!("{}/inbox/", BASE_URL.as_str())))
|
|
|
|
.expect("User::to_activity: endpoints.sharedInbox error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.ap_actor_props
|
|
|
|
.set_endpoints_endpoint(endpoints)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: endpoints error");
|
2018-06-21 23:12:24 +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))
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: publicKey.id error");
|
2018-11-24 12:44:17 +01:00
|
|
|
public_key
|
|
|
|
.set_owner_string(self.ap_url.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: publicKey.owner error");
|
2018-11-24 12:44:17 +01:00
|
|
|
public_key
|
|
|
|
.set_public_key_pem_string(self.public_key.clone())
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: publicKey.publicKeyPem error");
|
2018-06-21 23:12:24 +02:00
|
|
|
let mut ap_signature = ApSignature::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
ap_signature
|
|
|
|
.set_public_key_publickey(public_key)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: publicKey error");
|
2018-06-21 23:12:24 +02:00
|
|
|
|
2018-09-03 14:48:34 +02:00
|
|
|
let mut avatar = Image::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
avatar
|
|
|
|
.object_props
|
|
|
|
.set_url_string(
|
|
|
|
self.avatar_id
|
|
|
|
.and_then(|id| Media::get(conn, id).map(|m| m.url(conn)))
|
2018-11-26 10:21:52 +01:00
|
|
|
.unwrap_or_default(),
|
2018-11-24 12:44:17 +01:00
|
|
|
)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: icon.url error");
|
2018-11-24 12:44:17 +01:00
|
|
|
actor
|
|
|
|
.object_props
|
|
|
|
.set_icon_object(avatar)
|
2018-11-26 10:21:52 +01:00
|
|
|
.expect("User::to_activity: icon error");
|
2018-09-03 14:48:34 +02:00
|
|
|
|
2018-06-21 23:12:24 +02:00
|
|
|
CustomPerson::new(actor, ap_signature)
|
2018-05-19 00:04:30 +02:00
|
|
|
}
|
2018-06-18 18:34:29 +02:00
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn avatar_url(&self, conn: &Connection) -> String {
|
|
|
|
self.avatar_id.and_then(|id| Media::get(conn, id).map(|m| m.url(conn))).unwrap_or("/static/default-avatar.png".to_string())
|
2018-06-18 18:34:29 +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.username,
|
|
|
|
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(
|
|
|
|
USER_PREFIX,
|
2018-11-26 10:21:52 +01:00
|
|
|
&self.username,
|
2018-11-24 12:44:17 +01:00
|
|
|
"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-11-26 10:21:52 +01:00
|
|
|
pub fn from_url(conn: &Connection, url: &str) -> Option<User> {
|
|
|
|
User::find_by_ap_url(conn, url).or_else(|| {
|
2018-06-21 19:23:01 +02:00
|
|
|
// The requested user was not in the DB
|
|
|
|
// We try to fetch it if it is remote
|
2018-11-26 10:21:52 +01:00
|
|
|
if Url::parse(&url)
|
2018-11-24 12:44:17 +01:00
|
|
|
.expect("User::from_url: url error")
|
|
|
|
.host_str()
|
|
|
|
.expect("User::from_url: host error") != BASE_URL.as_str()
|
|
|
|
{
|
2018-06-23 13:14:03 +02:00
|
|
|
User::fetch_from_url(conn, url)
|
2018-06-21 19:23:01 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-03 14:04:17 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn set_avatar(&self, conn: &Connection, id: i32) {
|
2018-09-03 14:04:17 +02:00
|
|
|
diesel::update(self)
|
|
|
|
.set(users::avatar_id.eq(id))
|
|
|
|
.execute(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("User::set_avatar: update error");
|
2018-09-03 14:04:17 +02:00
|
|
|
}
|
2018-09-03 20:53:20 +02:00
|
|
|
|
|
|
|
pub fn needs_update(&self) -> bool {
|
|
|
|
(Utc::now().naive_utc() - self.last_fetched_date).num_days() > 1
|
|
|
|
}
|
2018-12-06 18:54:16 +01:00
|
|
|
|
|
|
|
pub fn name(&self, conn: &Connection) -> String {
|
|
|
|
if !self.display_name.is_empty() {
|
|
|
|
self.display_name.clone()
|
|
|
|
} else {
|
|
|
|
self.get_fqn(conn)
|
|
|
|
}
|
|
|
|
}
|
2018-05-19 00:04:30 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 11:52:44 +02:00
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for User {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<User, ()> {
|
|
|
|
let conn = request.guard::<DbConn>()?;
|
2018-11-24 12:44:17 +01:00
|
|
|
request
|
|
|
|
.cookies()
|
2018-04-23 11:52:44 +02:00
|
|
|
.get_private(AUTH_COOKIE)
|
|
|
|
.and_then(|cookie| cookie.value().parse().ok())
|
2018-10-20 08:44:33 +02:00
|
|
|
.map(|id| User::get(&*conn, id).expect("User::from_request: user not found error"))
|
2018-04-23 11:52:44 +02:00
|
|
|
.or_forward(())
|
|
|
|
}
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
2018-04-23 14:01:32 +02:00
|
|
|
|
2018-05-18 10:04:40 +02:00
|
|
|
impl IntoId for User {
|
2018-05-19 00:04:30 +02:00
|
|
|
fn into_id(self) -> Id {
|
2018-05-18 10:04:40 +02:00
|
|
|
Id::new(self.ap_url.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Object for User {}
|
|
|
|
impl Actor for User {}
|
|
|
|
|
|
|
|
impl WithInbox for User {
|
|
|
|
fn get_inbox_url(&self) -> String {
|
|
|
|
self.inbox_url.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_shared_inbox_url(&self) -> Option<String> {
|
2018-11-24 12:44:17 +01:00
|
|
|
self.shared_inbox_url.clone()
|
2018-05-18 10:04:40 +02:00
|
|
|
}
|
2018-07-18 15:49:13 +02:00
|
|
|
|
|
|
|
fn is_local(&self) -> bool {
|
2018-09-04 15:02:01 +02:00
|
|
|
self.instance_id == 1
|
2018-07-18 15:49:13 +02:00
|
|
|
}
|
2018-05-18 10:04:40 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 21:11:04 +02:00
|
|
|
impl Signer for User {
|
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 19:12:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
fn sign(&self, to_sign: &str) -> Vec<u8> {
|
2018-05-03 19:12:01 +02:00
|
|
|
let key = self.get_keypair();
|
2018-11-24 12:44:17 +01:00
|
|
|
let mut signer = sign::Signer::new(MessageDigest::sha256(), &key)
|
|
|
|
.expect("User::sign: initialization error");
|
|
|
|
signer
|
|
|
|
.update(to_sign.as_bytes())
|
|
|
|
.expect("User::sign: content insertion error");
|
|
|
|
signer
|
|
|
|
.sign_to_vec()
|
|
|
|
.expect("User::sign: finalization error")
|
2018-05-03 19:12:01 +02:00
|
|
|
}
|
2018-09-28 23:18:01 +02:00
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
fn verify(&self, data: &str, signature: &[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("User::verify: pem parsing error"),
|
|
|
|
).expect("User::verify: deserialization error");
|
|
|
|
let mut verifier = sign::Verifier::new(MessageDigest::sha256(), &key)
|
|
|
|
.expect("User::verify: initialization error");
|
|
|
|
verifier
|
|
|
|
.update(data.as_bytes())
|
|
|
|
.expect("User::verify: content insertion error");
|
|
|
|
verifier
|
|
|
|
.verify(&signature)
|
|
|
|
.expect("User::verify: finalization error")
|
2018-09-28 23:18:01 +02:00
|
|
|
}
|
2018-05-03 19:12:01 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
impl PartialEq for User {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 15:12:59 +02:00
|
|
|
impl NewUser {
|
|
|
|
/// Creates a new local user
|
|
|
|
pub fn new_local(
|
2018-09-26 17:22:42 +02:00
|
|
|
conn: &Connection,
|
2018-04-23 15:12:59 +02:00
|
|
|
username: String,
|
|
|
|
display_name: String,
|
|
|
|
is_admin: bool,
|
2018-11-26 10:21:52 +01:00
|
|
|
summary: &str,
|
2018-04-23 15:12:59 +02:00
|
|
|
email: String,
|
2018-11-24 12:44:17 +01:00
|
|
|
password: String,
|
2018-06-19 19:29:34 +02:00
|
|
|
) -> User {
|
2018-05-03 21:11:04 +02:00
|
|
|
let (pub_key, priv_key) = gen_keypair();
|
2018-11-24 12:44:17 +01:00
|
|
|
User::insert(
|
|
|
|
conn,
|
|
|
|
NewUser {
|
2018-11-26 10:21:52 +01:00
|
|
|
username,
|
|
|
|
display_name,
|
2018-11-24 12:44:17 +01:00
|
|
|
outbox_url: String::from(""),
|
|
|
|
inbox_url: String::from(""),
|
2018-11-26 10:21:52 +01:00
|
|
|
is_admin,
|
|
|
|
summary: SafeString::new(summary),
|
2018-11-24 12:44:17 +01:00
|
|
|
email: Some(email),
|
|
|
|
hashed_password: Some(password),
|
|
|
|
instance_id: Instance::local_id(conn),
|
|
|
|
ap_url: String::from(""),
|
|
|
|
public_key: String::from_utf8(pub_key)
|
|
|
|
.expect("NewUser::new_local: public key error"),
|
|
|
|
private_key: Some(
|
|
|
|
String::from_utf8(priv_key).expect("NewUser::new_local: private key error"),
|
|
|
|
),
|
|
|
|
shared_inbox_url: None,
|
|
|
|
followers_endpoint: String::from(""),
|
|
|
|
avatar_id: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
|
|
|
use diesel::Connection;
|
|
|
|
use instance::{tests as instance_tests, Instance};
|
2018-12-02 17:37:51 +01:00
|
|
|
use search::tests::get_searcher;
|
2018-11-24 12:44:17 +01:00
|
|
|
use tests::db;
|
|
|
|
use Connection as Conn;
|
|
|
|
|
|
|
|
pub(crate) fn fill_database(conn: &Conn) -> Vec<User> {
|
|
|
|
instance_tests::fill_database(conn);
|
2018-12-09 18:44:26 +01:00
|
|
|
let admin = NewUser::new_local(
|
|
|
|
conn,
|
|
|
|
"admin".to_owned(),
|
|
|
|
"The admin".to_owned(),
|
|
|
|
true,
|
|
|
|
"Hello there, I'm the admin",
|
|
|
|
"admin@example.com".to_owned(),
|
|
|
|
"invalid_admin_password".to_owned(),
|
|
|
|
);
|
|
|
|
admin.update_boxes(conn);
|
|
|
|
let user = NewUser::new_local(
|
|
|
|
conn,
|
|
|
|
"user".to_owned(),
|
|
|
|
"Some user".to_owned(),
|
|
|
|
false,
|
|
|
|
"Hello there, I'm no one",
|
|
|
|
"user@example.com".to_owned(),
|
|
|
|
"invalid_user_password".to_owned(),
|
|
|
|
);
|
|
|
|
user.update_boxes(conn);
|
|
|
|
let other = NewUser::new_local(
|
|
|
|
conn,
|
|
|
|
"other".to_owned(),
|
|
|
|
"Another user".to_owned(),
|
|
|
|
false,
|
|
|
|
"Hello there, I'm someone else",
|
|
|
|
"other@example.com".to_owned(),
|
|
|
|
"invalid_other_password".to_owned(),
|
|
|
|
);
|
|
|
|
other.update_boxes(conn);
|
|
|
|
vec![ admin, user, other ]
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_by() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
fill_database(conn);
|
|
|
|
let test_user = NewUser::new_local(
|
|
|
|
conn,
|
|
|
|
"test".to_owned(),
|
|
|
|
"test user".to_owned(),
|
|
|
|
false,
|
2018-11-26 10:21:52 +01:00
|
|
|
"Hello I'm a test",
|
2018-11-24 12:44:17 +01:00
|
|
|
"test@example.com".to_owned(),
|
2018-11-26 10:21:52 +01:00
|
|
|
User::hash_pass("test_password"),
|
2018-11-24 12:44:17 +01:00
|
|
|
);
|
|
|
|
test_user.update_boxes(conn);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_user.id,
|
2018-11-26 10:21:52 +01:00
|
|
|
User::find_by_name(conn, "test", Instance::local_id(conn))
|
2018-11-24 12:44:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.id
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
test_user.id,
|
2018-11-26 10:21:52 +01:00
|
|
|
User::find_by_fqn(conn, &test_user.get_fqn(conn)).unwrap().id
|
2018-11-24 12:44:17 +01:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
test_user.id,
|
2018-11-26 10:21:52 +01:00
|
|
|
User::find_by_email(conn, "test@example.com")
|
2018-11-24 12:44:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.id
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
test_user.id,
|
|
|
|
User::find_by_ap_url(
|
|
|
|
conn,
|
2018-11-26 10:21:52 +01:00
|
|
|
&format!(
|
2018-11-24 12:44:17 +01:00
|
|
|
"https://{}/@/{}/",
|
|
|
|
Instance::get_local(conn).unwrap().public_domain,
|
|
|
|
"test"
|
|
|
|
)
|
|
|
|
).unwrap()
|
|
|
|
.id
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let inserted = fill_database(conn);
|
|
|
|
|
|
|
|
assert!(User::get(conn, inserted[0].id).is_some());
|
2018-12-02 17:37:51 +01:00
|
|
|
inserted[0].delete(conn, &get_searcher());
|
2018-11-24 12:44:17 +01:00
|
|
|
assert!(User::get(conn, inserted[0].id).is_none());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn admin() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let inserted = fill_database(conn);
|
|
|
|
let local_inst = Instance::get_local(conn).unwrap();
|
|
|
|
let mut i = 0;
|
|
|
|
while local_inst.has_admin(conn) {
|
|
|
|
assert!(i < 100); //prevent from looping indefinitelly
|
|
|
|
local_inst.main_admin(conn).revoke_admin_rights(conn);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
inserted[0].grant_admin_rights(conn);
|
|
|
|
assert_eq!(inserted[0].id, local_inst.main_admin(conn).id);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let inserted = fill_database(conn);
|
|
|
|
let updated = inserted[0].update(
|
|
|
|
conn,
|
|
|
|
"new name".to_owned(),
|
|
|
|
"em@il".to_owned(),
|
|
|
|
"<p>summary</p><script></script>".to_owned(),
|
|
|
|
);
|
|
|
|
assert_eq!(updated.display_name, "new name");
|
|
|
|
assert_eq!(updated.email.unwrap(), "em@il");
|
|
|
|
assert_eq!(updated.summary.get(), "<p>summary</p>");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn auth() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
fill_database(conn);
|
|
|
|
let test_user = NewUser::new_local(
|
|
|
|
conn,
|
|
|
|
"test".to_owned(),
|
|
|
|
"test user".to_owned(),
|
|
|
|
false,
|
2018-11-26 10:21:52 +01:00
|
|
|
"Hello I'm a test",
|
2018-11-24 12:44:17 +01:00
|
|
|
"test@example.com".to_owned(),
|
2018-11-26 10:21:52 +01:00
|
|
|
User::hash_pass("test_password"),
|
2018-11-24 12:44:17 +01:00
|
|
|
);
|
|
|
|
test_user.update_boxes(conn);
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
assert!(test_user.auth("test_password"));
|
|
|
|
assert!(!test_user.auth("other_password"));
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_local_page() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
fill_database(conn);
|
|
|
|
|
|
|
|
let page = User::get_local_page(conn, (0, 2));
|
|
|
|
assert_eq!(page.len(), 2);
|
|
|
|
assert!(page[0].username <= page[1].username);
|
|
|
|
|
|
|
|
let mut last_username = User::get_local_page(conn, (0, 1))[0].username.clone();
|
|
|
|
for i in 1..User::count_local(conn) as i32 {
|
|
|
|
let page = User::get_local_page(conn, (i, i + 1));
|
|
|
|
assert_eq!(page.len(), 1);
|
|
|
|
assert!(last_username <= page[0].username);
|
|
|
|
last_username = page[0].username.clone();
|
|
|
|
}
|
|
|
|
assert_eq!(
|
2018-12-14 23:16:18 +01:00
|
|
|
User::get_local_page(conn, (0, User::count_local(conn) as i32 + 10)).len() as i64,
|
2018-11-24 12:44:17 +01:00
|
|
|
User::count_local(conn)
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
|
|
|
}
|