2021-03-10 22:33:55 +00:00
|
|
|
use diesel::{result::Error, *};
|
2021-03-11 04:43:11 +00:00
|
|
|
use lemmy_db_queries::{aggregates::person_aggregates::PersonAggregates, ToSafe, ToSafeSettings};
|
2021-03-10 22:33:55 +00:00
|
|
|
use lemmy_db_schema::{
|
2021-03-11 04:43:11 +00:00
|
|
|
schema::{local_user, person, person_aggregates},
|
|
|
|
source::{
|
|
|
|
local_user::{LocalUser, LocalUserSettings},
|
|
|
|
person::{Person, PersonSafe},
|
|
|
|
},
|
2021-03-18 20:25:21 +00:00
|
|
|
LocalUserId,
|
|
|
|
PersonId,
|
2021-03-10 22:33:55 +00:00
|
|
|
};
|
2021-10-19 16:37:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2021-10-19 16:37:01 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2021-03-10 22:33:55 +00:00
|
|
|
pub struct LocalUserView {
|
2021-03-11 22:47:44 +00:00
|
|
|
pub local_user: LocalUser,
|
2021-03-10 22:33:55 +00:00
|
|
|
pub person: Person,
|
|
|
|
pub counts: PersonAggregates,
|
|
|
|
}
|
|
|
|
|
2021-03-11 22:47:44 +00:00
|
|
|
type LocalUserViewTuple = (LocalUser, Person, PersonAggregates);
|
2021-03-10 22:33:55 +00:00
|
|
|
|
|
|
|
impl LocalUserView {
|
2021-03-18 20:25:21 +00:00
|
|
|
pub fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
|
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,
|
|
|
|
))
|
2021-03-10 22:33:55 +00:00
|
|
|
.first::<LocalUserViewTuple>(conn)?;
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:25:21 +00:00
|
|
|
pub fn read_person(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
|
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,
|
|
|
|
))
|
|
|
|
.first::<LocalUserViewTuple>(conn)?;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check where this is used
|
|
|
|
pub fn read_from_name(conn: &PgConnection, name: &str) -> Result<Self, Error> {
|
2021-03-11 22:47:44 +00:00
|
|
|
let (local_user, person, counts) = local_user::table
|
2021-03-10 22:33:55 +00:00
|
|
|
.filter(person::name.eq(name))
|
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,
|
|
|
|
))
|
|
|
|
.first::<LocalUserViewTuple>(conn)?;
|
|
|
|
Ok(Self {
|
2021-03-25 19:30:15 +00:00
|
|
|
local_user,
|
2021-03-11 04:43:11 +00:00
|
|
|
person,
|
|
|
|
counts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_email_or_name(conn: &PgConnection, name_or_email: &str) -> Result<Self, Error> {
|
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(
|
|
|
|
person::name
|
|
|
|
.ilike(name_or_email)
|
|
|
|
.or(local_user::email.ilike(name_or_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,
|
|
|
|
))
|
2021-03-10 22:33:55 +00:00
|
|
|
.first::<LocalUserViewTuple>(conn)?;
|
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
|
|
|
}
|
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<Self, Error> {
|
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,
|
|
|
|
))
|
2021-03-10 22:33:55 +00:00
|
|
|
.first::<LocalUserViewTuple>(conn)?;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:37:01 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2021-03-10 22:33:55 +00:00
|
|
|
pub struct LocalUserSettingsView {
|
2021-03-11 22:47:44 +00:00
|
|
|
pub local_user: LocalUserSettings,
|
2021-03-10 22:33:55 +00:00
|
|
|
pub person: PersonSafe,
|
|
|
|
pub counts: PersonAggregates,
|
|
|
|
}
|
|
|
|
|
2021-03-11 22:47:44 +00:00
|
|
|
type LocalUserSettingsViewTuple = (LocalUserSettings, PersonSafe, PersonAggregates);
|
2021-03-10 22:33:55 +00:00
|
|
|
|
|
|
|
impl LocalUserSettingsView {
|
2021-03-18 20:25:21 +00:00
|
|
|
pub fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
|
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
|
|
|
LocalUser::safe_settings_columns_tuple(),
|
2021-03-11 04:43:11 +00:00
|
|
|
Person::safe_columns_tuple(),
|
|
|
|
person_aggregates::all_columns,
|
|
|
|
))
|
2021-03-10 22:33:55 +00:00
|
|
|
.first::<LocalUserSettingsViewTuple>(conn)?;
|
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
|
|
|
}
|
|
|
|
}
|