1 use diesel::{dsl::*, result::Error, *};
2 use lemmy_db_queries::{
3 aggregates::user_aggregates::UserAggregates,
11 use lemmy_db_schema::{
12 schema::{user_, user_aggregates},
13 source::user::{UserSafe, User_},
17 #[derive(Debug, Serialize, Clone)]
18 pub struct UserViewSafe {
20 pub counts: UserAggregates,
23 type UserViewSafeTuple = (UserSafe, UserAggregates);
26 pub fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> {
27 let (user, counts) = user_::table
29 .inner_join(user_aggregates::table)
30 .select((User_::safe_columns_tuple(), user_aggregates::all_columns))
31 .first::<UserViewSafeTuple>(conn)?;
32 Ok(Self { user, counts })
35 pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
36 let admins = user_::table
37 .inner_join(user_aggregates::table)
38 .select((User_::safe_columns_tuple(), user_aggregates::all_columns))
39 .filter(user_::admin.eq(true))
40 .order_by(user_::published)
41 .load::<UserViewSafeTuple>(conn)?;
43 Ok(Self::from_tuple_to_vec(admins))
46 pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
47 let banned = user_::table
48 .inner_join(user_aggregates::table)
49 .select((User_::safe_columns_tuple(), user_aggregates::all_columns))
50 .filter(user_::banned.eq(true))
51 .load::<UserViewSafeTuple>(conn)?;
53 Ok(Self::from_tuple_to_vec(banned))
57 pub struct UserQueryBuilder<'a> {
58 conn: &'a PgConnection,
60 search_term: Option<String>,
65 impl<'a> UserQueryBuilder<'a> {
66 pub fn create(conn: &'a PgConnection) -> Self {
76 pub fn sort(mut self, sort: &'a SortType) -> Self {
81 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
82 self.search_term = search_term.get_optional();
86 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
87 self.page = page.get_optional();
91 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
92 self.limit = limit.get_optional();
96 pub fn list(self) -> Result<Vec<UserViewSafe>, Error> {
97 let mut query = user_::table
98 .inner_join(user_aggregates::table)
99 .select((User_::safe_columns_tuple(), user_aggregates::all_columns))
102 if let Some(search_term) = self.search_term {
103 query = query.filter(user_::name.ilike(fuzzy_search(&search_term)));
106 query = match self.sort {
107 SortType::Hot => query
108 .order_by(user_aggregates::comment_score.desc())
109 .then_order_by(user_::published.desc()),
110 SortType::Active => query
111 .order_by(user_aggregates::comment_score.desc())
112 .then_order_by(user_::published.desc()),
113 SortType::New | SortType::MostComments => query.order_by(user_::published.desc()),
114 SortType::TopAll => query.order_by(user_aggregates::comment_score.desc()),
115 SortType::TopYear => query
116 .filter(user_::published.gt(now - 1.years()))
117 .order_by(user_aggregates::comment_score.desc()),
118 SortType::TopMonth => query
119 .filter(user_::published.gt(now - 1.months()))
120 .order_by(user_aggregates::comment_score.desc()),
121 SortType::TopWeek => query
122 .filter(user_::published.gt(now - 1.weeks()))
123 .order_by(user_aggregates::comment_score.desc()),
124 SortType::TopDay => query
125 .filter(user_::published.gt(now - 1.days()))
126 .order_by(user_aggregates::comment_score.desc()),
129 let (limit, offset) = limit_and_offset(self.page, self.limit);
130 query = query.limit(limit).offset(offset);
132 let res = query.load::<UserViewSafeTuple>(self.conn)?;
134 Ok(UserViewSafe::from_tuple_to_vec(res))
138 impl ViewToVec for UserViewSafe {
139 type DbTuple = UserViewSafeTuple;
140 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
144 user: a.0.to_owned(),
145 counts: a.1.to_owned(),
147 .collect::<Vec<Self>>()