2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
2022-11-23 23:40:47 +00:00
|
|
|
newtypes::{CommunityId, DbUrl, PersonId},
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::person::dsl::{
|
|
|
|
actor_id,
|
|
|
|
avatar,
|
|
|
|
banner,
|
|
|
|
bio,
|
|
|
|
deleted,
|
|
|
|
display_name,
|
|
|
|
local,
|
|
|
|
matrix_user_id,
|
|
|
|
name,
|
|
|
|
person,
|
|
|
|
updated,
|
|
|
|
},
|
2022-11-23 23:40:47 +00:00
|
|
|
source::person::{
|
|
|
|
Person,
|
|
|
|
PersonFollower,
|
|
|
|
PersonFollowerForm,
|
|
|
|
PersonInsertForm,
|
|
|
|
PersonUpdateForm,
|
|
|
|
},
|
|
|
|
traits::{ApubActor, Crud, Followable},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{functions::lower, get_conn, naive_now, DbPool},
|
2021-02-26 13:49:58 +00:00
|
|
|
};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2021-02-26 13:49:58 +00:00
|
|
|
|
|
|
|
mod safe_type {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::{
|
|
|
|
schema::person::columns::{
|
|
|
|
actor_id,
|
|
|
|
admin,
|
|
|
|
avatar,
|
|
|
|
ban_expires,
|
|
|
|
banned,
|
|
|
|
banner,
|
|
|
|
bio,
|
|
|
|
bot_account,
|
|
|
|
deleted,
|
|
|
|
display_name,
|
|
|
|
id,
|
|
|
|
inbox_url,
|
|
|
|
instance_id,
|
|
|
|
local,
|
|
|
|
matrix_user_id,
|
|
|
|
name,
|
|
|
|
published,
|
|
|
|
shared_inbox_url,
|
|
|
|
updated,
|
|
|
|
},
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
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-11-09 10:05:00 +00:00
|
|
|
async fn read(pool: &DbPool, person_id: PersonId) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-02-26 13:49:58 +00:00
|
|
|
person
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.find(person_id)
|
|
|
|
.first::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-02-26 13:49:58 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn delete(pool: &DbPool, person_id: PersonId) -> Result<usize, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
diesel::delete(person.find(person_id)).execute(conn).await
|
2021-02-26 13:49:58 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn create(pool: &DbPool, form: &PersonInsertForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
insert_into(person)
|
|
|
|
.values(form)
|
|
|
|
.on_conflict(actor_id)
|
|
|
|
.do_update()
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-02-26 13:49:58 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
|
|
|
pool: &DbPool,
|
2022-09-26 14:09:32 +00:00
|
|
|
person_id: PersonId,
|
2022-10-27 09:24:07 +00:00
|
|
|
form: &PersonUpdateForm,
|
2022-09-26 14:09:32 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-02-26 13:49:58 +00:00
|
|
|
diesel::update(person.find(person_id))
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-02-26 13:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl Person {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn delete_account(pool: &DbPool, person_id: PersonId) -> Result<Person, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::local_user;
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
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()),
|
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.execute(conn)
|
|
|
|
.await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
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-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-02-26 13:49:58 +00:00
|
|
|
}
|
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-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2022-01-27 16:39:22 +00:00
|
|
|
impl ApubActor for Person {
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-01-27 16:39:22 +00:00
|
|
|
Ok(
|
|
|
|
person
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(actor_id.eq(object_id))
|
|
|
|
.first::<Person>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-01-27 16:39:22 +00:00
|
|
|
.ok()
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read_from_name(
|
|
|
|
pool: &DbPool,
|
2022-07-05 21:40:44 +00:00
|
|
|
from_name: &str,
|
|
|
|
include_deleted: bool,
|
|
|
|
) -> Result<Person, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-07-05 21:40:44 +00:00
|
|
|
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))
|
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
q.first::<Self>(conn).await
|
2022-01-27 16:39:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read_from_name_and_domain(
|
|
|
|
pool: &DbPool,
|
2022-01-27 16:39:22 +00:00
|
|
|
person_name: &str,
|
|
|
|
protocol_domain: &str,
|
|
|
|
) -> Result<Person, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-01-27 16:39:22 +00:00
|
|
|
person
|
|
|
|
.filter(lower(name).eq(lower(person_name)))
|
|
|
|
.filter(actor_id.like(format!("{}%", protocol_domain)))
|
|
|
|
.first::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-01-27 16:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 23:40:47 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Followable for PersonFollower {
|
|
|
|
type Form = PersonFollowerForm;
|
|
|
|
async fn follow(pool: &DbPool, form: &PersonFollowerForm) -> Result<Self, Error> {
|
|
|
|
use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id};
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
insert_into(person_follower)
|
|
|
|
.values(form)
|
|
|
|
.on_conflict((follower_id, person_id))
|
|
|
|
.do_update()
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
async fn follow_accepted(_: &DbPool, _: CommunityId, _: PersonId) -> Result<Self, Error> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
async fn unfollow(pool: &DbPool, form: &PersonFollowerForm) -> Result<usize, Error> {
|
|
|
|
use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id};
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
diesel::delete(
|
|
|
|
person_follower
|
|
|
|
.filter(follower_id.eq(&form.follower_id))
|
|
|
|
.filter(person_id.eq(&form.person_id)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PersonFollower {
|
|
|
|
pub async fn list_followers(pool: &DbPool, person_id_: PersonId) -> Result<Vec<Person>, Error> {
|
|
|
|
use crate::schema::{person, person_follower, person_follower::person_id};
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
person_follower::table
|
|
|
|
.inner_join(person::table)
|
|
|
|
.filter(person_id.eq(person_id_))
|
|
|
|
.select(person::all_columns)
|
|
|
|
.load(conn)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-10-27 09:24:07 +00:00
|
|
|
use crate::{
|
2022-11-19 04:33:54 +00:00
|
|
|
source::{
|
|
|
|
instance::Instance,
|
2022-11-23 23:40:47 +00:00
|
|
|
person::{Person, PersonFollower, PersonFollowerForm, PersonInsertForm, PersonUpdateForm},
|
2022-11-19 04:33:54 +00:00
|
|
|
},
|
2022-11-23 23:40:47 +00:00
|
|
|
traits::{Crud, Followable},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
2022-10-27 09:24:07 +00:00
|
|
|
};
|
2022-10-06 18:27:58 +00:00
|
|
|
use serial_test::serial;
|
2021-02-26 13:49:58 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[tokio::test]
|
2022-10-06 18:27:58 +00:00
|
|
|
#[serial]
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn test_crud() {
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
2021-02-26 13:49:58 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
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-11-09 10:05:00 +00:00
|
|
|
let inserted_person = Person::create(pool, &new_person).await.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,
|
2022-11-19 04:33:54 +00:00
|
|
|
actor_id: inserted_person.actor_id.clone(),
|
2021-02-26 13:49:58 +00:00
|
|
|
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,
|
2022-11-19 04:33:54 +00:00
|
|
|
inbox_url: inserted_person.inbox_url.clone(),
|
2021-02-26 13:49:58 +00:00
|
|
|
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-11-09 10:05:00 +00:00
|
|
|
let read_person = Person::read(pool, inserted_person.id).await.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let update_person_form = PersonUpdateForm::builder()
|
2022-11-19 04:33:54 +00:00
|
|
|
.actor_id(Some(inserted_person.actor_id.clone()))
|
2022-10-27 09:24:07 +00:00
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let updated_person = Person::update(pool, inserted_person.id, &update_person_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
|
|
|
|
Instance::delete(pool, inserted_instance.id).await.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);
|
|
|
|
}
|
2022-11-23 23:40:47 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn follow() {
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
|
|
|
let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
|
|
|
|
|
|
|
|
let person_form_1 = PersonInsertForm::builder()
|
|
|
|
.name("erich".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
|
|
|
let person_1 = Person::create(pool, &person_form_1).await.unwrap();
|
|
|
|
let person_form_2 = PersonInsertForm::builder()
|
|
|
|
.name("michele".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
|
|
|
let person_2 = Person::create(pool, &person_form_2).await.unwrap();
|
|
|
|
|
|
|
|
let follow_form = PersonFollowerForm {
|
|
|
|
person_id: person_1.id,
|
|
|
|
follower_id: person_2.id,
|
|
|
|
pending: false,
|
|
|
|
};
|
|
|
|
let person_follower = PersonFollower::follow(pool, &follow_form).await.unwrap();
|
|
|
|
assert_eq!(person_1.id, person_follower.person_id);
|
|
|
|
assert_eq!(person_2.id, person_follower.follower_id);
|
|
|
|
assert!(!person_follower.pending);
|
|
|
|
|
|
|
|
let followers = PersonFollower::list_followers(pool, person_1.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(vec![person_2], followers);
|
|
|
|
|
|
|
|
let unfollow = PersonFollower::unfollow(pool, &follow_form).await.unwrap();
|
|
|
|
assert_eq!(1, unfollow);
|
|
|
|
}
|
2021-02-26 13:49:58 +00:00
|
|
|
}
|