2020-12-04 00:47:58 +00:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_queries::{
|
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,
|
|
|
|
MaybeOptional,
|
|
|
|
SortType,
|
2020-12-05 04:18:30 +00:00
|
|
|
ToSafe,
|
2020-12-21 23:27:42 +00:00
|
|
|
ViewToVec,
|
2020-12-03 03:39:31 +00:00
|
|
|
};
|
2020-12-18 18:38:32 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
schema::{user_, user_aggregates},
|
|
|
|
source::user::{UserSafe, User_},
|
|
|
|
};
|
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-11 01:39:42 +00:00
|
|
|
type UserViewSafeTuple = (UserSafe, UserAggregates);
|
|
|
|
|
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)
|
2020-12-05 04:18:30 +00:00
|
|
|
.select((User_::safe_columns_tuple(), user_aggregates::all_columns))
|
2020-12-11 01:39:42 +00:00
|
|
|
.first::<UserViewSafeTuple>(conn)?;
|
2020-12-05 04:18:30 +00:00
|
|
|
Ok(Self { user, 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-05 04:18:30 +00:00
|
|
|
.select((User_::safe_columns_tuple(), user_aggregates::all_columns))
|
2020-12-03 03:39:31 +00:00
|
|
|
.filter(user_::admin.eq(true))
|
|
|
|
.order_by(user_::published)
|
2020-12-11 01:39:42 +00:00
|
|
|
.load::<UserViewSafeTuple>(conn)?;
|
2020-12-03 03:39:31 +00:00
|
|
|
|
2020-12-23 21:56:20 +00:00
|
|
|
Ok(Self::from_tuple_to_vec(admins))
|
2020-12-03 03:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05 04:18:30 +00:00
|
|
|
.select((User_::safe_columns_tuple(), user_aggregates::all_columns))
|
2020-12-03 03:39:31 +00:00
|
|
|
.filter(user_::banned.eq(true))
|
2020-12-11 01:39:42 +00:00
|
|
|
.load::<UserViewSafeTuple>(conn)?;
|
2020-12-03 03:39:31 +00:00
|
|
|
|
2020-12-23 21:56:20 +00:00
|
|
|
Ok(Self::from_tuple_to_vec(banned))
|
2020-12-03 03:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 00:47:58 +00:00
|
|
|
pub struct UserQueryBuilder<'a> {
|
|
|
|
conn: &'a PgConnection,
|
|
|
|
sort: &'a SortType,
|
2020-12-16 18:59:43 +00:00
|
|
|
search_term: Option<String>,
|
2020-12-04 00:47:58 +00:00
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> UserQueryBuilder<'a> {
|
|
|
|
pub fn create(conn: &'a PgConnection) -> Self {
|
|
|
|
UserQueryBuilder {
|
|
|
|
conn,
|
2020-12-16 18:59:43 +00:00
|
|
|
search_term: None,
|
2020-12-04 00:47:58 +00:00
|
|
|
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 {
|
2020-12-16 18:59:43 +00:00
|
|
|
self.search_term = search_term.get_optional();
|
2020-12-04 00:47:58 +00:00
|
|
|
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> {
|
2020-12-16 18:59:43 +00:00
|
|
|
let mut query = user_::table
|
|
|
|
.inner_join(user_aggregates::table)
|
|
|
|
.select((User_::safe_columns_tuple(), user_aggregates::all_columns))
|
|
|
|
.into_boxed();
|
|
|
|
|
|
|
|
if let Some(search_term) = self.search_term {
|
|
|
|
query = query.filter(user_::name.ilike(fuzzy_search(&search_term)));
|
|
|
|
}
|
2020-12-04 00:47:58 +00:00
|
|
|
|
|
|
|
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()),
|
2021-02-18 15:38:25 +00:00
|
|
|
SortType::New | SortType::MostComments | SortType::NewComments => {
|
|
|
|
query.order_by(user_::published.desc())
|
|
|
|
}
|
2020-12-04 00:47:58 +00:00
|
|
|
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);
|
|
|
|
|
2020-12-11 01:39:42 +00:00
|
|
|
let res = query.load::<UserViewSafeTuple>(self.conn)?;
|
2020-12-04 00:47:58 +00:00
|
|
|
|
2020-12-23 21:56:20 +00:00
|
|
|
Ok(UserViewSafe::from_tuple_to_vec(res))
|
2020-12-04 00:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 01:39:42 +00:00
|
|
|
impl ViewToVec for UserViewSafe {
|
|
|
|
type DbTuple = UserViewSafeTuple;
|
2020-12-23 21:56:20 +00:00
|
|
|
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
|
|
|
|
items
|
2020-12-11 01:39:42 +00:00
|
|
|
.iter()
|
|
|
|
.map(|a| Self {
|
|
|
|
user: a.0.to_owned(),
|
|
|
|
counts: a.1.to_owned(),
|
|
|
|
})
|
|
|
|
.collect::<Vec<Self>>()
|
|
|
|
}
|
2020-12-03 03:39:31 +00:00
|
|
|
}
|