2023-03-01 17:19:46 +00:00
|
|
|
use crate::structs::LocalUserView;
|
2023-08-29 14:47:57 +00:00
|
|
|
use actix_web::{dev::Payload, FromRequest, HttpMessage, HttpRequest};
|
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::{
|
2024-09-18 12:52:33 +00:00
|
|
|
newtypes::{LocalUserId, OAuthProviderId, PersonId},
|
|
|
|
schema::{local_user, local_user_vote_display_mode, oauth_account, person, person_aggregates},
|
2023-12-15 10:34:17 +00:00
|
|
|
utils::{
|
|
|
|
functions::{coalesce, lower},
|
|
|
|
DbConn,
|
|
|
|
DbPool,
|
|
|
|
ListFn,
|
|
|
|
Queries,
|
|
|
|
ReadFn,
|
|
|
|
},
|
2021-03-10 22:33:55 +00:00
|
|
|
};
|
2023-08-29 14:47:57 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorType};
|
|
|
|
use std::future::{ready, Ready};
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2023-07-28 08:36:50 +00:00
|
|
|
enum ReadBy<'a> {
|
|
|
|
Id(LocalUserId),
|
|
|
|
Person(PersonId),
|
|
|
|
Name(&'a str),
|
|
|
|
NameOrEmail(&'a str),
|
|
|
|
Email(&'a str),
|
2024-09-18 12:52:33 +00:00
|
|
|
OAuthID(OAuthProviderId, &'a str),
|
2023-07-28 08:36:50 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
|
2023-07-28 08:36:50 +00:00
|
|
|
enum ListMode {
|
|
|
|
AdminsWithEmails,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn queries<'a>(
|
|
|
|
) -> Queries<impl ReadFn<'a, LocalUserView, ReadBy<'a>>, impl ListFn<'a, LocalUserView, ListMode>> {
|
|
|
|
let selection = (
|
|
|
|
local_user::all_columns,
|
2024-03-13 16:10:58 +00:00
|
|
|
local_user_vote_display_mode::all_columns,
|
2023-07-28 08:36:50 +00:00
|
|
|
person::all_columns,
|
|
|
|
person_aggregates::all_columns,
|
|
|
|
);
|
|
|
|
|
|
|
|
let read = move |mut conn: DbConn<'a>, search: ReadBy<'a>| async move {
|
|
|
|
let mut query = local_user::table.into_boxed();
|
|
|
|
query = match search {
|
|
|
|
ReadBy::Id(local_user_id) => query.filter(local_user::id.eq(local_user_id)),
|
2023-12-15 10:34:17 +00:00
|
|
|
ReadBy::Email(from_email) => {
|
|
|
|
query.filter(lower(coalesce(local_user::email, "")).eq(from_email.to_lowercase()))
|
|
|
|
}
|
2023-07-28 08:36:50 +00:00
|
|
|
_ => query,
|
|
|
|
};
|
|
|
|
let mut query = query.inner_join(person::table);
|
|
|
|
query = match search {
|
|
|
|
ReadBy::Person(person_id) => query.filter(person::id.eq(person_id)),
|
|
|
|
ReadBy::Name(name) => query.filter(lower(person::name).eq(name.to_lowercase())),
|
|
|
|
ReadBy::NameOrEmail(name_or_email) => query.filter(
|
|
|
|
lower(person::name)
|
2023-12-15 10:34:17 +00:00
|
|
|
.eq(lower(name_or_email.to_lowercase()))
|
|
|
|
.or(lower(coalesce(local_user::email, "")).eq(name_or_email.to_lowercase())),
|
2023-07-28 08:36:50 +00:00
|
|
|
),
|
|
|
|
_ => query,
|
|
|
|
};
|
2024-09-18 12:52:33 +00:00
|
|
|
let query = query
|
2024-03-13 16:10:58 +00:00
|
|
|
.inner_join(local_user_vote_display_mode::table)
|
2024-09-18 12:52:33 +00:00
|
|
|
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)));
|
|
|
|
|
|
|
|
if let ReadBy::OAuthID(oauth_provider_id, oauth_user_id) = search {
|
|
|
|
query
|
|
|
|
.inner_join(oauth_account::table)
|
|
|
|
.filter(oauth_account::oauth_provider_id.eq(oauth_provider_id))
|
|
|
|
.filter(oauth_account::oauth_user_id.eq(oauth_user_id))
|
|
|
|
.select(selection)
|
|
|
|
.first(&mut conn)
|
|
|
|
.await
|
|
|
|
} else {
|
|
|
|
query.select(selection).first(&mut conn).await
|
|
|
|
}
|
2023-07-28 08:36:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let list = move |mut conn: DbConn<'a>, mode: ListMode| async move {
|
|
|
|
match mode {
|
|
|
|
ListMode::AdminsWithEmails => {
|
|
|
|
local_user::table
|
2024-03-13 16:10:58 +00:00
|
|
|
.inner_join(local_user_vote_display_mode::table)
|
2023-07-28 08:36:50 +00:00
|
|
|
.inner_join(person::table)
|
|
|
|
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
2024-03-13 16:10:58 +00:00
|
|
|
.filter(local_user::email.is_not_null())
|
|
|
|
.filter(local_user::admin.eq(true))
|
2023-07-28 08:36:50 +00:00
|
|
|
.select(selection)
|
2023-08-31 13:26:10 +00:00
|
|
|
.load::<LocalUserView>(&mut conn)
|
2023-07-28 08:36:50 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Queries::new(read, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalUserView {
|
2024-09-23 15:26:50 +00:00
|
|
|
pub async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result<Self, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().read(pool, ReadBy::Id(local_user_id)).await
|
2021-03-11 22:47:44 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 15:26:50 +00:00
|
|
|
pub async fn read_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().read(pool, ReadBy::Person(person_id)).await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 15:26:50 +00:00
|
|
|
pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result<Self, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().read(pool, ReadBy::Name(name)).await
|
2021-03-11 04:43:11 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn find_by_email_or_name(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
name_or_email: &str,
|
2024-09-23 15:26:50 +00:00
|
|
|
) -> Result<Self, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries()
|
|
|
|
.read(pool, ReadBy::NameOrEmail(name_or_email))
|
|
|
|
.await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 15:26:50 +00:00
|
|
|
pub async fn find_by_email(pool: &mut DbPool<'_>, from_email: &str) -> Result<Self, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().read(pool, ReadBy::Email(from_email)).await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
2022-09-27 16:48:44 +00:00
|
|
|
|
2024-09-18 12:52:33 +00:00
|
|
|
pub async fn find_by_oauth_id(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
oauth_provider_id: OAuthProviderId,
|
|
|
|
oauth_user_id: &str,
|
2024-09-23 15:26:50 +00:00
|
|
|
) -> Result<Self, Error> {
|
2024-09-18 12:52:33 +00:00
|
|
|
queries()
|
|
|
|
.read(pool, ReadBy::OAuthID(oauth_provider_id, oauth_user_id))
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn list_admins_with_emails(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().list(pool, ListMode::AdminsWithEmails).await
|
2022-09-27 16:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 14:47:57 +00:00
|
|
|
impl FromRequest for LocalUserView {
|
|
|
|
type Error = LemmyError;
|
|
|
|
type Future = Ready<Result<Self, Self::Error>>;
|
|
|
|
|
|
|
|
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
|
|
|
ready(match req.extensions().get::<LocalUserView>() {
|
|
|
|
Some(c) => Ok(c.clone()),
|
|
|
|
None => Err(LemmyErrorType::IncorrectLogin.into()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|