]> Untitled Git - lemmy.git/blob - server/src/db/user_mention_view.rs
Merge branch 'admin_settings' into dev
[lemmy.git] / server / src / db / user_mention_view.rs
1 use super::*;
2 use diesel::pg::Pg;
3
4 // The faked schema since diesel doesn't do views
5 table! {
6   user_mention_view (id) {
7     id -> Int4,
8     user_mention_id -> Int4,
9     creator_id -> Int4,
10     post_id -> Int4,
11     parent_id -> Nullable<Int4>,
12     content -> Text,
13     removed -> Bool,
14     read -> Bool,
15     published -> Timestamp,
16     updated -> Nullable<Timestamp>,
17     deleted -> Bool,
18     community_id -> Int4,
19     community_name -> Varchar,
20     banned -> Bool,
21     banned_from_community -> Bool,
22     creator_name -> Varchar,
23     creator_avatar -> Nullable<Text>,
24     score -> BigInt,
25     upvotes -> BigInt,
26     downvotes -> BigInt,
27     hot_rank -> Int4,
28     user_id -> Nullable<Int4>,
29     my_vote -> Nullable<Int4>,
30     saved -> Nullable<Bool>,
31     recipient_id -> Int4,
32   }
33 }
34
35 table! {
36   user_mention_mview (id) {
37     id -> Int4,
38     user_mention_id -> Int4,
39     creator_id -> Int4,
40     post_id -> Int4,
41     parent_id -> Nullable<Int4>,
42     content -> Text,
43     removed -> Bool,
44     read -> Bool,
45     published -> Timestamp,
46     updated -> Nullable<Timestamp>,
47     deleted -> Bool,
48     community_id -> Int4,
49     community_name -> Varchar,
50     banned -> Bool,
51     banned_from_community -> Bool,
52     creator_name -> Varchar,
53     creator_avatar -> Nullable<Text>,
54     score -> BigInt,
55     upvotes -> BigInt,
56     downvotes -> BigInt,
57     hot_rank -> Int4,
58     user_id -> Nullable<Int4>,
59     my_vote -> Nullable<Int4>,
60     saved -> Nullable<Bool>,
61     recipient_id -> Int4,
62   }
63 }
64
65 #[derive(
66   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
67 )]
68 #[table_name = "user_mention_view"]
69 pub struct UserMentionView {
70   pub id: i32,
71   pub user_mention_id: i32,
72   pub creator_id: i32,
73   pub post_id: i32,
74   pub parent_id: Option<i32>,
75   pub content: String,
76   pub removed: bool,
77   pub read: bool,
78   pub published: chrono::NaiveDateTime,
79   pub updated: Option<chrono::NaiveDateTime>,
80   pub deleted: bool,
81   pub community_id: i32,
82   pub community_name: String,
83   pub banned: bool,
84   pub banned_from_community: bool,
85   pub creator_name: String,
86   pub creator_avatar: Option<String>,
87   pub score: i64,
88   pub upvotes: i64,
89   pub downvotes: i64,
90   pub hot_rank: i32,
91   pub user_id: Option<i32>,
92   pub my_vote: Option<i32>,
93   pub saved: Option<bool>,
94   pub recipient_id: i32,
95 }
96
97 pub struct UserMentionQueryBuilder<'a> {
98   conn: &'a PgConnection,
99   query: super::user_mention_view::user_mention_mview::BoxedQuery<'a, Pg>,
100   for_user_id: i32,
101   sort: &'a SortType,
102   unread_only: bool,
103   page: Option<i64>,
104   limit: Option<i64>,
105 }
106
107 impl<'a> UserMentionQueryBuilder<'a> {
108   pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
109     use super::user_mention_view::user_mention_mview::dsl::*;
110
111     let query = user_mention_mview.into_boxed();
112
113     UserMentionQueryBuilder {
114       conn,
115       query,
116       for_user_id,
117       sort: &SortType::New,
118       unread_only: false,
119       page: None,
120       limit: None,
121     }
122   }
123
124   pub fn sort(mut self, sort: &'a SortType) -> Self {
125     self.sort = sort;
126     self
127   }
128
129   pub fn unread_only(mut self, unread_only: bool) -> Self {
130     self.unread_only = unread_only;
131     self
132   }
133
134   pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
135     self.page = page.get_optional();
136     self
137   }
138
139   pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
140     self.limit = limit.get_optional();
141     self
142   }
143
144   pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
145     use super::user_mention_view::user_mention_mview::dsl::*;
146
147     let mut query = self.query;
148
149     if self.unread_only {
150       query = query.filter(read.eq(false));
151     }
152
153     query = query
154       .filter(user_id.eq(self.for_user_id))
155       .filter(recipient_id.eq(self.for_user_id));
156
157     query = match self.sort {
158       SortType::Hot => query
159         .order_by(hot_rank.desc())
160         .then_order_by(published.desc()),
161       SortType::New => query.order_by(published.desc()),
162       SortType::TopAll => query.order_by(score.desc()),
163       SortType::TopYear => query
164         .filter(published.gt(now - 1.years()))
165         .order_by(score.desc()),
166       SortType::TopMonth => query
167         .filter(published.gt(now - 1.months()))
168         .order_by(score.desc()),
169       SortType::TopWeek => query
170         .filter(published.gt(now - 1.weeks()))
171         .order_by(score.desc()),
172       SortType::TopDay => query
173         .filter(published.gt(now - 1.days()))
174         .order_by(score.desc()),
175       // _ => query.order_by(published.desc()),
176     };
177
178     let (limit, offset) = limit_and_offset(self.page, self.limit);
179     query
180       .limit(limit)
181       .offset(offset)
182       .load::<UserMentionView>(self.conn)
183   }
184 }
185
186 impl UserMentionView {
187   pub fn read(
188     conn: &PgConnection,
189     from_user_mention_id: i32,
190     from_recipient_id: i32,
191   ) -> Result<Self, Error> {
192     use super::user_mention_view::user_mention_view::dsl::*;
193
194     user_mention_view
195       .filter(user_mention_id.eq(from_user_mention_id))
196       .filter(user_id.eq(from_recipient_id))
197       .first::<Self>(conn)
198   }
199 }