2018-04-24 11:21:39 +02:00
|
|
|
use bcrypt;
|
2018-04-30 19:46:27 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-04-29 22:23:44 +02:00
|
|
|
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, BelongingToDsl, PgConnection};
|
|
|
|
use diesel::dsl::any;
|
2018-05-01 13:48:19 +02:00
|
|
|
use reqwest::Client;
|
|
|
|
use reqwest::header::{Accept, qitem};
|
|
|
|
use reqwest::mime::Mime;
|
2018-04-24 11:21:39 +02:00
|
|
|
use rocket::request::{self, FromRequest, Request};
|
2018-04-23 11:52:44 +02:00
|
|
|
use rocket::outcome::IntoOutcome;
|
2018-05-01 13:48:19 +02:00
|
|
|
use serde_json;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-04-29 20:01:42 +02:00
|
|
|
use activity_pub::activity::Activity;
|
2018-04-24 11:21:39 +02:00
|
|
|
use activity_pub::actor::{ActorType, Actor};
|
2018-04-29 20:01:42 +02:00
|
|
|
use activity_pub::outbox::Outbox;
|
2018-05-01 13:48:19 +02:00
|
|
|
use activity_pub::webfinger::{Webfinger, resolve};
|
2018-04-23 11:52:44 +02:00
|
|
|
use db_conn::DbConn;
|
2018-05-01 15:23:23 +02:00
|
|
|
use models::follows::Follow;
|
2018-04-23 14:01:32 +02:00
|
|
|
use models::instance::Instance;
|
2018-04-29 22:23:44 +02:00
|
|
|
use models::post_authors::PostAuthor;
|
|
|
|
use models::posts::Post;
|
2018-04-24 11:21:39 +02:00
|
|
|
use schema::users;
|
2018-04-23 11:52:44 +02:00
|
|
|
|
|
|
|
pub const AUTH_COOKIE: &'static str = "user_id";
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-05-01 13:48:19 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize)]
|
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,
|
|
|
|
pub summary: String,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub hashed_password: Option<String>,
|
2018-04-30 19:46:27 +02:00
|
|
|
pub instance_id: i32,
|
|
|
|
pub creation_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,
|
|
|
|
pub summary: String,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub hashed_password: Option<String>,
|
|
|
|
pub instance_id: i32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl User {
|
2018-04-23 12:29:27 +02:00
|
|
|
pub fn grant_admin_rights() {}
|
2018-04-22 20:13:12 +02:00
|
|
|
|
|
|
|
pub fn insert (conn: &PgConnection, new: NewUser) -> User {
|
|
|
|
diesel::insert_into(users::table)
|
|
|
|
.values(new)
|
|
|
|
.get_result(conn)
|
2018-04-23 11:52:44 +02:00
|
|
|
.expect("Error saving new user")
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 11:52:44 +02:00
|
|
|
pub fn get(conn: &PgConnection, id: i32) -> Option<User> {
|
|
|
|
users::table.filter(users::id.eq(id))
|
|
|
|
.limit(1)
|
|
|
|
.load::<User>(conn)
|
|
|
|
.expect("Error loading user by id")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_email(conn: &PgConnection, email: String) -> Option<User> {
|
|
|
|
users::table.filter(users::email.eq(email))
|
|
|
|
.limit(1)
|
|
|
|
.load::<User>(conn)
|
|
|
|
.expect("Error loading user by email")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
|
2018-05-01 13:48:19 +02:00
|
|
|
pub fn find_by_name(conn: &PgConnection, username: String, instance_id: i32) -> Option<User> {
|
2018-04-23 11:52:44 +02:00
|
|
|
users::table.filter(users::username.eq(username))
|
2018-05-01 13:48:19 +02:00
|
|
|
.filter(users::instance_id.eq(instance_id))
|
2018-04-23 11:52:44 +02:00
|
|
|
.limit(1)
|
|
|
|
.load::<User>(conn)
|
|
|
|
.expect("Error loading user by email")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
|
2018-05-01 13:48:19 +02:00
|
|
|
pub fn find_local(conn: &PgConnection, username: String) -> Option<User> {
|
|
|
|
User::find_by_name(conn, username, Instance::local_id(conn))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_fqn(conn: &PgConnection, fqn: String) -> Option<User> {
|
|
|
|
if fqn.contains("@") { // remote user
|
|
|
|
match Instance::get_by_domain(conn, String::from(fqn.split("@").last().unwrap())) {
|
|
|
|
Some(instance) => {
|
|
|
|
match User::find_by_name(conn, String::from(fqn.split("@").nth(0).unwrap()), instance.id) {
|
|
|
|
Some(u) => Some(u),
|
|
|
|
None => User::fetch_from_webfinger(conn, fqn)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => User::fetch_from_webfinger(conn, fqn)
|
|
|
|
}
|
|
|
|
} else { // local user
|
|
|
|
User::find_local(conn, fqn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_from_webfinger(conn: &PgConnection, acct: String) -> Option<User> {
|
2018-05-01 13:52:50 +02:00
|
|
|
match resolve(acct.clone()) {
|
2018-05-01 13:48:19 +02:00
|
|
|
Ok(url) => {
|
|
|
|
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();
|
2018-05-01 13:52:50 +02:00
|
|
|
Some(User::from_activity(conn, json, acct.split("@").last().unwrap().to_string()))
|
2018-05-01 13:48:19 +02:00
|
|
|
},
|
|
|
|
Err(_) => None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(details) => {
|
|
|
|
println!("{}", details);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_activity(conn: &PgConnection, acct: serde_json::Value, inst: String) -> User {
|
|
|
|
let instance = match Instance::get_by_domain(conn, inst.clone()) {
|
|
|
|
Some(instance) => instance,
|
|
|
|
None => {
|
|
|
|
Instance::insert(conn, String::from(""), inst.clone(), inst.clone(), false)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
User::insert(conn, NewUser {
|
|
|
|
username: acct["preferredUsername"].as_str().unwrap().to_string(),
|
|
|
|
display_name: 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(),
|
|
|
|
is_admin: false,
|
|
|
|
summary: acct["summary"].as_str().unwrap().to_string(),
|
|
|
|
email: None,
|
|
|
|
hashed_password: None,
|
|
|
|
instance_id: instance.id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-23 11:52:44 +02:00
|
|
|
pub fn hash_pass(pass: String) -> String {
|
|
|
|
bcrypt::hash(pass.as_str(), bcrypt::DEFAULT_COST).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn auth(&self, pass: String) -> bool {
|
|
|
|
bcrypt::verify(pass.as_str(), self.hashed_password.clone().unwrap().as_str()).is_ok()
|
|
|
|
}
|
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(users::outbox_url.eq(self.compute_outbox(conn)))
|
|
|
|
.get_result::<User>(conn).expect("Couldn't update outbox URL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.inbox_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(users::inbox_url.eq(self.compute_inbox(conn)))
|
|
|
|
.get_result::<User>(conn).expect("Couldn't update outbox URL");
|
|
|
|
}
|
|
|
|
}
|
2018-04-29 20:01:42 +02:00
|
|
|
|
|
|
|
pub fn outbox(&self, conn: &PgConnection) -> Outbox {
|
2018-04-29 22:23:44 +02:00
|
|
|
Outbox::new(self.compute_outbox(conn), self.get_activities(conn))
|
2018-04-29 20:01:42 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 22:23:44 +02:00
|
|
|
fn get_activities(&self, conn: &PgConnection) -> Vec<Activity> {
|
|
|
|
use schema::posts;
|
|
|
|
use schema::post_authors;
|
|
|
|
let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id);
|
|
|
|
let posts = posts::table.filter(posts::id.eq(any(posts_by_self))).load::<Post>(conn).unwrap();
|
|
|
|
posts.into_iter().map(|p| Activity::create(self, p, conn)).collect::<Vec<Activity>>()
|
2018-04-29 20:01:42 +02:00
|
|
|
}
|
2018-05-01 15:23:23 +02:00
|
|
|
|
|
|
|
pub fn get_followers(&self, conn: &PgConnection) -> Vec<User> {
|
|
|
|
use schema::follows;
|
|
|
|
let follows = Follow::belonging_to(self).select(follows::follower_id);
|
|
|
|
users::table.filter(users::id.eq(any(follows))).load::<User>(conn).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_following(&self, conn: &PgConnection) -> Vec<User> {
|
|
|
|
use schema::follows;
|
|
|
|
let follows = follows::table.filter(follows::follower_id.eq(self.id)).select(follows::following_id);
|
|
|
|
users::table.filter(users::id.eq(any(follows))).load::<User>(conn).unwrap()
|
|
|
|
}
|
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>()?;
|
|
|
|
request.cookies()
|
|
|
|
.get_private(AUTH_COOKIE)
|
|
|
|
.and_then(|cookie| cookie.value().parse().ok())
|
|
|
|
.map(|id| User::get(&*conn, id).unwrap())
|
|
|
|
.or_forward(())
|
|
|
|
}
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
2018-04-23 14:01:32 +02:00
|
|
|
|
|
|
|
impl Actor for User {
|
|
|
|
fn get_box_prefix() -> &'static str {
|
|
|
|
"@"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_actor_id(&self) -> String {
|
|
|
|
self.username.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
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::Person
|
|
|
|
}
|
2018-04-23 14:01:32 +02:00
|
|
|
}
|
2018-04-23 15:12:59 +02:00
|
|
|
|
2018-04-24 10:35:45 +02:00
|
|
|
impl Webfinger for User {
|
|
|
|
fn webfinger_subject(&self, conn: &PgConnection) -> String {
|
|
|
|
format!("acct:{}@{}", self.username, 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-04-23 15:12:59 +02:00
|
|
|
impl NewUser {
|
|
|
|
/// Creates a new local user
|
|
|
|
pub fn new_local(
|
|
|
|
username: String,
|
|
|
|
display_name: String,
|
|
|
|
is_admin: bool,
|
|
|
|
summary: String,
|
|
|
|
email: String,
|
|
|
|
password: String,
|
|
|
|
instance_id: i32
|
|
|
|
) -> NewUser {
|
|
|
|
NewUser {
|
|
|
|
username: username,
|
|
|
|
display_name: display_name,
|
|
|
|
outbox_url: String::from(""),
|
|
|
|
inbox_url: String::from(""),
|
|
|
|
is_admin: is_admin,
|
|
|
|
summary: summary,
|
|
|
|
email: Some(email),
|
|
|
|
hashed_password: Some(password),
|
|
|
|
instance_id: instance_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|