]> Untitled Git - lemmy.git/blob - server/src/db/user_view.rs
Adding emoji support.
[lemmy.git] / server / src / db / user_view.rs
1 use super::*;
2
3 table! {
4   user_view (id) {
5     id -> Int4,
6     name -> Varchar,
7     fedi_name -> Varchar,
8     admin -> Bool,
9     banned -> Bool,
10     published -> Timestamp,
11     number_of_posts -> BigInt,
12     post_score -> BigInt,
13     number_of_comments -> BigInt,
14     comment_score -> BigInt,
15   }
16 }
17
18 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
19 #[table_name="user_view"]
20 pub struct UserView {
21   pub id: i32,
22   pub name: String,
23   pub fedi_name: String,
24   pub admin: bool,
25   pub banned: bool,
26   pub published: chrono::NaiveDateTime,
27   pub number_of_posts: i64,
28   pub post_score: i64,
29   pub number_of_comments: i64,
30   pub comment_score: i64,
31 }
32
33 impl UserView {
34
35   pub fn list(conn: &PgConnection, 
36               sort: &SortType, 
37               search_term: Option<String>,
38               page: Option<i64>,
39               limit: Option<i64>,
40               ) -> Result<Vec<Self>, Error> {
41     use super::user_view::user_view::dsl::*;
42
43     let (limit, offset) = limit_and_offset(page, limit);
44
45     let mut query = user_view.into_boxed();
46
47     if let Some(search_term) = search_term {
48       query = query.filter(name.ilike(fuzzy_search(&search_term)));
49     };
50
51     query = match sort {
52       SortType::Hot => query.order_by(comment_score.desc())
53         .then_order_by(published.desc()),
54       SortType::New => query.order_by(published.desc()),
55       SortType::TopAll => query.order_by(comment_score.desc()),
56       SortType::TopYear => query
57         .filter(published.gt(now - 1.years()))
58         .order_by(comment_score.desc()),
59         SortType::TopMonth => query
60           .filter(published.gt(now - 1.months()))
61           .order_by(comment_score.desc()),
62           SortType::TopWeek => query
63             .filter(published.gt(now - 1.weeks()))
64             .order_by(comment_score.desc()),
65             SortType::TopDay => query
66               .filter(published.gt(now - 1.days()))
67               .order_by(comment_score.desc())
68     };
69
70     query = query
71       .limit(limit)
72       .offset(offset);
73
74     query.load::<Self>(conn) 
75   }
76
77   pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> {
78     use super::user_view::user_view::dsl::*;
79
80     user_view.find(from_user_id)
81     .first::<Self>(conn)
82   }
83
84   pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
85     use super::user_view::user_view::dsl::*;
86     user_view.filter(admin.eq(true))
87     .load::<Self>(conn)
88   }
89
90   pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
91     use super::user_view::user_view::dsl::*;
92     user_view.filter(banned.eq(true))
93     .load::<Self>(conn)
94   }
95 }
96