]> Untitled Git - lemmy.git/blob - server/lemmy_db/src/user_mention_view.rs
Merge remote-tracking branch 'weblate/main' into main
[lemmy.git] / server / 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::{Deserialize, 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(
87   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
88 )]
89 #[table_name = "user_mention_fast_view"]
90 pub struct UserMentionView {
91   pub id: i32,
92   pub user_mention_id: i32,
93   pub creator_id: i32,
94   pub creator_actor_id: String,
95   pub creator_local: bool,
96   pub post_id: i32,
97   pub post_name: String,
98   pub parent_id: Option<i32>,
99   pub content: String,
100   pub removed: bool,
101   pub read: bool,
102   pub published: chrono::NaiveDateTime,
103   pub updated: Option<chrono::NaiveDateTime>,
104   pub deleted: bool,
105   pub community_id: i32,
106   pub community_actor_id: String,
107   pub community_local: bool,
108   pub community_name: String,
109   pub community_icon: Option<String>,
110   pub banned: bool,
111   pub banned_from_community: bool,
112   pub creator_name: String,
113   pub creator_preferred_username: Option<String>,
114   pub creator_avatar: Option<String>,
115   pub score: i64,
116   pub upvotes: i64,
117   pub downvotes: i64,
118   pub hot_rank: i32,
119   pub hot_rank_active: i32,
120   pub user_id: Option<i32>,
121   pub my_vote: Option<i32>,
122   pub saved: Option<bool>,
123   pub recipient_id: i32,
124   pub recipient_actor_id: String,
125   pub recipient_local: bool,
126 }
127
128 pub struct UserMentionQueryBuilder<'a> {
129   conn: &'a PgConnection,
130   query: super::user_mention_view::user_mention_fast_view::BoxedQuery<'a, Pg>,
131   for_user_id: i32,
132   sort: &'a SortType,
133   unread_only: bool,
134   page: Option<i64>,
135   limit: Option<i64>,
136 }
137
138 impl<'a> UserMentionQueryBuilder<'a> {
139   pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
140     use super::user_mention_view::user_mention_fast_view::dsl::*;
141
142     let query = user_mention_fast_view.into_boxed();
143
144     UserMentionQueryBuilder {
145       conn,
146       query,
147       for_user_id,
148       sort: &SortType::New,
149       unread_only: false,
150       page: None,
151       limit: None,
152     }
153   }
154
155   pub fn sort(mut self, sort: &'a SortType) -> Self {
156     self.sort = sort;
157     self
158   }
159
160   pub fn unread_only(mut self, unread_only: bool) -> Self {
161     self.unread_only = unread_only;
162     self
163   }
164
165   pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
166     self.page = page.get_optional();
167     self
168   }
169
170   pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
171     self.limit = limit.get_optional();
172     self
173   }
174
175   pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
176     use super::user_mention_view::user_mention_fast_view::dsl::*;
177
178     let mut query = self.query;
179
180     if self.unread_only {
181       query = query.filter(read.eq(false));
182     }
183
184     query = query
185       .filter(user_id.eq(self.for_user_id))
186       .filter(recipient_id.eq(self.for_user_id));
187
188     query = match self.sort {
189       SortType::Hot => query
190         .order_by(hot_rank.desc())
191         .then_order_by(published.desc()),
192       SortType::Active => query
193         .order_by(hot_rank_active.desc())
194         .then_order_by(published.desc()),
195       SortType::New => query.order_by(published.desc()),
196       SortType::TopAll => query.order_by(score.desc()),
197       SortType::TopYear => query
198         .filter(published.gt(now - 1.years()))
199         .order_by(score.desc()),
200       SortType::TopMonth => query
201         .filter(published.gt(now - 1.months()))
202         .order_by(score.desc()),
203       SortType::TopWeek => query
204         .filter(published.gt(now - 1.weeks()))
205         .order_by(score.desc()),
206       SortType::TopDay => query
207         .filter(published.gt(now - 1.days()))
208         .order_by(score.desc()),
209       // _ => query.order_by(published.desc()),
210     };
211
212     let (limit, offset) = limit_and_offset(self.page, self.limit);
213     query
214       .limit(limit)
215       .offset(offset)
216       .load::<UserMentionView>(self.conn)
217   }
218 }
219
220 impl UserMentionView {
221   pub fn read(
222     conn: &PgConnection,
223     from_user_mention_id: i32,
224     from_recipient_id: i32,
225   ) -> Result<Self, Error> {
226     use super::user_mention_view::user_mention_fast_view::dsl::*;
227
228     user_mention_fast_view
229       .filter(user_mention_id.eq(from_user_mention_id))
230       .filter(user_id.eq(from_recipient_id))
231       .first::<Self>(conn)
232   }
233 }