2021-03-11 04:43:11 +00:00
|
|
|
use crate::{ApubObject, Crud};
|
2021-02-26 13:49:58 +00:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
naive_now,
|
|
|
|
schema::person::dsl::*,
|
2021-03-11 04:43:11 +00:00
|
|
|
source::person::{Person, PersonForm},
|
2021-03-10 22:33:55 +00:00
|
|
|
DbUrl,
|
2021-03-18 20:25:21 +00:00
|
|
|
PersonId,
|
2021-02-26 13:49:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mod safe_type {
|
|
|
|
use crate::ToSafe;
|
|
|
|
use lemmy_db_schema::{schema::person::columns::*, source::person::Person};
|
|
|
|
|
|
|
|
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,
|
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,
|
2021-02-26 13:49:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod safe_type_alias_1 {
|
|
|
|
use crate::ToSafe;
|
|
|
|
use lemmy_db_schema::{schema::person_alias_1::columns::*, source::person::PersonAlias1};
|
|
|
|
|
|
|
|
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,
|
2021-02-26 13:49:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
impl ToSafe for PersonAlias1 {
|
|
|
|
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,
|
2021-02-26 13:49:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod safe_type_alias_2 {
|
|
|
|
use crate::ToSafe;
|
|
|
|
use lemmy_db_schema::{schema::person_alias_2::columns::*, source::person::PersonAlias2};
|
|
|
|
|
|
|
|
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,
|
2021-02-26 13:49:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
impl ToSafe for PersonAlias2 {
|
|
|
|
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,
|
2021-02-26 13:49:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:25:21 +00:00
|
|
|
impl Crud<PersonForm, PersonId> for Person {
|
|
|
|
fn read(conn: &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)
|
|
|
|
}
|
2021-03-18 20:25:21 +00:00
|
|
|
fn delete(conn: &PgConnection, person_id: PersonId) -> Result<usize, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
diesel::delete(person.find(person_id)).execute(conn)
|
|
|
|
}
|
|
|
|
fn create(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
|
|
|
|
insert_into(person).values(form).get_result::<Self>(conn)
|
|
|
|
}
|
2021-03-18 20:25:21 +00:00
|
|
|
fn update(conn: &PgConnection, person_id: PersonId, form: &PersonForm) -> Result<Self, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
diesel::update(person.find(person_id))
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ApubObject<PersonForm> for Person {
|
2021-03-10 22:33:55 +00:00
|
|
|
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
use lemmy_db_schema::schema::person::dsl::*;
|
|
|
|
person
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(actor_id.eq(object_id))
|
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
|
|
|
|
insert_into(person)
|
|
|
|
.values(person_form)
|
|
|
|
.on_conflict(actor_id)
|
|
|
|
.do_update()
|
|
|
|
.set(person_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Person_ {
|
2021-03-18 20:25:21 +00:00
|
|
|
fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Person, Error>;
|
2021-03-22 14:28:00 +00:00
|
|
|
fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Person, Error>;
|
2021-02-26 13:49:58 +00:00
|
|
|
fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
|
2021-03-18 20:25:21 +00:00
|
|
|
fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
|
|
|
|
fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
|
2021-02-26 13:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Person_ for Person {
|
2021-03-18 20:25:21 +00:00
|
|
|
fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Self, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
diesel::update(person.find(person_id))
|
|
|
|
.set(banned.eq(ban))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2021-03-22 14:28:00 +00:00
|
|
|
fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Self, Error> {
|
|
|
|
diesel::update(person.find(person_id))
|
|
|
|
.set(admin.eq(added))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
fn find_by_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
person
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(local.eq(true))
|
2021-03-10 22:33:55 +00:00
|
|
|
.filter(name.ilike(from_name))
|
2021-02-26 13:49:58 +00:00
|
|
|
.first::<Person>(conn)
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:25:21 +00:00
|
|
|
fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
|
2021-02-26 13:49:58 +00:00
|
|
|
diesel::update(person.find(person_id))
|
|
|
|
.set((last_refreshed_at.eq(naive_now()),))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:25:21 +00:00
|
|
|
fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
|
2021-03-10 22:33:55 +00:00
|
|
|
use lemmy_db_schema::schema::local_user;
|
|
|
|
|
|
|
|
// Set the local user info to none
|
|
|
|
diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
|
2021-03-20 19:21:51 +00:00
|
|
|
.set((local_user::email.eq::<Option<String>>(None),))
|
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),
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-03-10 22:33:55 +00:00
|
|
|
use crate::{establish_unpooled_connection, source::person::*};
|
2021-02-26 13:49:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_crud() {
|
|
|
|
let conn = establish_unpooled_connection();
|
|
|
|
|
|
|
|
let new_person = PersonForm {
|
2021-03-11 16:54:03 +00:00
|
|
|
name: "holly".into(),
|
2021-03-20 20:59:07 +00:00
|
|
|
..PersonForm::default()
|
2021-02-26 13:49:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_person = Person::create(&conn, &new_person).unwrap();
|
|
|
|
|
|
|
|
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,
|
|
|
|
public_key: None,
|
|
|
|
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,
|
2021-02-26 13:49:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let read_person = Person::read(&conn, inserted_person.id).unwrap();
|
|
|
|
let updated_person = Person::update(&conn, inserted_person.id, &new_person).unwrap();
|
|
|
|
let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(expected_person, read_person);
|
|
|
|
assert_eq!(expected_person, inserted_person);
|
|
|
|
assert_eq!(expected_person, updated_person);
|
|
|
|
assert_eq!(1, num_deleted);
|
|
|
|
}
|
|
|
|
}
|