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