2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::{DbUrl, PersonId},
|
2021-02-26 13:49:58 +00:00
|
|
|
schema::person::dsl::*,
|
2022-10-27 09:24:07 +00:00
|
|
|
source::person::{Person, PersonInsertForm, PersonUpdateForm},
|
2022-01-27 16:39:22 +00:00
|
|
|
traits::{ApubActor, Crud},
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{functions::lower, naive_now},
|
2022-01-27 16:39:22 +00:00
|
|
|
};
|
|
|
|
use diesel::{
|
|
|
|
dsl::*,
|
|
|
|
result::Error,
|
|
|
|
ExpressionMethods,
|
|
|
|
PgConnection,
|
|
|
|
QueryDsl,
|
|
|
|
RunQueryDsl,
|
|
|
|
TextExpressionMethods,
|
2021-02-26 13:49:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mod safe_type {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::{schema::person::columns::*, source::person::Person, traits::ToSafe};
|
2021-02-26 13:49:58 +00:00
|
|
|
|
|
|
|
type Columns = (
|
2021-03-11 04:43:11 +00:00
|
|
|
id,
|
|
|
|
name,
|
2021-04-01 17:57:45 +00:00
|
|
|
display_name,
|
2021-03-11 04:43:11 +00:00
|
|
|
avatar,
|
|
|
|
banned,
|
|
|
|
published,
|
|
|
|
updated,
|
|
|
|
actor_id,
|
|
|
|
bio,
|
|
|
|
local,
|
|
|
|
banner,
|
|
|
|
deleted,
|
|
|
|
inbox_url,
|
|
|
|
shared_inbox_url,
|
2021-03-20 19:21:51 +00:00
|
|
|
matrix_user_id,
|
2021-03-22 14:28:00 +00:00
|
|
|
admin,
|
2021-04-21 21:41:14 +00:00
|
|
|
bot_account,
|
2022-01-08 12:37:07 +00:00
|
|
|
ban_expires,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance_id,
|
2021-02-26 13:49:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
impl ToSafe for Person {
|
|
|
|
type SafeColumns = Columns;
|
|
|
|
fn safe_columns_tuple() -> Self::SafeColumns {
|
|
|
|
(
|
|
|
|
id,
|
|
|
|
name,
|
2021-04-01 17:57:45 +00:00
|
|
|
display_name,
|
2021-02-26 13:49:58 +00:00
|
|
|
avatar,
|
|
|
|
banned,
|
|
|
|
published,
|
|
|
|
updated,
|
|
|
|
actor_id,
|
|
|
|
bio,
|
|
|
|
local,
|
|
|
|
banner,
|
|
|
|
deleted,
|
|
|
|
inbox_url,
|
|
|
|
shared_inbox_url,
|
2021-03-20 19:21:51 +00:00
|
|
|
matrix_user_id,
|
2021-03-22 14:28:00 +00:00
|
|
|
admin,
|
2021-04-21 21:41:14 +00:00
|
|
|
bot_account,
|
2022-01-08 12:37:07 +00:00
|
|
|
ban_expires,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance_id,
|
2021-02-26 13:49:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for Person {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = PersonInsertForm;
|
|
|
|
type UpdateForm = PersonUpdateForm;
|
2021-08-17 18:04:58 +00:00
|
|
|
type IdType = PersonId;
|
2022-09-26 14:09:32 +00:00
|
|
|
fn read(conn: &mut PgConnection, person_id: PersonId) -> Result<Self, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
person
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.find(person_id)
|
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
2022-09-26 14:09:32 +00:00
|
|
|
fn delete(conn: &mut PgConnection, person_id: PersonId) -> Result<usize, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
diesel::delete(person.find(person_id)).execute(conn)
|
|
|
|
}
|
2022-10-27 09:24:07 +00:00
|
|
|
fn create(conn: &mut PgConnection, form: &PersonInsertForm) -> Result<Self, Error> {
|
|
|
|
insert_into(person)
|
|
|
|
.values(form)
|
|
|
|
.on_conflict(actor_id)
|
|
|
|
.do_update()
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2021-02-26 13:49:58 +00:00
|
|
|
}
|
2022-09-26 14:09:32 +00:00
|
|
|
fn update(
|
|
|
|
conn: &mut PgConnection,
|
|
|
|
person_id: PersonId,
|
2022-10-27 09:24:07 +00:00
|
|
|
form: &PersonUpdateForm,
|
2022-09-26 14:09:32 +00:00
|
|
|
) -> Result<Self, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
diesel::update(person.find(person_id))
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl Person {
|
2022-09-26 14:09:32 +00:00
|
|
|
pub fn delete_account(conn: &mut PgConnection, person_id: PersonId) -> Result<Person, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::local_user;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
|
|
|
// Set the local user info to none
|
|
|
|
diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
|
2021-05-28 15:50:52 +00:00
|
|
|
.set((
|
|
|
|
local_user::email.eq::<Option<String>>(None),
|
|
|
|
local_user::validator_time.eq(naive_now()),
|
|
|
|
))
|
2021-03-10 22:33:55 +00:00
|
|
|
.execute(conn)?;
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
diesel::update(person.find(person_id))
|
|
|
|
.set((
|
2021-04-01 17:57:45 +00:00
|
|
|
display_name.eq::<Option<String>>(None),
|
2022-06-13 19:15:04 +00:00
|
|
|
avatar.eq::<Option<String>>(None),
|
|
|
|
banner.eq::<Option<String>>(None),
|
2021-02-26 13:49:58 +00:00
|
|
|
bio.eq::<Option<String>>(None),
|
2021-03-20 19:21:51 +00:00
|
|
|
matrix_user_id.eq::<Option<String>>(None),
|
2021-02-26 13:49:58 +00:00
|
|
|
deleted.eq(true),
|
|
|
|
updated.eq(naive_now()),
|
|
|
|
))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2022-01-08 12:37:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 21:14:07 +00:00
|
|
|
pub fn is_banned(banned_: bool, expires: Option<chrono::NaiveDateTime>) -> bool {
|
2022-01-08 12:37:07 +00:00
|
|
|
if let Some(expires) = expires {
|
|
|
|
banned_ && expires.gt(&naive_now())
|
|
|
|
} else {
|
|
|
|
banned_
|
|
|
|
}
|
2021-10-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 16:39:22 +00:00
|
|
|
impl ApubActor for Person {
|
2022-09-26 14:09:32 +00:00
|
|
|
fn read_from_apub_id(conn: &mut PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error> {
|
2022-01-27 16:39:22 +00:00
|
|
|
use crate::schema::person::dsl::*;
|
|
|
|
Ok(
|
|
|
|
person
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(actor_id.eq(object_id))
|
|
|
|
.first::<Person>(conn)
|
|
|
|
.ok()
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-05 21:40:44 +00:00
|
|
|
fn read_from_name(
|
2022-09-26 14:09:32 +00:00
|
|
|
conn: &mut PgConnection,
|
2022-07-05 21:40:44 +00:00
|
|
|
from_name: &str,
|
|
|
|
include_deleted: bool,
|
|
|
|
) -> Result<Person, Error> {
|
|
|
|
let mut q = person
|
|
|
|
.into_boxed()
|
2022-01-27 16:39:22 +00:00
|
|
|
.filter(local.eq(true))
|
2022-07-05 21:40:44 +00:00
|
|
|
.filter(lower(name).eq(lower(from_name)));
|
|
|
|
if !include_deleted {
|
|
|
|
q = q.filter(deleted.eq(false))
|
|
|
|
}
|
|
|
|
q.first::<Self>(conn)
|
2022-01-27 16:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_from_name_and_domain(
|
2022-09-26 14:09:32 +00:00
|
|
|
conn: &mut PgConnection,
|
2022-01-27 16:39:22 +00:00
|
|
|
person_name: &str,
|
|
|
|
protocol_domain: &str,
|
|
|
|
) -> Result<Person, Error> {
|
|
|
|
use crate::schema::person::dsl::*;
|
|
|
|
person
|
|
|
|
.filter(lower(name).eq(lower(person_name)))
|
|
|
|
.filter(actor_id.like(format!("{}%", protocol_domain)))
|
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-10-27 09:24:07 +00:00
|
|
|
use crate::{
|
|
|
|
source::{instance::Instance, person::*},
|
|
|
|
traits::Crud,
|
|
|
|
utils::establish_unpooled_connection,
|
|
|
|
};
|
2022-10-06 18:27:58 +00:00
|
|
|
use serial_test::serial;
|
2021-02-26 13:49:58 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-10-06 18:27:58 +00:00
|
|
|
#[serial]
|
2021-02-26 13:49:58 +00:00
|
|
|
fn test_crud() {
|
2022-09-26 14:09:32 +00:00
|
|
|
let conn = &mut establish_unpooled_connection();
|
2021-02-26 13:49:58 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
|
|
|
|
|
|
|
|
let new_person = PersonInsertForm::builder()
|
|
|
|
.name("holly".into())
|
|
|
|
.public_key("nada".to_owned())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2021-02-26 13:49:58 +00:00
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
let inserted_person = Person::create(conn, &new_person).unwrap();
|
2021-02-26 13:49:58 +00:00
|
|
|
|
|
|
|
let expected_person = Person {
|
|
|
|
id: inserted_person.id,
|
2021-03-11 16:54:03 +00:00
|
|
|
name: "holly".into(),
|
2021-04-01 17:57:45 +00:00
|
|
|
display_name: None,
|
2021-02-26 13:49:58 +00:00
|
|
|
avatar: None,
|
|
|
|
banner: None,
|
|
|
|
banned: false,
|
|
|
|
deleted: false,
|
|
|
|
published: inserted_person.published,
|
|
|
|
updated: None,
|
|
|
|
actor_id: inserted_person.actor_id.to_owned(),
|
|
|
|
bio: None,
|
|
|
|
local: true,
|
2021-04-21 21:41:14 +00:00
|
|
|
bot_account: false,
|
2021-03-22 14:28:00 +00:00
|
|
|
admin: false,
|
2021-02-26 13:49:58 +00:00
|
|
|
private_key: None,
|
2021-11-22 15:10:18 +00:00
|
|
|
public_key: "nada".to_owned(),
|
2021-02-26 13:49:58 +00:00
|
|
|
last_refreshed_at: inserted_person.published,
|
|
|
|
inbox_url: inserted_person.inbox_url.to_owned(),
|
|
|
|
shared_inbox_url: None,
|
2021-03-20 19:21:51 +00:00
|
|
|
matrix_user_id: None,
|
2022-01-08 12:37:07 +00:00
|
|
|
ban_expires: None,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance_id: inserted_instance.id,
|
2021-02-26 13:49:58 +00:00
|
|
|
};
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
let read_person = Person::read(conn, inserted_person.id).unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let update_person_form = PersonUpdateForm::builder()
|
|
|
|
.actor_id(Some(inserted_person.actor_id.to_owned()))
|
|
|
|
.build();
|
|
|
|
let updated_person = Person::update(conn, inserted_person.id, &update_person_form).unwrap();
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
let num_deleted = Person::delete(conn, inserted_person.id).unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
Instance::delete(conn, inserted_instance.id).unwrap();
|
2021-02-26 13:49:58 +00:00
|
|
|
|
|
|
|
assert_eq!(expected_person, read_person);
|
|
|
|
assert_eq!(expected_person, inserted_person);
|
|
|
|
assert_eq!(expected_person, updated_person);
|
|
|
|
assert_eq!(1, num_deleted);
|
|
|
|
}
|
|
|
|
}
|