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