lemmy/lemmy_db/src/views/user_view.rs

210 lines
5.3 KiB
Rust
Raw Normal View History

2020-12-03 03:39:31 +00:00
use crate::{
2020-12-03 18:39:56 +00:00
aggregates::user_aggregates::UserAggregates,
2020-12-04 00:47:58 +00:00
fuzzy_search,
limit_and_offset,
2020-12-03 18:39:56 +00:00
schema::{user_, user_aggregates},
2020-12-03 03:39:31 +00:00
user::{UserSafe, User_},
2020-12-04 00:47:58 +00:00
MaybeOptional,
SortType,
2020-12-03 03:39:31 +00:00
};
2020-12-04 00:47:58 +00:00
use diesel::{dsl::*, result::Error, *};
2020-12-03 03:39:31 +00:00
use serde::Serialize;
#[derive(Debug, Serialize, Clone)]
pub struct UserViewSafe {
pub user: UserSafe,
2020-12-03 18:39:56 +00:00
pub counts: UserAggregates,
2020-12-03 03:39:31 +00:00
}
2020-12-03 18:39:56 +00:00
#[derive(Debug, Serialize, Clone)]
2020-12-03 03:39:31 +00:00
pub struct UserViewDangerous {
pub user: User_,
2020-12-03 18:39:56 +00:00
pub counts: UserAggregates,
2020-12-03 03:39:31 +00:00
}
impl UserViewDangerous {
pub fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> {
2020-12-03 18:39:56 +00:00
let (user, counts) = user_::table
.find(id)
.inner_join(user_aggregates::table)
.first::<(User_, UserAggregates)>(conn)?;
Ok(Self { user, counts })
2020-12-03 03:39:31 +00:00
}
}
impl UserViewSafe {
pub fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> {
2020-12-03 18:39:56 +00:00
let (user, counts) = user_::table
.find(id)
.inner_join(user_aggregates::table)
.first::<(User_, UserAggregates)>(conn)?;
Ok(Self {
user: user.to_safe(),
counts,
})
2020-12-03 03:39:31 +00:00
}
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
let admins = user_::table
2020-12-03 18:39:56 +00:00
.inner_join(user_aggregates::table)
2020-12-03 03:39:31 +00:00
.filter(user_::admin.eq(true))
.order_by(user_::published)
2020-12-03 18:39:56 +00:00
.load::<(User_, UserAggregates)>(conn)?;
2020-12-03 03:39:31 +00:00
Ok(vec_to_user_view_safe(admins))
}
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
let banned = user_::table
2020-12-03 18:39:56 +00:00
.inner_join(user_aggregates::table)
2020-12-03 03:39:31 +00:00
.filter(user_::banned.eq(true))
2020-12-03 18:39:56 +00:00
.load::<(User_, UserAggregates)>(conn)?;
2020-12-03 03:39:31 +00:00
Ok(vec_to_user_view_safe(banned))
}
}
2020-12-04 00:47:58 +00:00
mod join_types {
use crate::schema::{user_, user_aggregates};
use diesel::{
pg::Pg,
query_builder::BoxedSelectStatement,
query_source::joins::{Inner, Join, JoinOn},
sql_types::*,
};
/// TODO awful, but necessary because of the boxed join
pub(super) type BoxedUserJoin<'a> = BoxedSelectStatement<
'a,
(
(
Integer,
Text,
Nullable<Text>,
Text,
Nullable<Text>,
Nullable<Text>,
diesel::sql_types::Bool,
Bool,
Timestamp,
Nullable<Timestamp>,
Bool,
Text,
SmallInt,
SmallInt,
Text,
Bool,
Bool,
Nullable<Text>,
Text,
Nullable<Text>,
Bool,
Nullable<Text>,
Nullable<Text>,
Timestamp,
Nullable<Text>,
Bool,
),
(Integer, Integer, BigInt, BigInt, BigInt, BigInt),
),
JoinOn<
Join<user_::table, user_aggregates::table, Inner>,
diesel::expression::operators::Eq<
diesel::expression::nullable::Nullable<user_aggregates::columns::user_id>,
diesel::expression::nullable::Nullable<user_::columns::id>,
>,
>,
Pg,
>;
}
pub struct UserQueryBuilder<'a> {
conn: &'a PgConnection,
query: join_types::BoxedUserJoin<'a>,
sort: &'a SortType,
page: Option<i64>,
limit: Option<i64>,
}
impl<'a> UserQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection) -> Self {
let query = user_::table.inner_join(user_aggregates::table).into_boxed();
UserQueryBuilder {
conn,
query,
sort: &SortType::Hot,
page: None,
limit: None,
}
}
pub fn sort(mut self, sort: &'a SortType) -> Self {
self.sort = sort;
self
}
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
if let Some(search_term) = search_term.get_optional() {
self.query = self
.query
.filter(user_::name.ilike(fuzzy_search(&search_term)));
}
self
}
pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.page = page.get_optional();
self
}
pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
self.limit = limit.get_optional();
self
}
pub fn list(self) -> Result<Vec<UserViewSafe>, Error> {
let mut query = self.query;
query = match self.sort {
SortType::Hot => query
.order_by(user_aggregates::comment_score.desc())
.then_order_by(user_::published.desc()),
SortType::Active => query
.order_by(user_aggregates::comment_score.desc())
.then_order_by(user_::published.desc()),
SortType::New => query.order_by(user_::published.desc()),
SortType::TopAll => query.order_by(user_aggregates::comment_score.desc()),
SortType::TopYear => query
.filter(user_::published.gt(now - 1.years()))
.order_by(user_aggregates::comment_score.desc()),
SortType::TopMonth => query
.filter(user_::published.gt(now - 1.months()))
.order_by(user_aggregates::comment_score.desc()),
SortType::TopWeek => query
.filter(user_::published.gt(now - 1.weeks()))
.order_by(user_aggregates::comment_score.desc()),
SortType::TopDay => query
.filter(user_::published.gt(now - 1.days()))
.order_by(user_aggregates::comment_score.desc()),
};
let (limit, offset) = limit_and_offset(self.page, self.limit);
query = query.limit(limit).offset(offset);
let res = query.load::<(User_, UserAggregates)>(self.conn)?;
Ok(vec_to_user_view_safe(res))
}
}
2020-12-03 18:39:56 +00:00
fn vec_to_user_view_safe(users: Vec<(User_, UserAggregates)>) -> Vec<UserViewSafe> {
2020-12-03 03:39:31 +00:00
users
.iter()
2020-12-03 18:39:56 +00:00
.map(|a| UserViewSafe {
user: a.0.to_safe(),
counts: a.1.to_owned(),
})
2020-12-03 03:39:31 +00:00
.collect::<Vec<UserViewSafe>>()
}