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