1 use super::user_view::user_mview::BoxedQuery;
10 avatar -> Nullable<Text>,
11 email -> Nullable<Text>,
12 matrix_user_id -> Nullable<Text>,
13 bio -> Nullable<Text>,
18 send_notifications_to_email -> Bool,
19 published -> Timestamp,
20 number_of_posts -> BigInt,
22 number_of_comments -> BigInt,
23 comment_score -> BigInt,
32 avatar -> Nullable<Text>,
33 email -> Nullable<Text>,
34 matrix_user_id -> Nullable<Text>,
35 bio -> Nullable<Text>,
40 send_notifications_to_email -> Bool,
41 published -> Timestamp,
42 number_of_posts -> BigInt,
44 number_of_comments -> BigInt,
45 comment_score -> BigInt,
50 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
52 #[table_name = "user_view"]
57 pub avatar: Option<String>,
58 pub email: Option<String>,
59 pub matrix_user_id: Option<String>,
60 pub bio: Option<String>,
64 pub show_avatars: bool,
65 pub send_notifications_to_email: bool,
66 pub published: chrono::NaiveDateTime,
67 pub number_of_posts: i64,
69 pub number_of_comments: i64,
70 pub comment_score: i64,
73 pub struct UserQueryBuilder<'a> {
74 conn: &'a PgConnection,
75 query: BoxedQuery<'a, Pg>,
81 impl<'a> UserQueryBuilder<'a> {
82 pub fn create(conn: &'a PgConnection) -> Self {
83 use super::user_view::user_mview::dsl::*;
85 let query = user_mview.into_boxed();
96 pub fn sort(mut self, sort: &'a SortType) -> Self {
101 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
102 use super::user_view::user_mview::dsl::*;
103 if let Some(search_term) = search_term.get_optional() {
104 self.query = self.query.filter(name.ilike(fuzzy_search(&search_term)));
109 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
110 self.page = page.get_optional();
114 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
115 self.limit = limit.get_optional();
119 pub fn list(self) -> Result<Vec<UserView>, Error> {
120 use super::user_view::user_mview::dsl::*;
122 let mut query = self.query;
124 query = match self.sort {
125 SortType::Hot => query
126 .order_by(comment_score.desc())
127 .then_order_by(published.desc()),
128 SortType::New => query.order_by(published.desc()),
129 SortType::TopAll => query.order_by(comment_score.desc()),
130 SortType::TopYear => query
131 .filter(published.gt(now - 1.years()))
132 .order_by(comment_score.desc()),
133 SortType::TopMonth => query
134 .filter(published.gt(now - 1.months()))
135 .order_by(comment_score.desc()),
136 SortType::TopWeek => query
137 .filter(published.gt(now - 1.weeks()))
138 .order_by(comment_score.desc()),
139 SortType::TopDay => query
140 .filter(published.gt(now - 1.days()))
141 .order_by(comment_score.desc()),
144 let (limit, offset) = limit_and_offset(self.page, self.limit);
145 query = query.limit(limit).offset(offset);
147 query.load::<UserView>(self.conn)
152 pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> {
153 use super::user_view::user_mview::dsl::*;
154 user_mview.find(from_user_id).first::<Self>(conn)
157 pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
158 use super::user_view::user_mview::dsl::*;
159 user_mview.filter(admin.eq(true)).load::<Self>(conn)
162 pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
163 use super::user_view::user_mview::dsl::*;
164 user_mview.filter(banned.eq(true)).load::<Self>(conn)