1 use super::user_view::user_fast::BoxedQuery;
2 use crate::{fuzzy_search, limit_and_offset, MaybeOptional, SortType};
3 use diesel::{dsl::*, pg::Pg, result::Error, *};
11 preferred_username -> Nullable<Varchar>,
12 avatar -> Nullable<Text>,
13 banner -> Nullable<Text>,
14 email -> Nullable<Text>,
15 matrix_user_id -> Nullable<Text>,
16 bio -> Nullable<Text>,
21 send_notifications_to_email -> Bool,
22 published -> Timestamp,
23 number_of_posts -> BigInt,
25 number_of_comments -> BigInt,
26 comment_score -> BigInt,
35 preferred_username -> Nullable<Varchar>,
36 avatar -> Nullable<Text>,
37 banner -> Nullable<Text>,
38 email -> Nullable<Text>,
39 matrix_user_id -> Nullable<Text>,
40 bio -> Nullable<Text>,
45 send_notifications_to_email -> Bool,
46 published -> Timestamp,
47 number_of_posts -> BigInt,
49 number_of_comments -> BigInt,
50 comment_score -> BigInt,
54 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)]
55 #[table_name = "user_fast"]
60 pub preferred_username: Option<String>,
61 pub avatar: Option<String>,
62 pub banner: Option<String>,
63 pub email: Option<String>, // TODO this shouldn't be in this view
64 pub matrix_user_id: Option<String>,
65 pub bio: Option<String>,
69 pub show_avatars: bool, // TODO this is a setting, probably doesn't need to be here
70 pub send_notifications_to_email: bool, // TODO also never used
71 pub published: chrono::NaiveDateTime,
72 pub number_of_posts: i64,
74 pub number_of_comments: i64,
75 pub comment_score: i64,
78 pub struct UserQueryBuilder<'a> {
79 conn: &'a PgConnection,
80 query: BoxedQuery<'a, Pg>,
86 impl<'a> UserQueryBuilder<'a> {
87 pub fn create(conn: &'a PgConnection) -> Self {
88 use super::user_view::user_fast::dsl::*;
90 let query = user_fast.into_boxed();
101 pub fn sort(mut self, sort: &'a SortType) -> Self {
106 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
107 use super::user_view::user_fast::dsl::*;
108 if let Some(search_term) = search_term.get_optional() {
109 self.query = self.query.filter(name.ilike(fuzzy_search(&search_term)));
114 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
115 self.page = page.get_optional();
119 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
120 self.limit = limit.get_optional();
124 pub fn list(self) -> Result<Vec<UserView>, Error> {
125 use super::user_view::user_fast::dsl::*;
126 use diesel::sql_types::{Nullable, Text};
128 let mut query = self.query;
130 query = match self.sort {
131 SortType::Hot => query
132 .order_by(comment_score.desc())
133 .then_order_by(published.desc()),
134 SortType::Active => query
135 .order_by(comment_score.desc())
136 .then_order_by(published.desc()),
137 SortType::New => query.order_by(published.desc()),
138 SortType::TopAll => query.order_by(comment_score.desc()),
139 SortType::TopYear => query
140 .filter(published.gt(now - 1.years()))
141 .order_by(comment_score.desc()),
142 SortType::TopMonth => query
143 .filter(published.gt(now - 1.months()))
144 .order_by(comment_score.desc()),
145 SortType::TopWeek => query
146 .filter(published.gt(now - 1.weeks()))
147 .order_by(comment_score.desc()),
148 SortType::TopDay => query
149 .filter(published.gt(now - 1.days()))
150 .order_by(comment_score.desc()),
153 let (limit, offset) = limit_and_offset(self.page, self.limit);
154 query = query.limit(limit).offset(offset);
156 // The select is necessary here to not get back emails
157 query = query.select((
164 "".into_sql::<Nullable<Text>>(),
171 send_notifications_to_email,
178 query.load::<UserView>(self.conn)
183 pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
184 use super::user_view::user_fast::dsl::*;
185 use diesel::sql_types::{Nullable, Text};
187 // The select is necessary here to not get back emails
195 "".into_sql::<Nullable<Text>>(),
202 send_notifications_to_email,
209 .filter(admin.eq(true))
214 pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
215 use super::user_view::user_fast::dsl::*;
216 use diesel::sql_types::{Nullable, Text};
225 "".into_sql::<Nullable<Text>>(),
232 send_notifications_to_email,
239 .filter(banned.eq(true))
243 pub fn get_user_secure(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
244 use super::user_view::user_fast::dsl::*;
245 use diesel::sql_types::{Nullable, Text};
254 "".into_sql::<Nullable<Text>>(),
261 send_notifications_to_email,