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