2023-03-01 17:19:46 +00:00
|
|
|
use crate::structs::LocalUserView;
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{result::Error, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2021-03-10 22:33:55 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-05-03 17:44:13 +00:00
|
|
|
aggregates::structs::PersonAggregates,
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::{LocalUserId, PersonId},
|
2021-03-11 04:43:11 +00:00
|
|
|
schema::{local_user, person, person_aggregates},
|
2023-03-01 17:19:46 +00:00
|
|
|
source::{local_user::LocalUser, person::Person},
|
|
|
|
traits::JoinView,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{functions::lower, get_conn, DbPool},
|
2021-03-10 22:33:55 +00:00
|
|
|
};
|
|
|
|
|
2021-03-11 22:47:44 +00:00
|
|
|
type LocalUserViewTuple = (LocalUser, Person, PersonAggregates);
|
2021-03-10 22:33:55 +00:00
|
|
|
|
|
|
|
impl LocalUserView {
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
|
2021-03-11 22:47:44 +00:00
|
|
|
let (local_user, person, counts) = local_user::table
|
|
|
|
.find(local_user_id)
|
|
|
|
.inner_join(person::table)
|
|
|
|
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
2021-03-11 04:43:11 +00:00
|
|
|
.select((
|
2021-03-11 22:47:44 +00:00
|
|
|
local_user::all_columns,
|
2021-03-11 04:43:11 +00:00
|
|
|
person::all_columns,
|
|
|
|
person_aggregates::all_columns,
|
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.first::<LocalUserViewTuple>(conn)
|
|
|
|
.await?;
|
2021-03-11 04:43:11 +00:00
|
|
|
Ok(Self {
|
2021-03-11 22:47:44 +00:00
|
|
|
local_user,
|
2021-03-11 04:43:11 +00:00
|
|
|
person,
|
|
|
|
counts,
|
2021-03-11 22:47:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-11 22:47:44 +00:00
|
|
|
let (local_user, person, counts) = local_user::table
|
|
|
|
.filter(person::id.eq(person_id))
|
|
|
|
.inner_join(person::table)
|
|
|
|
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
|
|
|
.select((
|
|
|
|
local_user::all_columns,
|
|
|
|
person::all_columns,
|
|
|
|
person_aggregates::all_columns,
|
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.first::<LocalUserViewTuple>(conn)
|
|
|
|
.await?;
|
2021-03-11 22:47:44 +00:00
|
|
|
Ok(Self {
|
2021-03-11 04:43:11 +00:00
|
|
|
local_user,
|
2021-03-11 22:47:44 +00:00
|
|
|
person,
|
|
|
|
counts,
|
2021-03-11 04:43:11 +00:00
|
|
|
})
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-11 22:47:44 +00:00
|
|
|
let (local_user, person, counts) = local_user::table
|
2023-04-25 23:28:06 +00:00
|
|
|
.filter(lower(person::name).eq(name.to_lowercase()))
|
2021-03-11 22:47:44 +00:00
|
|
|
.inner_join(person::table)
|
|
|
|
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
2021-03-11 04:43:11 +00:00
|
|
|
.select((
|
2021-03-11 22:47:44 +00:00
|
|
|
local_user::all_columns,
|
2021-03-11 04:43:11 +00:00
|
|
|
person::all_columns,
|
|
|
|
person_aggregates::all_columns,
|
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.first::<LocalUserViewTuple>(conn)
|
|
|
|
.await?;
|
2021-03-11 04:43:11 +00:00
|
|
|
Ok(Self {
|
2021-03-25 19:30:15 +00:00
|
|
|
local_user,
|
2021-03-11 04:43:11 +00:00
|
|
|
person,
|
|
|
|
counts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn find_by_email_or_name(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
name_or_email: &str,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-11 22:47:44 +00:00
|
|
|
let (local_user, person, counts) = local_user::table
|
|
|
|
.inner_join(person::table)
|
|
|
|
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
2021-03-11 04:43:11 +00:00
|
|
|
.filter(
|
2022-01-26 18:05:07 +00:00
|
|
|
lower(person::name)
|
|
|
|
.eq(lower(name_or_email))
|
2021-11-19 01:58:11 +00:00
|
|
|
.or(local_user::email.eq(name_or_email)),
|
2021-03-11 04:43:11 +00:00
|
|
|
)
|
|
|
|
.select((
|
2021-03-11 22:47:44 +00:00
|
|
|
local_user::all_columns,
|
2021-03-11 04:43:11 +00:00
|
|
|
person::all_columns,
|
|
|
|
person_aggregates::all_columns,
|
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.first::<LocalUserViewTuple>(conn)
|
|
|
|
.await?;
|
2021-03-11 04:43:11 +00:00
|
|
|
Ok(Self {
|
2021-03-25 19:30:15 +00:00
|
|
|
local_user,
|
2021-03-11 04:43:11 +00:00
|
|
|
person,
|
|
|
|
counts,
|
|
|
|
})
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn find_by_email(pool: &mut DbPool<'_>, from_email: &str) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-11 22:47:44 +00:00
|
|
|
let (local_user, person, counts) = local_user::table
|
|
|
|
.inner_join(person::table)
|
|
|
|
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
2021-03-11 04:43:11 +00:00
|
|
|
.filter(local_user::email.eq(from_email))
|
|
|
|
.select((
|
2021-03-11 22:47:44 +00:00
|
|
|
local_user::all_columns,
|
2021-03-11 04:43:11 +00:00
|
|
|
person::all_columns,
|
|
|
|
person_aggregates::all_columns,
|
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.first::<LocalUserViewTuple>(conn)
|
|
|
|
.await?;
|
2021-03-11 04:43:11 +00:00
|
|
|
Ok(Self {
|
2021-03-25 19:30:15 +00:00
|
|
|
local_user,
|
2021-03-11 04:43:11 +00:00
|
|
|
person,
|
|
|
|
counts,
|
|
|
|
})
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
2022-09-27 16:48:44 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn list_admins_with_emails(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-09-27 16:48:44 +00:00
|
|
|
let res = local_user::table
|
|
|
|
.filter(person::admin.eq(true))
|
|
|
|
.filter(local_user::email.is_not_null())
|
|
|
|
.inner_join(person::table)
|
|
|
|
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
|
|
|
.select((
|
2023-03-01 17:19:46 +00:00
|
|
|
local_user::all_columns,
|
|
|
|
person::all_columns,
|
2022-09-27 16:48:44 +00:00
|
|
|
person_aggregates::all_columns,
|
|
|
|
))
|
2023-03-01 17:19:46 +00:00
|
|
|
.load::<LocalUserViewTuple>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?;
|
2022-09-27 16:48:44 +00:00
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
Ok(res.into_iter().map(LocalUserView::from_tuple).collect())
|
2022-09-27 16:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
impl JoinView for LocalUserView {
|
|
|
|
type JoinTuple = LocalUserViewTuple;
|
|
|
|
fn from_tuple(a: Self::JoinTuple) -> Self {
|
|
|
|
Self {
|
|
|
|
local_user: a.0,
|
|
|
|
person: a.1,
|
|
|
|
counts: a.2,
|
|
|
|
}
|
2022-09-27 16:48:44 +00:00
|
|
|
}
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|