2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::{
|
2021-01-11 20:27:52 +00:00
|
|
|
|
ap_url, blocklisted_emails::BlocklistedEmail, blogs::Blog, db_conn::DbConn, follows::Follow,
|
|
|
|
|
instance::*, medias::Media, notifications::Notification, post_authors::PostAuthor, posts::Post,
|
2021-01-31 15:48:22 +00:00
|
|
|
|
safe_string::SafeString, schema::users, timeline::Timeline, Connection, Error, Result,
|
|
|
|
|
UserEvent::*, CONFIG, ITEMS_PER_PAGE, USER_CHAN,
|
2020-01-21 06:02:03 +00:00
|
|
|
|
};
|
2018-06-10 11:13:07 +00:00
|
|
|
|
use activitypub::{
|
2019-04-28 17:01:41 +00:00
|
|
|
|
activity::Delete,
|
|
|
|
|
actor::Person,
|
2019-10-30 10:22:28 +00:00
|
|
|
|
collection::{OrderedCollection, OrderedCollectionPage},
|
2019-04-28 17:01:41 +00:00
|
|
|
|
object::{Image, Tombstone},
|
|
|
|
|
Activity, CustomObject, Endpoint,
|
2018-05-16 18:20:44 +00:00
|
|
|
|
};
|
2022-02-12 16:43:01 +00:00
|
|
|
|
use activitystreams::{
|
|
|
|
|
activity::Delete as Delete07, actor::AsApActor, object::AsObject as _, prelude::*,
|
|
|
|
|
};
|
2018-11-24 11:44:17 +00:00
|
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2019-10-07 17:08:20 +00:00
|
|
|
|
use diesel::{self, BelongingToDsl, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
|
2020-10-04 10:18:54 +00:00
|
|
|
|
use ldap3::{LdapConn, Scope, SearchEntry};
|
2018-05-19 07:39:59 +00:00
|
|
|
|
use openssl::{
|
|
|
|
|
hash::MessageDigest,
|
|
|
|
|
pkey::{PKey, Private},
|
|
|
|
|
rsa::Rsa,
|
2018-11-24 11:44:17 +00:00
|
|
|
|
sign,
|
2018-05-19 07:39:59 +00:00
|
|
|
|
};
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use plume_common::{
|
|
|
|
|
activity_pub::{
|
2022-02-11 05:49:52 +00:00
|
|
|
|
inbox::{AsActor, AsObject, AsObject07, FromId, FromId07},
|
2021-12-05 10:27:57 +00:00
|
|
|
|
request::get,
|
2021-11-24 12:41:44 +00:00
|
|
|
|
sign::{gen_keypair, Error as SignError, Result as SignResult, Signer},
|
2022-02-11 05:49:52 +00:00
|
|
|
|
ActivityStream, ApSignature, CustomPerson as CustomPerson07, Id, IntoId, PublicKey,
|
2022-02-12 16:23:47 +00:00
|
|
|
|
ToAsString, ToAsUri, PUBLIC_VISIBILITY,
|
2020-01-21 06:02:03 +00:00
|
|
|
|
},
|
|
|
|
|
utils,
|
2018-06-23 16:36:11 +00:00
|
|
|
|
};
|
2021-01-31 15:48:22 +00:00
|
|
|
|
use riker::actors::{Publish, Tell};
|
2018-05-19 07:39:59 +00:00
|
|
|
|
use rocket::{
|
2018-11-24 11:44:17 +00:00
|
|
|
|
outcome::IntoOutcome,
|
2018-05-19 07:39:59 +00:00
|
|
|
|
request::{self, FromRequest, Request},
|
|
|
|
|
};
|
2019-03-20 16:56:17 +00:00
|
|
|
|
use std::{
|
|
|
|
|
cmp::PartialEq,
|
|
|
|
|
hash::{Hash, Hasher},
|
2021-01-31 13:55:28 +00:00
|
|
|
|
sync::Arc,
|
2019-03-20 16:56:17 +00:00
|
|
|
|
};
|
2018-05-01 18:02:29 +00:00
|
|
|
|
use url::Url;
|
2018-06-18 21:50:40 +00:00
|
|
|
|
use webfinger::*;
|
2018-04-24 09:21:39 +00:00
|
|
|
|
|
2018-06-21 20:30:56 +00:00
|
|
|
|
pub type CustomPerson = CustomObject<ApSignature, Person>;
|
|
|
|
|
|
2019-09-13 10:28:36 +00:00
|
|
|
|
pub enum Role {
|
|
|
|
|
Admin = 0,
|
|
|
|
|
Moderator = 1,
|
|
|
|
|
Normal = 2,
|
2021-11-22 14:05:28 +00:00
|
|
|
|
Instance = 3,
|
2019-09-13 10:28:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-12 18:40:54 +00:00
|
|
|
|
#[derive(Queryable, Identifiable, Clone, Debug, AsChangeset)]
|
2020-04-12 15:36:00 +00:00
|
|
|
|
#[changeset_options(treat_none_as_null = "true")]
|
2018-04-22 18:13:12 +00:00
|
|
|
|
pub struct User {
|
|
|
|
|
pub id: i32,
|
|
|
|
|
pub username: String,
|
|
|
|
|
pub display_name: String,
|
|
|
|
|
pub outbox_url: String,
|
|
|
|
|
pub inbox_url: String,
|
2019-03-17 19:11:29 +00:00
|
|
|
|
pub summary: String,
|
2018-04-22 18:13:12 +00:00
|
|
|
|
pub email: Option<String>,
|
|
|
|
|
pub hashed_password: Option<String>,
|
2018-04-30 17:46:27 +00:00
|
|
|
|
pub instance_id: i32,
|
2018-05-01 18:02:29 +00:00
|
|
|
|
pub creation_date: NaiveDateTime,
|
2018-05-03 17:12:01 +00:00
|
|
|
|
pub ap_url: String,
|
|
|
|
|
pub private_key: Option<String>,
|
2018-05-13 18:12:27 +00:00
|
|
|
|
pub public_key: String,
|
2018-07-27 10:53:21 +00:00
|
|
|
|
pub shared_inbox_url: Option<String>,
|
2018-09-03 11:17:59 +00:00
|
|
|
|
pub followers_endpoint: String,
|
|
|
|
|
pub avatar_id: Option<i32>,
|
2018-11-24 11:44:17 +00:00
|
|
|
|
pub last_fetched_date: NaiveDateTime,
|
2019-03-06 17:28:10 +00:00
|
|
|
|
pub fqn: String,
|
2019-03-17 19:11:29 +00:00
|
|
|
|
pub summary_html: SafeString,
|
2019-09-13 10:28:36 +00:00
|
|
|
|
/// 0 = admin
|
|
|
|
|
/// 1 = moderator
|
2021-11-22 14:05:28 +00:00
|
|
|
|
/// 3 = local instance
|
2019-09-13 10:28:36 +00:00
|
|
|
|
/// anything else = normal user
|
|
|
|
|
pub role: i32,
|
2019-08-20 22:42:04 +00:00
|
|
|
|
pub preferred_theme: Option<String>,
|
|
|
|
|
pub hide_custom_css: bool,
|
2018-04-22 18:13:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 20:35:03 +00:00
|
|
|
|
#[derive(Default, Insertable)]
|
2018-04-22 18:13:12 +00:00
|
|
|
|
#[table_name = "users"]
|
|
|
|
|
pub struct NewUser {
|
|
|
|
|
pub username: String,
|
|
|
|
|
pub display_name: String,
|
|
|
|
|
pub outbox_url: String,
|
|
|
|
|
pub inbox_url: String,
|
2019-03-17 19:11:29 +00:00
|
|
|
|
pub summary: String,
|
2018-04-22 18:13:12 +00:00
|
|
|
|
pub email: Option<String>,
|
|
|
|
|
pub hashed_password: Option<String>,
|
2018-05-01 18:02:29 +00:00
|
|
|
|
pub instance_id: i32,
|
2018-05-03 17:12:01 +00:00
|
|
|
|
pub ap_url: String,
|
|
|
|
|
pub private_key: Option<String>,
|
2018-05-13 18:12:27 +00:00
|
|
|
|
pub public_key: String,
|
2018-07-27 10:53:21 +00:00
|
|
|
|
pub shared_inbox_url: Option<String>,
|
2018-09-03 11:17:59 +00:00
|
|
|
|
pub followers_endpoint: String,
|
|
|
|
|
pub avatar_id: Option<i32>,
|
2019-03-17 19:11:29 +00:00
|
|
|
|
pub summary_html: SafeString,
|
2019-09-13 10:28:36 +00:00
|
|
|
|
pub role: i32,
|
2019-08-27 20:28:40 +00:00
|
|
|
|
pub fqn: String,
|
2018-04-22 18:13:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 09:21:52 +00:00
|
|
|
|
pub const AUTH_COOKIE: &str = "user_id";
|
|
|
|
|
const USER_PREFIX: &str = "@";
|
2018-06-21 17:42:17 +00:00
|
|
|
|
|
2018-04-22 18:13:12 +00:00
|
|
|
|
impl User {
|
2019-08-27 20:28:40 +00:00
|
|
|
|
insert!(users, NewUser);
|
2018-06-20 08:44:56 +00:00
|
|
|
|
get!(users);
|
2018-11-26 09:21:52 +00: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 13:57:38 +00:00
|
|
|
|
|
2019-09-13 10:28:36 +00:00
|
|
|
|
pub fn is_moderator(&self) -> bool {
|
|
|
|
|
self.role == Role::Admin as i32 || self.role == Role::Moderator as i32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_admin(&self) -> bool {
|
|
|
|
|
self.role == Role::Admin as i32
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn one_by_instance(conn: &Connection) -> Result<Vec<User>> {
|
2018-11-24 11:44:17 +00:00
|
|
|
|
users::table
|
|
|
|
|
.filter(users::instance_id.eq_any(users::table.select(users::instance_id).distinct()))
|
2018-09-27 21:06:40 +00:00
|
|
|
|
.load::<User>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
2018-09-09 11:19:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 13:51:46 +00:00
|
|
|
|
pub fn delete(&self, conn: &Connection) -> Result<()> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::post_authors;
|
2018-09-29 16:34:43 +00:00
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
for blog in Blog::find_for_author(conn, self)?
|
2018-11-24 11:44:17 +00:00
|
|
|
|
.iter()
|
2019-03-20 16:56:17 +00:00
|
|
|
|
.filter(|b| b.count_authors(conn).map(|c| c <= 1).unwrap_or(false))
|
|
|
|
|
{
|
2021-01-07 13:51:46 +00:00
|
|
|
|
blog.delete(conn)?;
|
2018-12-29 08:36:07 +00:00
|
|
|
|
}
|
2018-09-29 16:34:43 +00: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)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.load(conn)?;
|
2018-09-29 16:34:43 +00:00
|
|
|
|
for post_id in all_their_posts_ids {
|
2019-03-19 13:37:56 +00:00
|
|
|
|
// disabling this lint, because otherwise we'd have to turn it on
|
|
|
|
|
// the head, and make it even harder to follow!
|
|
|
|
|
#[allow(clippy::op_ref)]
|
2018-09-29 16:34:43 +00:00
|
|
|
|
let has_other_authors = post_authors::table
|
|
|
|
|
.filter(post_authors::post_id.eq(post_id))
|
|
|
|
|
.filter(post_authors::author_id.ne(self.id))
|
|
|
|
|
.count()
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.load(conn)?
|
2018-11-26 09:21:52 +00:00
|
|
|
|
.first()
|
2019-03-20 16:56:17 +00:00
|
|
|
|
.unwrap_or(&0)
|
|
|
|
|
> &0;
|
2018-09-29 16:34:43 +00:00
|
|
|
|
if !has_other_authors {
|
2021-01-07 13:51:46 +00:00
|
|
|
|
Post::get(conn, post_id)?.delete(conn)?;
|
2018-09-29 16:34:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 09:29:52 +00:00
|
|
|
|
for notif in Notification::find_followed_by(conn, self)? {
|
|
|
|
|
notif.delete(conn)?
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 11:44:17 +00:00
|
|
|
|
diesel::delete(self)
|
|
|
|
|
.execute(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map(|_| ())
|
|
|
|
|
.map_err(Error::from)
|
2018-09-09 10:25:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn get_instance(&self, conn: &Connection) -> Result<Instance> {
|
|
|
|
|
Instance::get(conn, self.instance_id)
|
2018-06-21 17:53:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:28:36 +00:00
|
|
|
|
pub fn set_role(&self, conn: &Connection, new_role: Role) -> Result<()> {
|
2018-11-24 11:44:17 +00:00
|
|
|
|
diesel::update(self)
|
2019-09-13 10:28:36 +00:00
|
|
|
|
.set(users::role.eq(new_role as i32))
|
2018-11-24 11:44:17 +00:00
|
|
|
|
.execute(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map(|_| ())
|
|
|
|
|
.map_err(Error::from)
|
2018-11-24 11:44:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn count_local(conn: &Connection) -> Result<i64> {
|
2018-11-24 11:44:17 +00:00
|
|
|
|
users::table
|
2019-05-10 20:59:34 +00:00
|
|
|
|
.filter(users::instance_id.eq(Instance::get_local()?.id))
|
2018-12-14 22:16:18 +00:00
|
|
|
|
.count()
|
2021-01-30 10:24:16 +00:00
|
|
|
|
.get_result(&*conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
2018-06-10 19:33:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
pub fn find_by_fqn(conn: &DbConn, fqn: &str) -> Result<User> {
|
2019-03-06 17:28:10 +00:00
|
|
|
|
let from_db = users::table
|
|
|
|
|
.filter(users::fqn.eq(fqn))
|
2021-01-30 10:24:16 +00:00
|
|
|
|
.first(&**conn)
|
2019-10-07 17:08:20 +00:00
|
|
|
|
.optional()?;
|
2019-03-06 17:28:10 +00:00
|
|
|
|
if let Some(from_db) = from_db {
|
|
|
|
|
Ok(from_db)
|
|
|
|
|
} else {
|
2021-01-30 10:24:16 +00:00
|
|
|
|
User::fetch_from_webfinger(conn, fqn)
|
2018-05-01 11:48:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 18:15:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* TODO: Should create user record with normalized(lowercased) email
|
|
|
|
|
*/
|
|
|
|
|
pub fn email_used(conn: &DbConn, email: &str) -> Result<bool> {
|
|
|
|
|
use diesel::dsl::{exists, select};
|
|
|
|
|
|
|
|
|
|
select(exists(
|
|
|
|
|
users::table
|
|
|
|
|
.filter(users::instance_id.eq(Instance::get_local()?.id))
|
|
|
|
|
.filter(users::email.eq(email))
|
|
|
|
|
.or_filter(users::email.eq(email.to_ascii_lowercase())),
|
|
|
|
|
))
|
|
|
|
|
.get_result(&**conn)
|
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
fn fetch_from_webfinger(conn: &DbConn, acct: &str) -> Result<User> {
|
2019-03-21 09:30:33 +00:00
|
|
|
|
let link = resolve(acct.to_owned(), true)?
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.links
|
|
|
|
|
.into_iter()
|
|
|
|
|
.find(|l| l.mime_type == Some(String::from("application/activity+json")))
|
|
|
|
|
.ok_or(Error::Webfinger)?;
|
2021-11-27 22:53:13 +00:00
|
|
|
|
User::from_id(
|
|
|
|
|
conn,
|
|
|
|
|
link.href.as_ref().ok_or(Error::Webfinger)?,
|
|
|
|
|
None,
|
|
|
|
|
CONFIG.proxy(),
|
|
|
|
|
)
|
|
|
|
|
.map_err(|(_, e)| e)
|
2018-05-01 11:48:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 20:09:07 +00:00
|
|
|
|
pub fn fetch_remote_interact_uri(acct: &str) -> Result<String> {
|
|
|
|
|
resolve(acct.to_owned(), true)?
|
|
|
|
|
.links
|
|
|
|
|
.into_iter()
|
|
|
|
|
.find(|l| l.rel == "http://ostatus.org/schema/1.0/subscribe")
|
|
|
|
|
.and_then(|l| l.template)
|
|
|
|
|
.ok_or(Error::Webfinger)
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
fn fetch(url: &str) -> Result<CustomPerson> {
|
2021-12-05 10:27:57 +00:00
|
|
|
|
let mut res = get(url, Self::get_sender(), CONFIG.proxy().cloned())?;
|
2018-12-29 08:36:07 +00:00
|
|
|
|
let text = &res.text()?;
|
|
|
|
|
// without this workaround, publicKey is not correctly deserialized
|
|
|
|
|
let ap_sign = serde_json::from_str::<ApSignature>(text)?;
|
|
|
|
|
let mut json = serde_json::from_str::<CustomPerson>(text)?;
|
|
|
|
|
json.custom_props = ap_sign;
|
|
|
|
|
Ok(json)
|
2018-05-01 18:02:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
pub fn fetch_from_url(conn: &DbConn, url: &str) -> Result<User> {
|
|
|
|
|
User::fetch(url).and_then(|json| User::from_activity(conn, json))
|
2018-05-01 11:48:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn refetch(&self, conn: &Connection) -> Result<()> {
|
|
|
|
|
User::fetch(&self.ap_url.clone()).and_then(|json| {
|
2018-11-24 11:44:17 +00:00
|
|
|
|
let avatar = Media::save_remote(
|
|
|
|
|
conn,
|
|
|
|
|
json.object
|
|
|
|
|
.object_props
|
2021-12-05 11:22:10 +00:00
|
|
|
|
.icon_image()? // FIXME: Fails when icon is not set
|
2018-11-24 11:44:17 +00:00
|
|
|
|
.object_props
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.url_string()?,
|
2021-11-27 22:53:13 +00:00
|
|
|
|
self,
|
2019-03-20 16:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
.ok();
|
2018-09-03 18:53:20 +00:00
|
|
|
|
|
|
|
|
|
diesel::update(self)
|
|
|
|
|
.set((
|
2019-03-20 16:56:17 +00:00
|
|
|
|
users::username.eq(json.object.ap_actor_props.preferred_username_string()?),
|
|
|
|
|
users::display_name.eq(json.object.object_props.name_string()?),
|
|
|
|
|
users::outbox_url.eq(json.object.ap_actor_props.outbox_string()?),
|
|
|
|
|
users::inbox_url.eq(json.object.ap_actor_props.inbox_string()?),
|
2018-11-24 11:44:17 +00:00
|
|
|
|
users::summary.eq(SafeString::new(
|
|
|
|
|
&json
|
|
|
|
|
.object
|
|
|
|
|
.object_props
|
|
|
|
|
.summary_string()
|
2018-11-26 09:21:52 +00:00
|
|
|
|
.unwrap_or_default(),
|
2018-11-24 11:44:17 +00:00
|
|
|
|
)),
|
2019-03-20 16:56:17 +00:00
|
|
|
|
users::followers_endpoint.eq(json.object.ap_actor_props.followers_string()?),
|
2018-12-02 18:07:36 +00:00
|
|
|
|
users::avatar_id.eq(avatar.map(|a| a.id)),
|
2018-11-24 11:44:17 +00:00
|
|
|
|
users::last_fetched_date.eq(Utc::now().naive_utc()),
|
2019-01-05 21:30:28 +00:00
|
|
|
|
users::public_key.eq(json
|
|
|
|
|
.custom_props
|
|
|
|
|
.public_key_publickey()?
|
|
|
|
|
.public_key_pem_string()?),
|
2018-11-24 11:44:17 +00:00
|
|
|
|
))
|
|
|
|
|
.execute(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map(|_| ())
|
2021-12-05 11:24:11 +00:00
|
|
|
|
.map_err(Error::from)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
})
|
2018-09-03 18:53:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn hash_pass(pass: &str) -> Result<String> {
|
|
|
|
|
bcrypt::hash(pass, 10).map_err(Error::from)
|
2018-04-23 09:52:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-04 10:18:22 +00:00
|
|
|
|
fn ldap_register(conn: &Connection, name: &str, password: &str) -> Result<User> {
|
2020-10-07 21:39:38 +00:00
|
|
|
|
if CONFIG.ldap.is_none() {
|
|
|
|
|
return Err(Error::NotFound);
|
|
|
|
|
}
|
|
|
|
|
let ldap = CONFIG.ldap.as_ref().unwrap();
|
|
|
|
|
|
|
|
|
|
let mut ldap_conn = LdapConn::new(&ldap.addr).map_err(|_| Error::NotFound)?;
|
|
|
|
|
let ldap_name = format!("{}={},{}", ldap.user_name_attr, name, ldap.base_dn);
|
|
|
|
|
let bind = ldap_conn
|
|
|
|
|
.simple_bind(&ldap_name, password)
|
|
|
|
|
.map_err(|_| Error::NotFound)?;
|
|
|
|
|
|
|
|
|
|
if bind.success().is_err() {
|
|
|
|
|
return Err(Error::NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let search = ldap_conn
|
|
|
|
|
.search(
|
|
|
|
|
&ldap_name,
|
|
|
|
|
Scope::Base,
|
|
|
|
|
"(|(objectClass=person)(objectClass=user))",
|
|
|
|
|
vec![&ldap.mail_attr],
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| Error::NotFound)?
|
|
|
|
|
.success()
|
|
|
|
|
.map_err(|_| Error::NotFound)?;
|
|
|
|
|
for entry in search.0 {
|
|
|
|
|
let entry = SearchEntry::construct(entry);
|
|
|
|
|
let email = entry.attrs.get("mail").and_then(|vec| vec.first());
|
2020-10-08 18:24:03 +00:00
|
|
|
|
if let Some(email) = email {
|
2020-10-04 10:18:22 +00:00
|
|
|
|
let _ = ldap_conn.unbind();
|
2020-10-07 21:39:38 +00:00
|
|
|
|
return NewUser::new_local(
|
|
|
|
|
conn,
|
|
|
|
|
name.to_owned(),
|
|
|
|
|
name.to_owned(),
|
|
|
|
|
Role::Normal,
|
|
|
|
|
"",
|
2020-10-08 18:24:03 +00:00
|
|
|
|
email.to_owned(),
|
2020-10-07 21:39:38 +00:00
|
|
|
|
None,
|
|
|
|
|
);
|
2020-10-04 10:18:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-07 21:39:38 +00:00
|
|
|
|
let _ = ldap_conn.unbind();
|
|
|
|
|
Err(Error::NotFound)
|
2020-10-03 11:21:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-04 10:18:22 +00:00
|
|
|
|
fn ldap_login(&self, password: &str) -> bool {
|
|
|
|
|
if let Some(ldap) = CONFIG.ldap.as_ref() {
|
|
|
|
|
let mut conn = if let Ok(conn) = LdapConn::new(&ldap.addr) {
|
|
|
|
|
conn
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2020-10-04 10:18:54 +00:00
|
|
|
|
let name = format!(
|
|
|
|
|
"{}={},{}",
|
|
|
|
|
ldap.user_name_attr, &self.username, ldap.base_dn
|
|
|
|
|
);
|
2020-10-04 10:18:22 +00:00
|
|
|
|
if let Ok(bind) = conn.simple_bind(&name, password) {
|
|
|
|
|
bind.success().is_ok()
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2020-10-03 11:21:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn login(conn: &Connection, ident: &str, password: &str) -> Result<User> {
|
|
|
|
|
let local_id = Instance::get_local()?.id;
|
2020-10-07 21:39:38 +00:00
|
|
|
|
let user = match User::find_by_email(conn, ident) {
|
|
|
|
|
Ok(user) => Ok(user),
|
2020-10-07 21:40:27 +00:00
|
|
|
|
_ => User::find_by_name(conn, ident, local_id),
|
|
|
|
|
}
|
|
|
|
|
.and_then(|u| {
|
|
|
|
|
if u.instance_id == local_id {
|
|
|
|
|
Ok(u)
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::NotFound)
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-10-03 11:21:31 +00:00
|
|
|
|
|
|
|
|
|
match user {
|
|
|
|
|
Ok(user) if user.hashed_password.is_some() => {
|
2020-10-04 10:18:54 +00:00
|
|
|
|
if bcrypt::verify(password, user.hashed_password.as_ref().unwrap()).unwrap_or(false)
|
|
|
|
|
{
|
2020-10-03 11:21:31 +00:00
|
|
|
|
Ok(user)
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::NotFound)
|
|
|
|
|
}
|
2020-10-04 10:18:54 +00:00
|
|
|
|
}
|
2020-10-03 11:21:31 +00:00
|
|
|
|
Ok(user) => {
|
2020-10-04 10:18:22 +00:00
|
|
|
|
if user.ldap_login(password) {
|
2020-10-03 11:21:31 +00:00
|
|
|
|
Ok(user)
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::NotFound)
|
|
|
|
|
}
|
2020-10-04 10:18:54 +00:00
|
|
|
|
}
|
2020-10-03 11:21:31 +00:00
|
|
|
|
e => {
|
2020-10-04 10:18:22 +00:00
|
|
|
|
if let Ok(user) = User::ldap_register(conn, ident, password) {
|
|
|
|
|
return Ok(user);
|
|
|
|
|
}
|
2020-10-07 21:39:38 +00:00
|
|
|
|
// if no user was found, and we were unable to auto-register from ldap
|
|
|
|
|
// fake-verify a password, and return an error.
|
2020-10-04 10:18:54 +00:00
|
|
|
|
let other = User::get(&*conn, 1)
|
|
|
|
|
.expect("No user is registered")
|
|
|
|
|
.hashed_password;
|
2020-10-03 11:21:31 +00:00
|
|
|
|
other.map(|pass| bcrypt::verify(password, &pass));
|
|
|
|
|
e
|
2020-10-04 10:18:54 +00:00
|
|
|
|
}
|
2020-10-03 11:21:31 +00:00
|
|
|
|
}
|
2018-04-23 09:52:44 +00:00
|
|
|
|
}
|
2018-04-23 13:12:59 +00:00
|
|
|
|
|
2019-01-03 15:45:27 +00:00
|
|
|
|
pub fn reset_password(&self, conn: &Connection, pass: &str) -> Result<()> {
|
|
|
|
|
diesel::update(self)
|
|
|
|
|
.set(users::hashed_password.eq(User::hash_pass(pass)?))
|
|
|
|
|
.execute(conn)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn get_local_page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<User>> {
|
2018-11-24 11:44:17 +00:00
|
|
|
|
users::table
|
2019-05-10 20:59:34 +00:00
|
|
|
|
.filter(users::instance_id.eq(Instance::get_local()?.id))
|
2018-09-09 10:25:55 +00:00
|
|
|
|
.order(users::username.asc())
|
|
|
|
|
.offset(min.into())
|
|
|
|
|
.limit((max - min).into())
|
|
|
|
|
.load::<User>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
2018-09-09 10:25:55 +00:00
|
|
|
|
}
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn outbox(&self, conn: &Connection) -> Result<ActivityStream<OrderedCollection>> {
|
2022-01-10 13:18:34 +00:00
|
|
|
|
Ok(ActivityStream::new(self.outbox_collection(conn)?))
|
|
|
|
|
}
|
|
|
|
|
pub fn outbox_collection(&self, conn: &Connection) -> Result<OrderedCollection> {
|
2018-05-18 22:04:30 +00:00
|
|
|
|
let mut coll = OrderedCollection::default();
|
2019-10-30 10:22:28 +00:00
|
|
|
|
let first = &format!("{}?page=1", &self.outbox_url);
|
|
|
|
|
let last = &format!(
|
|
|
|
|
"{}?page={}",
|
|
|
|
|
&self.outbox_url,
|
2021-11-27 22:53:13 +00:00
|
|
|
|
self.get_activities_count(conn) / i64::from(ITEMS_PER_PAGE) + 1
|
2019-10-30 10:22:28 +00:00
|
|
|
|
);
|
|
|
|
|
coll.collection_props.set_first_link(Id::new(first))?;
|
|
|
|
|
coll.collection_props.set_last_link(Id::new(last))?;
|
|
|
|
|
coll.collection_props
|
2021-11-27 22:53:13 +00:00
|
|
|
|
.set_total_items_u64(self.get_activities_count(conn) as u64)?;
|
2022-01-10 13:18:34 +00:00
|
|
|
|
Ok(coll)
|
2019-10-30 10:22:28 +00:00
|
|
|
|
}
|
|
|
|
|
pub fn outbox_page(
|
|
|
|
|
&self,
|
|
|
|
|
conn: &Connection,
|
|
|
|
|
(min, max): (i32, i32),
|
|
|
|
|
) -> Result<ActivityStream<OrderedCollectionPage>> {
|
2022-01-10 13:18:59 +00:00
|
|
|
|
Ok(ActivityStream::new(
|
|
|
|
|
self.outbox_collection_page(conn, (min, max))?,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
pub fn outbox_collection_page(
|
|
|
|
|
&self,
|
|
|
|
|
conn: &Connection,
|
|
|
|
|
(min, max): (i32, i32),
|
|
|
|
|
) -> Result<OrderedCollectionPage> {
|
2019-10-30 10:22:28 +00:00
|
|
|
|
let acts = self.get_activities_page(conn, (min, max))?;
|
2021-11-27 22:53:13 +00:00
|
|
|
|
let n_acts = self.get_activities_count(conn);
|
2019-10-30 10:22:28 +00:00
|
|
|
|
let mut coll = OrderedCollectionPage::default();
|
|
|
|
|
if n_acts - i64::from(min) >= i64::from(ITEMS_PER_PAGE) {
|
|
|
|
|
coll.collection_page_props.set_next_link(Id::new(&format!(
|
|
|
|
|
"{}?page={}",
|
|
|
|
|
&self.outbox_url,
|
|
|
|
|
min / ITEMS_PER_PAGE + 2
|
|
|
|
|
)))?;
|
|
|
|
|
}
|
|
|
|
|
if min > 0 {
|
|
|
|
|
coll.collection_page_props.set_prev_link(Id::new(&format!(
|
|
|
|
|
"{}?page={}",
|
|
|
|
|
&self.outbox_url,
|
|
|
|
|
min / ITEMS_PER_PAGE
|
|
|
|
|
)))?;
|
|
|
|
|
}
|
2018-12-29 08:36:07 +00:00
|
|
|
|
coll.collection_props.items = serde_json::to_value(acts)?;
|
2019-10-30 10:22:28 +00:00
|
|
|
|
coll.collection_page_props
|
|
|
|
|
.set_part_of_link(Id::new(&self.outbox_url))?;
|
2022-01-10 13:18:59 +00:00
|
|
|
|
Ok(coll)
|
2018-04-29 18:01:42 +00:00
|
|
|
|
}
|
2019-10-30 10:22:28 +00:00
|
|
|
|
fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
|
2021-12-05 10:27:57 +00:00
|
|
|
|
let mut res = get(url, Self::get_sender(), CONFIG.proxy().cloned())?;
|
2018-12-29 08:36:07 +00:00
|
|
|
|
let text = &res.text()?;
|
2019-03-20 16:56:17 +00:00
|
|
|
|
let json: serde_json::Value = serde_json::from_str(text)?;
|
2019-10-30 10:22:28 +00:00
|
|
|
|
let items = json["items"]
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.as_array()
|
|
|
|
|
.unwrap_or(&vec![])
|
2019-03-19 13:37:56 +00:00
|
|
|
|
.iter()
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.filter_map(|j| serde_json::from_value(j.clone()).ok())
|
2019-10-30 10:22:28 +00:00
|
|
|
|
.collect::<Vec<T>>();
|
|
|
|
|
|
2021-03-27 18:46:37 +00:00
|
|
|
|
let next = json.get("next").map(|x| x.as_str().unwrap().to_owned());
|
2019-10-30 10:22:28 +00:00
|
|
|
|
Ok((items, next))
|
|
|
|
|
}
|
|
|
|
|
pub fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
|
2021-12-05 10:27:57 +00:00
|
|
|
|
let mut res = get(
|
|
|
|
|
&self.outbox_url[..],
|
|
|
|
|
Self::get_sender(),
|
|
|
|
|
CONFIG.proxy().cloned(),
|
|
|
|
|
)?;
|
2019-10-30 10:22:28 +00:00
|
|
|
|
let text = &res.text()?;
|
|
|
|
|
let json: serde_json::Value = serde_json::from_str(text)?;
|
|
|
|
|
if let Some(first) = json.get("first") {
|
|
|
|
|
let mut items: Vec<T> = Vec::new();
|
|
|
|
|
let mut next = first.as_str().unwrap().to_owned();
|
|
|
|
|
while let Ok((mut page, nxt)) = self.fetch_outbox_page(&next) {
|
|
|
|
|
if page.is_empty() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-11-27 22:53:13 +00:00
|
|
|
|
items.append(&mut page);
|
2019-10-30 10:22:28 +00:00
|
|
|
|
if let Some(n) = nxt {
|
|
|
|
|
if n == next {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
next = n;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(items)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(json["items"]
|
|
|
|
|
.as_array()
|
|
|
|
|
.unwrap_or(&vec![])
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|j| serde_json::from_value(j.clone()).ok())
|
|
|
|
|
.collect::<Vec<T>>())
|
|
|
|
|
}
|
2018-07-26 20:23:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn fetch_followers_ids(&self) -> Result<Vec<String>> {
|
2021-12-05 10:27:57 +00:00
|
|
|
|
let mut res = get(
|
|
|
|
|
&self.followers_endpoint[..],
|
|
|
|
|
Self::get_sender(),
|
|
|
|
|
CONFIG.proxy().cloned(),
|
|
|
|
|
)?;
|
2018-12-29 08:36:07 +00:00
|
|
|
|
let text = &res.text()?;
|
|
|
|
|
let json: serde_json::Value = serde_json::from_str(text)?;
|
|
|
|
|
Ok(json["items"]
|
|
|
|
|
.as_array()
|
|
|
|
|
.unwrap_or(&vec![])
|
2019-03-19 13:37:56 +00:00
|
|
|
|
.iter()
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.filter_map(|j| serde_json::from_value(j.clone()).ok())
|
|
|
|
|
.collect::<Vec<String>>())
|
2018-07-27 10:53:21 +00:00
|
|
|
|
}
|
2019-10-30 10:22:28 +00:00
|
|
|
|
fn get_activities_count(&self, conn: &Connection) -> i64 {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::post_authors;
|
|
|
|
|
use crate::schema::posts;
|
2019-10-30 10:22:28 +00:00
|
|
|
|
let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id);
|
|
|
|
|
posts::table
|
|
|
|
|
.filter(posts::published.eq(true))
|
|
|
|
|
.filter(posts::id.eq_any(posts_by_self))
|
|
|
|
|
.count()
|
|
|
|
|
.first(conn)
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
fn get_activities_page(
|
|
|
|
|
&self,
|
|
|
|
|
conn: &Connection,
|
|
|
|
|
(min, max): (i32, i32),
|
|
|
|
|
) -> Result<Vec<serde_json::Value>> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::post_authors;
|
|
|
|
|
use crate::schema::posts;
|
2018-04-29 20:23:44 +00:00
|
|
|
|
let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id);
|
2018-09-10 19:06:00 +00:00
|
|
|
|
let posts = posts::table
|
|
|
|
|
.filter(posts::published.eq(true))
|
2018-09-27 21:06:40 +00:00
|
|
|
|
.filter(posts::id.eq_any(posts_by_self))
|
2019-10-30 10:22:28 +00:00
|
|
|
|
.order(posts::creation_date.desc())
|
|
|
|
|
.offset(min.into())
|
|
|
|
|
.limit((max - min).into())
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.load::<Post>(conn)?;
|
|
|
|
|
Ok(posts
|
2018-11-24 11:44:17 +00:00
|
|
|
|
.into_iter()
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.filter_map(|p| {
|
2019-03-20 16:56:17 +00:00
|
|
|
|
p.create_activity(conn)
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|a| serde_json::to_value(a).ok())
|
2018-11-24 11:44:17 +00:00
|
|
|
|
})
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.collect::<Vec<serde_json::Value>>())
|
2018-04-29 18:01:42 +00:00
|
|
|
|
}
|
2018-05-01 13:23:23 +00:00
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn get_followers(&self, conn: &Connection) -> Result<Vec<User>> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::follows;
|
2018-05-01 13:23:23 +00:00
|
|
|
|
let follows = Follow::belonging_to(self).select(follows::follower_id);
|
2018-11-24 11:44:17 +00:00
|
|
|
|
users::table
|
|
|
|
|
.filter(users::id.eq_any(follows))
|
|
|
|
|
.load::<User>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
2018-05-01 13:23:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn count_followers(&self, conn: &Connection) -> Result<i64> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::follows;
|
2018-12-14 22:16:18 +00:00
|
|
|
|
let follows = Follow::belonging_to(self).select(follows::follower_id);
|
|
|
|
|
users::table
|
|
|
|
|
.filter(users::id.eq_any(follows))
|
|
|
|
|
.count()
|
|
|
|
|
.get_result(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
2018-12-14 22:16:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 16:56:17 +00:00
|
|
|
|
pub fn get_followers_page(
|
|
|
|
|
&self,
|
|
|
|
|
conn: &Connection,
|
|
|
|
|
(min, max): (i32, i32),
|
|
|
|
|
) -> Result<Vec<User>> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::follows;
|
2018-07-25 13:50:29 +00:00
|
|
|
|
let follows = Follow::belonging_to(self).select(follows::follower_id);
|
2018-11-24 11:44:17 +00:00
|
|
|
|
users::table
|
|
|
|
|
.filter(users::id.eq_any(follows))
|
2018-07-25 13:50:29 +00:00
|
|
|
|
.offset(min.into())
|
|
|
|
|
.limit((max - min).into())
|
2018-11-24 11:44:17 +00:00
|
|
|
|
.load::<User>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
2018-07-25 13:50:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-26 12:13:00 +00:00
|
|
|
|
pub fn get_followed(&self, conn: &Connection) -> Result<Vec<User>> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::follows::dsl::*;
|
2018-09-27 21:06:40 +00:00
|
|
|
|
let f = follows.filter(follower_id.eq(self.id)).select(following_id);
|
2018-11-24 11:44:17 +00:00
|
|
|
|
users::table
|
|
|
|
|
.filter(users::id.eq_any(f))
|
|
|
|
|
.load::<User>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
2018-05-01 13:23:23 +00:00
|
|
|
|
}
|
2018-05-03 17:12:01 +00:00
|
|
|
|
|
2019-02-26 12:13:00 +00:00
|
|
|
|
pub fn count_followed(&self, conn: &Connection) -> Result<i64> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::follows;
|
2019-02-26 12:13:00 +00:00
|
|
|
|
follows::table
|
|
|
|
|
.filter(follows::follower_id.eq(self.id))
|
|
|
|
|
.count()
|
|
|
|
|
.get_result(conn)
|
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 16:56:17 +00:00
|
|
|
|
pub fn get_followed_page(
|
|
|
|
|
&self,
|
|
|
|
|
conn: &Connection,
|
|
|
|
|
(min, max): (i32, i32),
|
|
|
|
|
) -> Result<Vec<User>> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::follows;
|
2019-02-26 12:13:00 +00:00
|
|
|
|
let follows = follows::table
|
|
|
|
|
.filter(follows::follower_id.eq(self.id))
|
|
|
|
|
.select(follows::following_id)
|
|
|
|
|
.limit((max - min).into());
|
|
|
|
|
users::table
|
|
|
|
|
.filter(users::id.eq_any(follows))
|
|
|
|
|
.offset(min.into())
|
|
|
|
|
.load::<User>(conn)
|
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn is_followed_by(&self, conn: &Connection, other_id: i32) -> Result<bool> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::follows;
|
2018-12-14 22:16:18 +00:00
|
|
|
|
follows::table
|
2018-06-13 18:06:14 +00:00
|
|
|
|
.filter(follows::follower_id.eq(other_id))
|
|
|
|
|
.filter(follows::following_id.eq(self.id))
|
2018-12-14 22:16:18 +00:00
|
|
|
|
.count()
|
|
|
|
|
.get_result::<i64>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
.map(|r| r > 0)
|
2018-06-13 18:06:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn is_following(&self, conn: &Connection, other_id: i32) -> Result<bool> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::follows;
|
2018-12-14 22:16:18 +00:00
|
|
|
|
follows::table
|
2018-07-20 15:51:32 +00:00
|
|
|
|
.filter(follows::follower_id.eq(self.id))
|
|
|
|
|
.filter(follows::following_id.eq(other_id))
|
2018-12-14 22:16:18 +00:00
|
|
|
|
.count()
|
|
|
|
|
.get_result::<i64>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
.map(|r| r > 0)
|
2018-07-20 15:51:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn has_liked(&self, conn: &Connection, post: &Post) -> Result<bool> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::likes;
|
2018-12-14 22:16:18 +00:00
|
|
|
|
likes::table
|
2018-05-12 20:56:57 +00:00
|
|
|
|
.filter(likes::post_id.eq(post.id))
|
|
|
|
|
.filter(likes::user_id.eq(self.id))
|
2018-12-14 22:16:18 +00:00
|
|
|
|
.count()
|
|
|
|
|
.get_result::<i64>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
.map(|r| r > 0)
|
2018-05-12 20:56:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn has_reshared(&self, conn: &Connection, post: &Post) -> Result<bool> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::reshares;
|
2018-12-14 22:16:18 +00:00
|
|
|
|
reshares::table
|
2018-05-19 09:51:10 +00:00
|
|
|
|
.filter(reshares::post_id.eq(post.id))
|
|
|
|
|
.filter(reshares::user_id.eq(self.id))
|
2018-12-14 22:16:18 +00:00
|
|
|
|
.count()
|
|
|
|
|
.get_result::<i64>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
.map(|r| r > 0)
|
2018-05-19 09:51:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn is_author_in(&self, conn: &Connection, blog: &Blog) -> Result<bool> {
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::schema::blog_authors;
|
2018-12-14 22:16:18 +00:00
|
|
|
|
blog_authors::table
|
2018-11-24 11:44:17 +00:00
|
|
|
|
.filter(blog_authors::author_id.eq(self.id))
|
2018-06-10 18:16:25 +00:00
|
|
|
|
.filter(blog_authors::blog_id.eq(blog.id))
|
2018-12-14 22:16:18 +00:00
|
|
|
|
.count()
|
|
|
|
|
.get_result::<i64>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
.map(|r| r > 0)
|
2018-06-10 18:16:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn get_keypair(&self) -> Result<PKey<Private>> {
|
2019-03-20 16:56:17 +00:00
|
|
|
|
PKey::from_rsa(Rsa::private_key_from_pem(
|
2021-11-27 22:53:13 +00:00
|
|
|
|
self.private_key.clone().ok_or(Error::Signature)?.as_ref(),
|
2019-03-20 16:56:17 +00:00
|
|
|
|
)?)
|
|
|
|
|
.map_err(Error::from)
|
2018-05-03 17:12:01 +00:00
|
|
|
|
}
|
2018-05-18 22:04:30 +00:00
|
|
|
|
|
2019-01-05 21:30:28 +00:00
|
|
|
|
pub fn rotate_keypair(&self, conn: &Connection) -> Result<PKey<Private>> {
|
|
|
|
|
if self.private_key.is_none() {
|
2019-03-20 16:56:17 +00:00
|
|
|
|
return Err(Error::InvalidValue);
|
2019-01-05 21:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
if (Utc::now().naive_utc() - self.last_fetched_date).num_minutes() < 10 {
|
|
|
|
|
//rotated recently
|
|
|
|
|
self.get_keypair()
|
|
|
|
|
} else {
|
|
|
|
|
let (public_key, private_key) = gen_keypair();
|
2019-03-20 16:56:17 +00:00
|
|
|
|
let public_key =
|
|
|
|
|
String::from_utf8(public_key).expect("NewUser::new_local: public key error");
|
|
|
|
|
let private_key =
|
|
|
|
|
String::from_utf8(private_key).expect("NewUser::new_local: private key error");
|
|
|
|
|
let res = PKey::from_rsa(Rsa::private_key_from_pem(private_key.as_ref())?)?;
|
2019-01-05 21:30:28 +00:00
|
|
|
|
diesel::update(self)
|
2019-03-20 16:56:17 +00:00
|
|
|
|
.set((
|
|
|
|
|
users::public_key.eq(public_key),
|
2019-01-05 21:30:28 +00:00
|
|
|
|
users::private_key.eq(Some(private_key)),
|
2019-03-20 16:56:17 +00:00
|
|
|
|
users::last_fetched_date.eq(Utc::now().naive_utc()),
|
|
|
|
|
))
|
2019-01-05 21:30:28 +00:00
|
|
|
|
.execute(conn)
|
|
|
|
|
.map_err(Error::from)
|
|
|
|
|
.map(|_| res)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn to_activity(&self, conn: &Connection) -> Result<CustomPerson> {
|
2018-05-18 22:04:30 +00:00
|
|
|
|
let mut actor = Person::default();
|
2019-03-20 16:56:17 +00:00
|
|
|
|
actor.object_props.set_id_string(self.ap_url.clone())?;
|
2018-11-24 11:44:17 +00:00
|
|
|
|
actor
|
|
|
|
|
.object_props
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.set_name_string(self.display_name.clone())?;
|
2018-11-24 11:44:17 +00:00
|
|
|
|
actor
|
|
|
|
|
.object_props
|
2019-03-17 19:11:29 +00:00
|
|
|
|
.set_summary_string(self.summary_html.get().clone())?;
|
2019-03-20 16:56:17 +00:00
|
|
|
|
actor.object_props.set_url_string(self.ap_url.clone())?;
|
2018-11-24 11:44:17 +00:00
|
|
|
|
actor
|
|
|
|
|
.ap_actor_props
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.set_inbox_string(self.inbox_url.clone())?;
|
2018-11-24 11:44:17 +00:00
|
|
|
|
actor
|
|
|
|
|
.ap_actor_props
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.set_outbox_string(self.outbox_url.clone())?;
|
2018-11-24 11:44:17 +00:00
|
|
|
|
actor
|
|
|
|
|
.ap_actor_props
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.set_preferred_username_string(self.username.clone())?;
|
2018-11-24 11:44:17 +00:00
|
|
|
|
actor
|
|
|
|
|
.ap_actor_props
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.set_followers_string(self.followers_endpoint.clone())?;
|
2018-06-21 15:14:26 +00:00
|
|
|
|
|
2019-04-17 17:31:47 +00:00
|
|
|
|
if let Some(shared_inbox_url) = self.shared_inbox_url.clone() {
|
|
|
|
|
let mut endpoints = Endpoint::default();
|
|
|
|
|
endpoints.set_shared_inbox_string(shared_inbox_url)?;
|
|
|
|
|
actor.ap_actor_props.set_endpoints_endpoint(endpoints)?;
|
|
|
|
|
}
|
2018-06-21 21:12:24 +00:00
|
|
|
|
|
|
|
|
|
let mut public_key = PublicKey::default();
|
2019-03-20 16:56:17 +00:00
|
|
|
|
public_key.set_id_string(format!("{}#main-key", self.ap_url))?;
|
|
|
|
|
public_key.set_owner_string(self.ap_url.clone())?;
|
|
|
|
|
public_key.set_public_key_pem_string(self.public_key.clone())?;
|
2018-06-21 21:12:24 +00:00
|
|
|
|
let mut ap_signature = ApSignature::default();
|
2019-03-20 16:56:17 +00:00
|
|
|
|
ap_signature.set_public_key_publickey(public_key)?;
|
2018-06-21 21:12:24 +00:00
|
|
|
|
|
2022-01-10 13:20:10 +00:00
|
|
|
|
if let Some(avatar_id) = self.avatar_id {
|
|
|
|
|
let mut avatar = Image::default();
|
|
|
|
|
avatar
|
|
|
|
|
.object_props
|
|
|
|
|
.set_url_string(Media::get(conn, avatar_id)?.url()?)?;
|
|
|
|
|
actor.object_props.set_icon_object(avatar)?;
|
|
|
|
|
}
|
2018-09-03 12:48:34 +00:00
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
Ok(CustomPerson::new(actor, ap_signature))
|
2018-05-18 22:04:30 +00:00
|
|
|
|
}
|
2018-06-18 16:34:29 +00:00
|
|
|
|
|
2019-04-28 17:01:41 +00:00
|
|
|
|
pub fn delete_activity(&self, conn: &Connection) -> Result<Delete> {
|
|
|
|
|
let mut del = Delete::default();
|
|
|
|
|
|
|
|
|
|
let mut tombstone = Tombstone::default();
|
|
|
|
|
tombstone.object_props.set_id_string(self.ap_url.clone())?;
|
|
|
|
|
|
|
|
|
|
del.delete_props
|
|
|
|
|
.set_actor_link(Id::new(self.ap_url.clone()))?;
|
|
|
|
|
del.delete_props.set_object_object(tombstone)?;
|
|
|
|
|
del.object_props
|
|
|
|
|
.set_id_string(format!("{}#delete", self.ap_url))?;
|
|
|
|
|
del.object_props
|
|
|
|
|
.set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY)])?;
|
|
|
|
|
del.object_props.set_cc_link_vec(
|
|
|
|
|
self.get_followers(conn)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|f| Id::new(f.ap_url))
|
|
|
|
|
.collect(),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
Ok(del)
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 17:54:16 +00:00
|
|
|
|
pub fn avatar_url(&self, conn: &Connection) -> String {
|
2019-03-20 16:56:17 +00:00
|
|
|
|
self.avatar_id
|
2019-05-10 20:59:34 +00:00
|
|
|
|
.and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok())
|
2019-08-20 22:42:04 +00:00
|
|
|
|
.unwrap_or_else(|| "/static/images/default-avatar.png".to_string())
|
2018-06-18 16:34:29 +00:00
|
|
|
|
}
|
2018-06-18 21:50:40 +00:00
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn webfinger(&self, conn: &Connection) -> Result<Webfinger> {
|
|
|
|
|
Ok(Webfinger {
|
2021-02-14 13:14:44 +00:00
|
|
|
|
subject: format!("acct:{}", self.acct_authority(conn)?),
|
2018-06-21 14:48:54 +00:00
|
|
|
|
aliases: vec![self.ap_url.clone()],
|
2018-06-18 21:50:40 +00:00
|
|
|
|
links: vec![
|
|
|
|
|
Link {
|
|
|
|
|
rel: String::from("http://webfinger.net/rel/profile-page"),
|
2019-04-17 20:09:07 +00:00
|
|
|
|
mime_type: Some(String::from("text/html")),
|
2018-07-26 19:35:35 +00:00
|
|
|
|
href: Some(self.ap_url.clone()),
|
2018-11-24 11:44:17 +00:00
|
|
|
|
template: None,
|
2018-06-18 21:50:40 +00:00
|
|
|
|
},
|
|
|
|
|
Link {
|
|
|
|
|
rel: String::from("http://schemas.google.com/g/2010#updates-from"),
|
|
|
|
|
mime_type: Some(String::from("application/atom+xml")),
|
2018-12-29 08:36:07 +00:00
|
|
|
|
href: Some(self.get_instance(conn)?.compute_box(
|
2018-11-24 11:44:17 +00:00
|
|
|
|
USER_PREFIX,
|
2018-11-26 09:21:52 +00:00
|
|
|
|
&self.username,
|
2018-11-24 11:44:17 +00:00
|
|
|
|
"feed.atom",
|
|
|
|
|
)),
|
|
|
|
|
template: None,
|
2018-06-18 21:50:40 +00:00
|
|
|
|
},
|
|
|
|
|
Link {
|
|
|
|
|
rel: String::from("self"),
|
|
|
|
|
mime_type: Some(String::from("application/activity+json")),
|
2018-07-26 19:35:35 +00:00
|
|
|
|
href: Some(self.ap_url.clone()),
|
2018-11-24 11:44:17 +00:00
|
|
|
|
template: None,
|
|
|
|
|
},
|
2019-04-17 20:09:07 +00:00
|
|
|
|
Link {
|
|
|
|
|
rel: String::from("http://ostatus.org/schema/1.0/subscribe"),
|
|
|
|
|
mime_type: None,
|
|
|
|
|
href: None,
|
|
|
|
|
template: Some(format!(
|
2020-05-02 10:07:45 +00:00
|
|
|
|
"https://{}/remote_interact?target={{uri}}",
|
2019-04-17 20:09:07 +00:00
|
|
|
|
self.get_instance(conn)?.public_domain
|
|
|
|
|
)),
|
|
|
|
|
},
|
2018-11-24 11:44:17 +00:00
|
|
|
|
],
|
2018-12-29 08:36:07 +00:00
|
|
|
|
})
|
2018-06-18 21:50:40 +00:00
|
|
|
|
}
|
2018-06-21 17:23:01 +00:00
|
|
|
|
|
2021-02-14 13:14:44 +00:00
|
|
|
|
pub fn acct_authority(&self, conn: &Connection) -> Result<String> {
|
|
|
|
|
Ok(format!(
|
|
|
|
|
"{}@{}",
|
|
|
|
|
self.username,
|
|
|
|
|
self.get_instance(conn)?.public_domain
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
|
pub fn set_avatar(&self, conn: &Connection, id: i32) -> Result<()> {
|
2018-09-03 12:04:17 +00:00
|
|
|
|
diesel::update(self)
|
|
|
|
|
.set(users::avatar_id.eq(id))
|
|
|
|
|
.execute(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.map(|_| ())
|
|
|
|
|
.map_err(Error::from)
|
2018-09-03 12:04:17 +00:00
|
|
|
|
}
|
2018-09-03 18:53:20 +00:00
|
|
|
|
|
|
|
|
|
pub fn needs_update(&self) -> bool {
|
|
|
|
|
(Utc::now().naive_utc() - self.last_fetched_date).num_days() > 1
|
|
|
|
|
}
|
2018-12-06 17:54:16 +00:00
|
|
|
|
|
2019-03-06 17:28:10 +00:00
|
|
|
|
pub fn name(&self) -> String {
|
2018-12-06 17:54:16 +00:00
|
|
|
|
if !self.display_name.is_empty() {
|
|
|
|
|
self.display_name.clone()
|
|
|
|
|
} else {
|
2019-03-06 17:28:10 +00:00
|
|
|
|
self.fqn.clone()
|
2018-12-06 17:54:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-31 15:48:22 +00:00
|
|
|
|
|
|
|
|
|
pub fn remote_user_found(&self) {
|
|
|
|
|
tracing::trace!("{:?}", self);
|
|
|
|
|
self.publish_remote_user_found();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn publish_remote_user_found(&self) {
|
|
|
|
|
USER_CHAN.tell(
|
|
|
|
|
Publish {
|
|
|
|
|
msg: RemoteUserFound(Arc::new(self.clone())),
|
|
|
|
|
topic: "user.remote_user_found".into(),
|
|
|
|
|
},
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
}
|
2018-05-18 22:04:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 09:52:44 +00: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 11:44:17 +00:00
|
|
|
|
request
|
|
|
|
|
.cookies()
|
2018-04-23 09:52:44 +00:00
|
|
|
|
.get_private(AUTH_COOKIE)
|
|
|
|
|
.and_then(|cookie| cookie.value().parse().ok())
|
2018-12-29 08:36:07 +00:00
|
|
|
|
.and_then(|id| User::get(&*conn, id).ok())
|
2018-04-23 09:52:44 +00:00
|
|
|
|
.or_forward(())
|
|
|
|
|
}
|
2018-04-22 18:13:12 +00:00
|
|
|
|
}
|
2018-04-23 12:01:32 +00:00
|
|
|
|
|
2018-05-18 08:04:40 +00:00
|
|
|
|
impl IntoId for User {
|
2018-05-18 22:04:30 +00:00
|
|
|
|
fn into_id(self) -> Id {
|
2020-01-19 11:52:32 +00:00
|
|
|
|
Id::new(self.ap_url)
|
2018-05-18 08:04:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-24 10:23:04 +00:00
|
|
|
|
impl Eq for User {}
|
2018-05-18 08:04:40 +00:00
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
impl FromId<DbConn> for User {
|
2019-04-17 17:31:47 +00:00
|
|
|
|
type Error = Error;
|
|
|
|
|
type Object = CustomPerson;
|
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
fn from_db(conn: &DbConn, id: &str) -> Result<Self> {
|
|
|
|
|
Self::find_by_ap_url(conn, id)
|
2019-04-17 17:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
fn from_activity(conn: &DbConn, acct: CustomPerson) -> Result<Self> {
|
2019-04-17 17:31:47 +00:00
|
|
|
|
let url = Url::parse(&acct.object.object_props.id_string()?)?;
|
2021-11-27 22:53:13 +00:00
|
|
|
|
let inst = url.host_str().ok_or(Error::Url)?;
|
2021-01-30 10:24:16 +00:00
|
|
|
|
let instance = Instance::find_by_domain(conn, inst).or_else(|_| {
|
2019-04-17 17:31:47 +00:00
|
|
|
|
Instance::insert(
|
2021-01-30 10:24:16 +00:00
|
|
|
|
conn,
|
2019-04-17 17:31:47 +00:00
|
|
|
|
NewInstance {
|
|
|
|
|
name: inst.to_owned(),
|
|
|
|
|
public_domain: inst.to_owned(),
|
|
|
|
|
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(),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let username = acct.object.ap_actor_props.preferred_username_string()?;
|
|
|
|
|
|
|
|
|
|
if username.contains(&['<', '>', '&', '@', '\'', '"', ' ', '\t'][..]) {
|
|
|
|
|
return Err(Error::InvalidValue);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 20:28:40 +00:00
|
|
|
|
let fqn = if instance.local {
|
|
|
|
|
username.clone()
|
|
|
|
|
} else {
|
|
|
|
|
format!("{}@{}", username, instance.public_domain)
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-17 17:31:47 +00:00
|
|
|
|
let user = User::insert(
|
2021-01-30 10:24:16 +00:00
|
|
|
|
conn,
|
2019-04-17 17:31:47 +00:00
|
|
|
|
NewUser {
|
|
|
|
|
display_name: acct
|
|
|
|
|
.object
|
|
|
|
|
.object_props
|
|
|
|
|
.name_string()
|
|
|
|
|
.unwrap_or_else(|_| username.clone()),
|
|
|
|
|
username,
|
|
|
|
|
outbox_url: acct.object.ap_actor_props.outbox_string()?,
|
|
|
|
|
inbox_url: acct.object.ap_actor_props.inbox_string()?,
|
2019-09-13 10:28:36 +00:00
|
|
|
|
role: 2,
|
2019-04-17 17:31:47 +00:00
|
|
|
|
summary: acct
|
|
|
|
|
.object
|
|
|
|
|
.object_props
|
|
|
|
|
.summary_string()
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
summary_html: SafeString::new(
|
|
|
|
|
&acct
|
|
|
|
|
.object
|
|
|
|
|
.object_props
|
|
|
|
|
.summary_string()
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
),
|
|
|
|
|
email: None,
|
|
|
|
|
hashed_password: None,
|
|
|
|
|
instance_id: instance.id,
|
|
|
|
|
ap_url: acct.object.object_props.id_string()?,
|
|
|
|
|
public_key: acct
|
|
|
|
|
.custom_props
|
|
|
|
|
.public_key_publickey()?
|
|
|
|
|
.public_key_pem_string()?,
|
|
|
|
|
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()?,
|
2019-08-27 20:28:40 +00:00
|
|
|
|
fqn,
|
2019-04-17 17:31:47 +00:00
|
|
|
|
avatar_id: None,
|
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
if let Ok(icon) = acct.object.object_props.icon_image() {
|
|
|
|
|
if let Ok(url) = icon.object_props.url_string() {
|
2021-01-30 10:24:16 +00:00
|
|
|
|
let avatar = Media::save_remote(conn, url, &user);
|
2019-04-17 17:31:47 +00:00
|
|
|
|
|
|
|
|
|
if let Ok(avatar) = avatar {
|
2021-01-30 10:24:16 +00:00
|
|
|
|
user.set_avatar(conn, avatar.id)?;
|
2019-04-17 17:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
|
}
|
2021-11-24 13:50:16 +00:00
|
|
|
|
|
|
|
|
|
fn get_sender() -> &'static dyn Signer {
|
|
|
|
|
Instance::get_local_instance_user().expect("Failed to local instance user")
|
|
|
|
|
}
|
2019-04-17 17:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 05:49:52 +00:00
|
|
|
|
impl FromId07<DbConn> for User {
|
|
|
|
|
type Error = Error;
|
|
|
|
|
type Object = CustomPerson07;
|
|
|
|
|
|
|
|
|
|
fn from_db07(conn: &DbConn, id: &str) -> Result<Self> {
|
|
|
|
|
Self::find_by_ap_url(conn, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_activity07(conn: &DbConn, acct: CustomPerson07) -> Result<Self> {
|
2022-02-12 13:32:19 +00:00
|
|
|
|
let actor = acct.ap_actor_ref();
|
|
|
|
|
let username = actor
|
|
|
|
|
.preferred_username()
|
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
|
|
if username.contains(&['<', '>', '&', '@', '\'', '"', ' ', '\t'][..]) {
|
|
|
|
|
return Err(Error::InvalidValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let summary = acct
|
|
|
|
|
.object_ref()
|
|
|
|
|
.summary()
|
2022-02-12 15:53:08 +00:00
|
|
|
|
.and_then(|prop| prop.to_as_string())
|
2022-02-12 13:32:19 +00:00
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
let mut new_user = NewUser {
|
|
|
|
|
display_name: acct
|
|
|
|
|
.object_ref()
|
|
|
|
|
.name()
|
2022-02-12 15:53:08 +00:00
|
|
|
|
.and_then(|prop| prop.to_as_string())
|
|
|
|
|
.unwrap_or_else(|| username.clone()),
|
|
|
|
|
username: username.clone(),
|
2022-02-12 13:32:19 +00:00
|
|
|
|
outbox_url: actor.outbox()?.ok_or(Error::MissingApProperty)?.to_string(),
|
|
|
|
|
inbox_url: actor.inbox()?.to_string(),
|
|
|
|
|
role: 2,
|
2022-02-12 15:53:08 +00:00
|
|
|
|
summary_html: SafeString::new(&summary),
|
|
|
|
|
summary,
|
2022-02-12 13:32:19 +00:00
|
|
|
|
public_key: acct.ext_one.public_key.public_key_pem.to_string(),
|
|
|
|
|
shared_inbox_url: actor
|
|
|
|
|
.endpoints()?
|
|
|
|
|
.and_then(|e| e.shared_inbox.map(|inbox| inbox.to_string())),
|
|
|
|
|
followers_endpoint: actor
|
|
|
|
|
.followers()?
|
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
|
.to_string(),
|
|
|
|
|
..NewUser::default()
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-12 16:31:15 +00:00
|
|
|
|
let avatar_id = acct.object_ref().icon().and_then(|icon| icon.to_as_uri());
|
2022-02-12 13:32:19 +00:00
|
|
|
|
|
|
|
|
|
let (ap_url, inst) = {
|
|
|
|
|
let any_base = acct.into_any_base()?;
|
|
|
|
|
let id = any_base.id().ok_or(Error::MissingApProperty)?;
|
|
|
|
|
(
|
|
|
|
|
id.to_string(),
|
|
|
|
|
id.authority_components()
|
|
|
|
|
.ok_or(Error::Url)?
|
|
|
|
|
.host()
|
|
|
|
|
.to_string(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
new_user.ap_url = ap_url;
|
|
|
|
|
|
|
|
|
|
let instance = Instance::find_by_domain(conn, &inst).or_else(|_| {
|
|
|
|
|
Instance::insert(
|
|
|
|
|
conn,
|
|
|
|
|
NewInstance {
|
|
|
|
|
name: inst.to_owned(),
|
|
|
|
|
public_domain: inst.to_owned(),
|
|
|
|
|
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(),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
new_user.instance_id = instance.id;
|
|
|
|
|
new_user.fqn = if instance.local {
|
2022-02-12 15:53:08 +00:00
|
|
|
|
username
|
2022-02-12 13:32:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
format!("{}@{}", username, instance.public_domain)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let user = User::insert(conn, new_user)?;
|
|
|
|
|
if let Some(avatar_id) = avatar_id {
|
|
|
|
|
let avatar = Media::save_remote(conn, avatar_id, &user);
|
|
|
|
|
|
|
|
|
|
if let Ok(avatar) = avatar {
|
|
|
|
|
if let Err(e) = user.set_avatar(conn, avatar.id) {
|
|
|
|
|
tracing::error!("{:?}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(user)
|
2022-02-11 05:49:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_sender07() -> &'static dyn Signer {
|
|
|
|
|
Instance::get_local_instance_user().expect("Failed to local instance user")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
impl AsActor<&DbConn> for User {
|
2018-05-18 08:04:40 +00:00
|
|
|
|
fn get_inbox_url(&self) -> String {
|
|
|
|
|
self.inbox_url.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_shared_inbox_url(&self) -> Option<String> {
|
2018-11-24 11:44:17 +00:00
|
|
|
|
self.shared_inbox_url.clone()
|
2018-05-18 08:04:40 +00:00
|
|
|
|
}
|
2018-07-18 13:49:13 +00:00
|
|
|
|
|
|
|
|
|
fn is_local(&self) -> bool {
|
2019-05-10 20:59:34 +00:00
|
|
|
|
Instance::get_local()
|
|
|
|
|
.map(|i| self.instance_id == i.id)
|
|
|
|
|
.unwrap_or(false)
|
2018-07-18 13:49:13 +00:00
|
|
|
|
}
|
2018-05-18 08:04:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
impl AsObject<User, Delete, &DbConn> for User {
|
2019-04-28 17:01:41 +00:00
|
|
|
|
type Error = Error;
|
|
|
|
|
type Output = ();
|
|
|
|
|
|
2021-01-30 10:24:16 +00:00
|
|
|
|
fn activity(self, conn: &DbConn, actor: User, _id: &str) -> Result<()> {
|
2019-04-28 17:01:41 +00:00
|
|
|
|
if self.id == actor.id {
|
2021-01-30 10:24:16 +00:00
|
|
|
|
self.delete(conn).map(|_| ())
|
2019-04-28 17:01:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
Err(Error::Unauthorized)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 16:43:01 +00:00
|
|
|
|
impl AsObject07<User, Delete07, &DbConn> for User {
|
|
|
|
|
type Error = Error;
|
|
|
|
|
type Output = ();
|
|
|
|
|
|
|
|
|
|
fn activity07(self, conn: &DbConn, actor: User, _id: &str) -> Result<()> {
|
|
|
|
|
if self.id == actor.id {
|
|
|
|
|
self.delete(conn).map(|_| ())
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::Unauthorized)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 19:11:04 +00:00
|
|
|
|
impl Signer for User {
|
2018-06-21 15:25:32 +00:00
|
|
|
|
fn get_key_id(&self) -> String {
|
2018-06-21 14:48:54 +00:00
|
|
|
|
format!("{}#main-key", self.ap_url)
|
2018-05-03 17:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 12:41:44 +00:00
|
|
|
|
fn sign(&self, to_sign: &str) -> SignResult<Vec<u8>> {
|
|
|
|
|
let key = self.get_keypair().map_err(|_| SignError())?;
|
2018-12-29 08:36:07 +00:00
|
|
|
|
let mut signer = sign::Signer::new(MessageDigest::sha256(), &key)?;
|
2019-03-20 16:56:17 +00:00
|
|
|
|
signer.update(to_sign.as_bytes())?;
|
2021-11-24 12:41:44 +00:00
|
|
|
|
signer.sign_to_vec().map_err(SignError::from)
|
2018-05-03 17:12:01 +00:00
|
|
|
|
}
|
2018-09-28 21:18:01 +00:00
|
|
|
|
|
2021-11-24 12:41:44 +00:00
|
|
|
|
fn verify(&self, data: &str, signature: &[u8]) -> SignResult<bool> {
|
2018-12-29 08:36:07 +00:00
|
|
|
|
let key = PKey::from_rsa(Rsa::public_key_from_pem(self.public_key.as_ref())?)?;
|
|
|
|
|
let mut verifier = sign::Verifier::new(MessageDigest::sha256(), &key)?;
|
2019-03-20 16:56:17 +00:00
|
|
|
|
verifier.update(data.as_bytes())?;
|
2021-11-24 12:41:44 +00:00
|
|
|
|
verifier.verify(signature).map_err(SignError::from)
|
2018-09-28 21:18:01 +00:00
|
|
|
|
}
|
2018-05-03 17:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 17:54:16 +00:00
|
|
|
|
impl PartialEq for User {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.id == other.id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-24 10:23:04 +00:00
|
|
|
|
impl Hash for User {
|
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.id.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 13:12:59 +00:00
|
|
|
|
impl NewUser {
|
|
|
|
|
/// Creates a new local user
|
|
|
|
|
pub fn new_local(
|
2018-09-26 15:22:42 +00:00
|
|
|
|
conn: &Connection,
|
2018-04-23 13:12:59 +00:00
|
|
|
|
username: String,
|
|
|
|
|
display_name: String,
|
2019-09-13 10:28:36 +00:00
|
|
|
|
role: Role,
|
2018-11-26 09:21:52 +00:00
|
|
|
|
summary: &str,
|
2018-04-23 13:12:59 +00:00
|
|
|
|
email: String,
|
2020-10-04 10:18:22 +00:00
|
|
|
|
password: Option<String>,
|
2018-12-29 08:36:07 +00:00
|
|
|
|
) -> Result<User> {
|
2018-05-03 19:11:04 +00:00
|
|
|
|
let (pub_key, priv_key) = gen_keypair();
|
2019-08-27 20:28:40 +00:00
|
|
|
|
let instance = Instance::get_local()?;
|
2020-01-12 18:41:35 +00:00
|
|
|
|
let blocklisted = BlocklistedEmail::matches_blocklist(conn, &email)?;
|
|
|
|
|
if let Some(x) = blocklisted {
|
|
|
|
|
return Err(Error::Blocklisted(x.notify_user, x.notification_text));
|
|
|
|
|
}
|
2019-08-27 20:28:40 +00:00
|
|
|
|
|
2019-10-07 17:08:20 +00:00
|
|
|
|
let res = User::insert(
|
2018-11-24 11:44:17 +00:00
|
|
|
|
conn,
|
|
|
|
|
NewUser {
|
2019-08-27 20:28:40 +00:00
|
|
|
|
username: username.clone(),
|
2018-11-26 09:21:52 +00:00
|
|
|
|
display_name,
|
2019-09-13 10:28:36 +00:00
|
|
|
|
role: role as i32,
|
2019-03-17 19:11:29 +00:00
|
|
|
|
summary: summary.to_owned(),
|
2021-11-27 22:53:13 +00:00
|
|
|
|
summary_html: SafeString::new(&utils::md_to_html(summary, None, false, None).0),
|
2018-11-24 11:44:17 +00:00
|
|
|
|
email: Some(email),
|
2020-10-04 10:18:22 +00:00
|
|
|
|
hashed_password: password,
|
2019-08-27 20:28:40 +00:00
|
|
|
|
instance_id: instance.id,
|
2019-03-04 20:35:03 +00:00
|
|
|
|
public_key: String::from_utf8(pub_key).or(Err(Error::Signature))?,
|
|
|
|
|
private_key: Some(String::from_utf8(priv_key).or(Err(Error::Signature))?),
|
2019-08-27 20:28:40 +00:00
|
|
|
|
outbox_url: instance.compute_box(USER_PREFIX, &username, "outbox"),
|
|
|
|
|
inbox_url: instance.compute_box(USER_PREFIX, &username, "inbox"),
|
|
|
|
|
ap_url: instance.compute_box(USER_PREFIX, &username, ""),
|
|
|
|
|
shared_inbox_url: Some(ap_url(&format!("{}/inbox", &instance.public_domain))),
|
|
|
|
|
followers_endpoint: instance.compute_box(USER_PREFIX, &username, "followers"),
|
|
|
|
|
fqn: username,
|
|
|
|
|
avatar_id: None,
|
2018-11-24 11:44:17 +00:00
|
|
|
|
},
|
2019-10-07 17:08:20 +00:00
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// create default timeline
|
|
|
|
|
Timeline::new_for_user(conn, res.id, "My feed".into(), "followed".into())?;
|
|
|
|
|
|
|
|
|
|
Ok(res)
|
2018-11-24 11:44:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 13:55:28 +00:00
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum UserEvent {
|
|
|
|
|
RemoteUserFound(Arc<User>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<UserEvent> for Arc<User> {
|
|
|
|
|
fn from(event: UserEvent) -> Self {
|
|
|
|
|
use UserEvent::*;
|
|
|
|
|
|
|
|
|
|
match event {
|
|
|
|
|
RemoteUserFound(user) => user,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 11:44:17 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
pub(crate) mod tests {
|
|
|
|
|
use super::*;
|
2020-01-21 06:02:03 +00:00
|
|
|
|
use crate::{
|
|
|
|
|
instance::{tests as instance_tests, Instance},
|
2022-01-10 06:08:11 +00:00
|
|
|
|
medias::{Media, NewMedia},
|
2021-01-30 14:15:07 +00:00
|
|
|
|
tests::db,
|
2022-01-10 13:19:44 +00:00
|
|
|
|
Connection as Conn, ITEMS_PER_PAGE,
|
2020-01-21 06:02:03 +00:00
|
|
|
|
};
|
2022-01-10 13:19:44 +00:00
|
|
|
|
use assert_json_diff::assert_json_eq;
|
|
|
|
|
use diesel::{Connection, SaveChangesDsl};
|
|
|
|
|
use serde_json::to_value;
|
2018-11-24 11:44:17 +00:00
|
|
|
|
|
|
|
|
|
pub(crate) fn fill_database(conn: &Conn) -> Vec<User> {
|
|
|
|
|
instance_tests::fill_database(conn);
|
2018-12-09 17:44:26 +00:00
|
|
|
|
let admin = NewUser::new_local(
|
|
|
|
|
conn,
|
|
|
|
|
"admin".to_owned(),
|
|
|
|
|
"The admin".to_owned(),
|
2019-09-13 10:28:36 +00:00
|
|
|
|
Role::Admin,
|
2018-12-09 17:44:26 +00:00
|
|
|
|
"Hello there, I'm the admin",
|
|
|
|
|
"admin@example.com".to_owned(),
|
2020-10-08 17:59:54 +00:00
|
|
|
|
Some("invalid_admin_password".to_owned()),
|
2019-03-20 16:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2018-12-09 17:44:26 +00:00
|
|
|
|
let user = NewUser::new_local(
|
|
|
|
|
conn,
|
|
|
|
|
"user".to_owned(),
|
|
|
|
|
"Some user".to_owned(),
|
2019-09-13 10:28:36 +00:00
|
|
|
|
Role::Normal,
|
2018-12-09 17:44:26 +00:00
|
|
|
|
"Hello there, I'm no one",
|
|
|
|
|
"user@example.com".to_owned(),
|
2020-10-08 17:59:54 +00:00
|
|
|
|
Some("invalid_user_password".to_owned()),
|
2019-03-20 16:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2022-01-10 13:19:44 +00:00
|
|
|
|
let mut other = NewUser::new_local(
|
2018-12-09 17:44:26 +00:00
|
|
|
|
conn,
|
|
|
|
|
"other".to_owned(),
|
|
|
|
|
"Another user".to_owned(),
|
2019-09-13 10:28:36 +00:00
|
|
|
|
Role::Normal,
|
2018-12-09 17:44:26 +00:00
|
|
|
|
"Hello there, I'm someone else",
|
|
|
|
|
"other@example.com".to_owned(),
|
2020-10-08 17:59:54 +00:00
|
|
|
|
Some("invalid_other_password".to_owned()),
|
2019-03-20 16:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2022-01-10 13:19:44 +00:00
|
|
|
|
let avatar = Media::insert(
|
2022-01-10 06:08:11 +00:00
|
|
|
|
conn,
|
|
|
|
|
NewMedia {
|
2022-01-10 13:19:44 +00:00
|
|
|
|
file_path: "static/media/example.png".into(),
|
2022-01-10 06:08:11 +00:00
|
|
|
|
alt_text: "Another user".into(),
|
|
|
|
|
is_remote: false,
|
|
|
|
|
remote_url: None,
|
|
|
|
|
sensitive: false,
|
|
|
|
|
content_warning: None,
|
|
|
|
|
owner_id: other.id,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2022-01-10 13:19:44 +00:00
|
|
|
|
other.avatar_id = Some(avatar.id);
|
|
|
|
|
let other = other.save_changes::<User>(&*conn).unwrap();
|
2022-01-10 06:08:11 +00:00
|
|
|
|
|
2019-03-20 16:56:17 +00:00
|
|
|
|
vec![admin, user, other]
|
2018-11-24 11:44:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 13:19:44 +00:00
|
|
|
|
fn fill_pages(
|
|
|
|
|
conn: &DbConn,
|
|
|
|
|
) -> (
|
|
|
|
|
Vec<crate::posts::Post>,
|
|
|
|
|
Vec<crate::users::User>,
|
|
|
|
|
Vec<crate::blogs::Blog>,
|
|
|
|
|
) {
|
|
|
|
|
use crate::post_authors::NewPostAuthor;
|
|
|
|
|
use crate::posts::NewPost;
|
|
|
|
|
|
|
|
|
|
let (mut posts, users, blogs) = crate::inbox::tests::fill_database(conn);
|
|
|
|
|
let user = &users[0];
|
|
|
|
|
let blog = &blogs[0];
|
|
|
|
|
|
|
|
|
|
for i in 1..(ITEMS_PER_PAGE * 4 + 3) {
|
|
|
|
|
let title = format!("Post {}", i);
|
|
|
|
|
let content = format!("Content for post {}.", i);
|
|
|
|
|
let post = Post::insert(
|
|
|
|
|
conn,
|
|
|
|
|
NewPost {
|
|
|
|
|
blog_id: blog.id,
|
|
|
|
|
slug: title.clone(),
|
|
|
|
|
title: title.clone(),
|
|
|
|
|
content: SafeString::new(&content),
|
|
|
|
|
published: true,
|
|
|
|
|
license: "CC-0".into(),
|
|
|
|
|
creation_date: None,
|
|
|
|
|
ap_url: format!("{}/{}", blog.ap_url, title),
|
|
|
|
|
subtitle: "".into(),
|
|
|
|
|
source: content,
|
|
|
|
|
cover_id: None,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
PostAuthor::insert(
|
|
|
|
|
conn,
|
|
|
|
|
NewPostAuthor {
|
|
|
|
|
post_id: post.id,
|
|
|
|
|
author_id: user.id,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
posts.push(post);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(posts, users, blogs)
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 11:44:17 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn find_by() {
|
2021-01-30 14:15:07 +00:00
|
|
|
|
let conn = db();
|
2018-11-24 11:44:17 +00:00
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 14:15:07 +00:00
|
|
|
|
fill_database(&conn);
|
2018-11-24 11:44:17 +00:00
|
|
|
|
let test_user = NewUser::new_local(
|
2021-01-30 14:15:07 +00:00
|
|
|
|
&conn,
|
2018-11-24 11:44:17 +00:00
|
|
|
|
"test".to_owned(),
|
|
|
|
|
"test user".to_owned(),
|
2019-09-13 10:28:36 +00:00
|
|
|
|
Role::Normal,
|
2018-11-26 09:21:52 +00:00
|
|
|
|
"Hello I'm a test",
|
2018-11-24 11:44:17 +00:00
|
|
|
|
"test@example.com".to_owned(),
|
2020-10-08 17:59:54 +00:00
|
|
|
|
Some(User::hash_pass("test_password").unwrap()),
|
2019-03-20 16:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2018-11-24 11:44:17 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
test_user.id,
|
2021-01-30 14:15:07 +00:00
|
|
|
|
User::find_by_name(&conn, "test", Instance::get_local().unwrap().id)
|
2018-11-24 11:44:17 +00:00
|
|
|
|
.unwrap()
|
|
|
|
|
.id
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
test_user.id,
|
2021-01-30 14:15:07 +00:00
|
|
|
|
User::find_by_fqn(&conn, &test_user.fqn).unwrap().id
|
2018-11-24 11:44:17 +00:00
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
test_user.id,
|
2021-01-30 14:15:07 +00:00
|
|
|
|
User::find_by_email(&conn, "test@example.com").unwrap().id
|
2018-11-24 11:44:17 +00:00
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
test_user.id,
|
|
|
|
|
User::find_by_ap_url(
|
2021-01-30 14:15:07 +00:00
|
|
|
|
&conn,
|
2018-11-26 09:21:52 +00:00
|
|
|
|
&format!(
|
2018-11-24 11:44:17 +00:00
|
|
|
|
"https://{}/@/{}/",
|
2019-05-10 20:59:34 +00:00
|
|
|
|
Instance::get_local().unwrap().public_domain,
|
2018-11-24 11:44:17 +00:00
|
|
|
|
"test"
|
|
|
|
|
)
|
2019-03-20 16:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.id
|
2018-11-24 11:44:17 +00:00
|
|
|
|
);
|
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn delete() {
|
|
|
|
|
let conn = &db();
|
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 14:15:07 +00:00
|
|
|
|
let inserted = fill_database(&conn);
|
2018-11-24 11:44:17 +00:00
|
|
|
|
|
2021-01-30 14:15:07 +00:00
|
|
|
|
assert!(User::get(&conn, inserted[0].id).is_ok());
|
|
|
|
|
inserted[0].delete(&conn).unwrap();
|
|
|
|
|
assert!(User::get(&conn, inserted[0].id).is_err());
|
2018-11-24 11:44:17 +00:00
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn admin() {
|
|
|
|
|
let conn = &db();
|
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 14:15:07 +00:00
|
|
|
|
let inserted = fill_database(&conn);
|
2019-05-10 20:59:34 +00:00
|
|
|
|
let local_inst = Instance::get_local().unwrap();
|
2018-11-24 11:44:17 +00:00
|
|
|
|
let mut i = 0;
|
2021-01-30 14:15:07 +00:00
|
|
|
|
while local_inst.has_admin(&conn).unwrap() {
|
2018-11-24 11:44:17 +00:00
|
|
|
|
assert!(i < 100); //prevent from looping indefinitelly
|
2019-03-20 16:56:17 +00:00
|
|
|
|
local_inst
|
2021-01-30 14:15:07 +00:00
|
|
|
|
.main_admin(&conn)
|
2019-03-20 16:56:17 +00:00
|
|
|
|
.unwrap()
|
2021-01-30 14:15:07 +00:00
|
|
|
|
.set_role(&conn, Role::Normal)
|
2019-03-20 16:56:17 +00:00
|
|
|
|
.unwrap();
|
2018-11-24 11:44:17 +00:00
|
|
|
|
i += 1;
|
|
|
|
|
}
|
2021-01-30 14:15:07 +00:00
|
|
|
|
inserted[0].set_role(&conn, Role::Admin).unwrap();
|
|
|
|
|
assert_eq!(inserted[0].id, local_inst.main_admin(&conn).unwrap().id);
|
2018-11-24 11:44:17 +00:00
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn auth() {
|
|
|
|
|
let conn = &db();
|
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 14:15:07 +00:00
|
|
|
|
fill_database(&conn);
|
2018-11-24 11:44:17 +00:00
|
|
|
|
let test_user = NewUser::new_local(
|
2021-01-30 14:15:07 +00:00
|
|
|
|
&conn,
|
2018-11-24 11:44:17 +00:00
|
|
|
|
"test".to_owned(),
|
|
|
|
|
"test user".to_owned(),
|
2019-09-13 10:28:36 +00:00
|
|
|
|
Role::Normal,
|
2018-11-26 09:21:52 +00:00
|
|
|
|
"Hello I'm a test",
|
2018-11-24 11:44:17 +00:00
|
|
|
|
"test@example.com".to_owned(),
|
2020-10-08 17:59:54 +00:00
|
|
|
|
Some(User::hash_pass("test_password").unwrap()),
|
2019-03-20 16:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2018-11-24 11:44:17 +00:00
|
|
|
|
|
2020-10-08 18:24:03 +00:00
|
|
|
|
assert_eq!(
|
2021-01-30 14:15:07 +00:00
|
|
|
|
User::login(&conn, "test", "test_password").unwrap().id,
|
2020-10-08 18:24:03 +00:00
|
|
|
|
test_user.id
|
|
|
|
|
);
|
2021-01-30 14:15:07 +00:00
|
|
|
|
assert!(User::login(&conn, "test", "other_password").is_err());
|
2018-11-24 11:44:17 +00:00
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn get_local_page() {
|
|
|
|
|
let conn = &db();
|
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 14:15:07 +00:00
|
|
|
|
fill_database(&conn);
|
2018-11-24 11:44:17 +00:00
|
|
|
|
|
2021-01-30 14:15:07 +00:00
|
|
|
|
let page = User::get_local_page(&conn, (0, 2)).unwrap();
|
2018-11-24 11:44:17 +00:00
|
|
|
|
assert_eq!(page.len(), 2);
|
|
|
|
|
assert!(page[0].username <= page[1].username);
|
|
|
|
|
|
2021-01-30 14:15:07 +00:00
|
|
|
|
let mut last_username = User::get_local_page(&conn, (0, 1)).unwrap()[0]
|
2019-03-20 16:56:17 +00:00
|
|
|
|
.username
|
|
|
|
|
.clone();
|
2021-01-30 14:15:07 +00:00
|
|
|
|
for i in 1..User::count_local(&conn).unwrap() as i32 {
|
|
|
|
|
let page = User::get_local_page(&conn, (i, i + 1)).unwrap();
|
2018-11-24 11:44:17 +00:00
|
|
|
|
assert_eq!(page.len(), 1);
|
|
|
|
|
assert!(last_username <= page[0].username);
|
|
|
|
|
last_username = page[0].username.clone();
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(
|
2021-01-30 14:15:07 +00:00
|
|
|
|
User::get_local_page(&conn, (0, User::count_local(&conn).unwrap() as i32 + 10))
|
2019-03-20 16:56:17 +00:00
|
|
|
|
.unwrap()
|
|
|
|
|
.len() as i64,
|
2021-01-30 14:15:07 +00:00
|
|
|
|
User::count_local(&conn).unwrap()
|
2018-11-24 11:44:17 +00:00
|
|
|
|
);
|
|
|
|
|
Ok(())
|
|
|
|
|
});
|
2018-04-23 13:12:59 +00:00
|
|
|
|
}
|
2019-04-17 17:31:47 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn self_federation() {
|
2021-01-30 14:15:07 +00:00
|
|
|
|
let conn = db();
|
2019-04-17 17:31:47 +00:00
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 14:15:07 +00:00
|
|
|
|
let users = fill_database(&conn);
|
2019-04-17 17:31:47 +00:00
|
|
|
|
|
2021-01-30 14:15:07 +00:00
|
|
|
|
let ap_repr = users[0].to_activity(&conn).unwrap();
|
|
|
|
|
users[0].delete(&conn).unwrap();
|
|
|
|
|
let user = User::from_activity(&conn, ap_repr).unwrap();
|
2019-04-17 17:31:47 +00:00
|
|
|
|
|
|
|
|
|
assert_eq!(user.username, users[0].username);
|
|
|
|
|
assert_eq!(user.display_name, users[0].display_name);
|
|
|
|
|
assert_eq!(user.outbox_url, users[0].outbox_url);
|
|
|
|
|
assert_eq!(user.inbox_url, users[0].inbox_url);
|
|
|
|
|
assert_eq!(user.instance_id, users[0].instance_id);
|
|
|
|
|
assert_eq!(user.ap_url, users[0].ap_url);
|
|
|
|
|
assert_eq!(user.public_key, users[0].public_key);
|
|
|
|
|
assert_eq!(user.shared_inbox_url, users[0].shared_inbox_url);
|
|
|
|
|
assert_eq!(user.followers_endpoint, users[0].followers_endpoint);
|
2021-01-30 14:15:07 +00:00
|
|
|
|
assert_eq!(user.avatar_url(&conn), users[0].avatar_url(&conn));
|
2019-04-17 17:31:47 +00:00
|
|
|
|
assert_eq!(user.fqn, users[0].fqn);
|
|
|
|
|
assert_eq!(user.summary_html, users[0].summary_html);
|
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-01-10 13:19:44 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn to_activity() {
|
|
|
|
|
let conn = db();
|
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
|
let users = fill_database(&conn);
|
|
|
|
|
let user = &users[0];
|
|
|
|
|
let act = user.to_activity(&conn)?;
|
|
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
|
"endpoints": {
|
|
|
|
|
"sharedInbox": "https://plu.me/inbox"
|
|
|
|
|
},
|
|
|
|
|
"followers": "https://plu.me/@/admin/followers",
|
|
|
|
|
"following": null,
|
|
|
|
|
"id": "https://plu.me/@/admin/",
|
|
|
|
|
"inbox": "https://plu.me/@/admin/inbox",
|
|
|
|
|
"liked": null,
|
|
|
|
|
"name": "The admin",
|
|
|
|
|
"outbox": "https://plu.me/@/admin/outbox",
|
|
|
|
|
"preferredUsername": "admin",
|
|
|
|
|
"publicKey": {
|
|
|
|
|
"id": "https://plu.me/@/admin/#main-key",
|
|
|
|
|
"owner": "https://plu.me/@/admin/",
|
|
|
|
|
"publicKeyPem": user.public_key,
|
|
|
|
|
},
|
|
|
|
|
"summary": "<p dir=\"auto\">Hello there, I’m the admin</p>\n",
|
|
|
|
|
"type": "Person",
|
|
|
|
|
"url": "https://plu.me/@/admin/"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
|
|
let other = &users[2];
|
|
|
|
|
let other_act = other.to_activity(&conn)?;
|
|
|
|
|
let expected_other = json!({
|
|
|
|
|
"endpoints": {
|
|
|
|
|
"sharedInbox": "https://plu.me/inbox"
|
|
|
|
|
},
|
|
|
|
|
"followers": "https://plu.me/@/other/followers",
|
|
|
|
|
"following": null,
|
|
|
|
|
"icon": {
|
|
|
|
|
"url": "https://plu.me/static/media/example.png",
|
|
|
|
|
"type": "Image",
|
|
|
|
|
},
|
|
|
|
|
"id": "https://plu.me/@/other/",
|
|
|
|
|
"inbox": "https://plu.me/@/other/inbox",
|
|
|
|
|
"liked": null,
|
|
|
|
|
"name": "Another user",
|
|
|
|
|
"outbox": "https://plu.me/@/other/outbox",
|
|
|
|
|
"preferredUsername": "other",
|
|
|
|
|
"publicKey": {
|
|
|
|
|
"id": "https://plu.me/@/other/#main-key",
|
|
|
|
|
"owner": "https://plu.me/@/other/",
|
|
|
|
|
"publicKeyPem": other.public_key,
|
|
|
|
|
},
|
|
|
|
|
"summary": "<p dir=\"auto\">Hello there, I’m someone else</p>\n",
|
|
|
|
|
"type": "Person",
|
|
|
|
|
"url": "https://plu.me/@/other/"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(other_act)?, expected_other);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn delete_activity() {
|
|
|
|
|
let conn = db();
|
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
|
let users = fill_database(&conn);
|
|
|
|
|
let user = &users[1];
|
|
|
|
|
let act = user.delete_activity(&conn)?;
|
|
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
|
"actor": "https://plu.me/@/user/",
|
|
|
|
|
"cc": [],
|
|
|
|
|
"id": "https://plu.me/@/user/#delete",
|
|
|
|
|
"object": {
|
|
|
|
|
"id": "https://plu.me/@/user/",
|
|
|
|
|
"type": "Tombstone",
|
|
|
|
|
},
|
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
|
"type": "Delete",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn outbox_collection() {
|
|
|
|
|
let conn = db();
|
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
|
let (_pages, users, _blogs) = fill_pages(&conn);
|
|
|
|
|
let user = &users[0];
|
|
|
|
|
let act = user.outbox_collection(&conn)?;
|
|
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
|
"first": "https://plu.me/@/admin/outbox?page=1",
|
|
|
|
|
"items": null,
|
|
|
|
|
"last": "https://plu.me/@/admin/outbox?page=5",
|
|
|
|
|
"totalItems": 51,
|
|
|
|
|
"type": "OrderedCollection",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn outbox_collection_page() {
|
|
|
|
|
let conn = db();
|
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
|
let users = fill_database(&conn);
|
|
|
|
|
let user = &users[0];
|
|
|
|
|
let act = user.outbox_collection_page(&conn, (33, 36))?;
|
|
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
|
"items": [],
|
|
|
|
|
"partOf": "https://plu.me/@/admin/outbox",
|
|
|
|
|
"prev": "https://plu.me/@/admin/outbox?page=2",
|
|
|
|
|
"type": "OrderedCollectionPage",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-04-23 13:12:59 +00:00
|
|
|
|
}
|