]> Untitled Git - lemmy.git/blob - server/src/db/community_view.rs
Spanish translations
[lemmy.git] / server / src / db / community_view.rs
1 use super::*;
2
3 table! {
4   community_view (id) {
5     id -> Int4,
6     name -> Varchar,
7     title -> Varchar,
8     description -> Nullable<Text>,
9     category_id -> Int4,
10     creator_id -> Int4,
11     removed -> Bool,
12     published -> Timestamp,
13     updated -> Nullable<Timestamp>,
14     deleted -> Bool,
15     nsfw -> Bool,
16     creator_name -> Varchar,
17     category_name -> Varchar,
18     number_of_subscribers -> BigInt,
19     number_of_posts -> BigInt,
20     number_of_comments -> BigInt,
21     hot_rank -> Int4,
22     user_id -> Nullable<Int4>,
23     subscribed -> Nullable<Bool>,
24   }
25 }
26
27 table! {
28   community_moderator_view (id) {
29     id -> Int4,
30     community_id -> Int4,
31     user_id -> Int4,
32     published -> Timestamp,
33     user_name -> Varchar,
34     community_name -> Varchar,
35   }
36 }
37
38 table! {
39   community_follower_view (id) {
40     id -> Int4,
41     community_id -> Int4,
42     user_id -> Int4,
43     published -> Timestamp,
44     user_name -> Varchar,
45     community_name -> Varchar,
46   }
47 }
48
49 table! {
50   community_user_ban_view (id) {
51     id -> Int4,
52     community_id -> Int4,
53     user_id -> Int4,
54     published -> Timestamp,
55     user_name -> Varchar,
56     community_name -> Varchar,
57   }
58 }
59
60 table! {
61   site_view (id) {
62     id -> Int4,
63     name -> Varchar,
64     description -> Nullable<Text>,
65     creator_id -> Int4,
66     published -> Timestamp,
67     updated -> Nullable<Timestamp>,
68     creator_name -> Varchar,
69     number_of_users -> BigInt,
70     number_of_posts -> BigInt,
71     number_of_comments -> BigInt,
72     number_of_communities -> BigInt,
73   }
74 }
75
76 #[derive(
77   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
78 )]
79 #[table_name = "community_view"]
80 pub struct CommunityView {
81   pub id: i32,
82   pub name: String,
83   pub title: String,
84   pub description: Option<String>,
85   pub category_id: i32,
86   pub creator_id: i32,
87   pub removed: bool,
88   pub published: chrono::NaiveDateTime,
89   pub updated: Option<chrono::NaiveDateTime>,
90   pub deleted: bool,
91   pub nsfw: bool,
92   pub creator_name: String,
93   pub category_name: String,
94   pub number_of_subscribers: i64,
95   pub number_of_posts: i64,
96   pub number_of_comments: i64,
97   pub hot_rank: i32,
98   pub user_id: Option<i32>,
99   pub subscribed: Option<bool>,
100 }
101
102 impl CommunityView {
103   pub fn read(
104     conn: &PgConnection,
105     from_community_id: i32,
106     from_user_id: Option<i32>,
107   ) -> Result<Self, Error> {
108     use super::community_view::community_view::dsl::*;
109
110     let mut query = community_view.into_boxed();
111
112     query = query.filter(id.eq(from_community_id));
113
114     // The view lets you pass a null user_id, if you're not logged in
115     if let Some(from_user_id) = from_user_id {
116       query = query.filter(user_id.eq(from_user_id));
117     } else {
118       query = query.filter(user_id.is_null());
119     };
120
121     query.first::<Self>(conn)
122   }
123
124   pub fn list(
125     conn: &PgConnection,
126     sort: &SortType,
127     from_user_id: Option<i32>,
128     show_nsfw: bool,
129     search_term: Option<String>,
130     page: Option<i64>,
131     limit: Option<i64>,
132   ) -> Result<Vec<Self>, Error> {
133     use super::community_view::community_view::dsl::*;
134     let mut query = community_view.into_boxed();
135
136     let (limit, offset) = limit_and_offset(page, limit);
137
138     if let Some(search_term) = search_term {
139       query = query.filter(name.ilike(fuzzy_search(&search_term)));
140     };
141
142     // The view lets you pass a null user_id, if you're not logged in
143     match sort {
144       SortType::Hot => {
145         query = query
146           .order_by(hot_rank.desc())
147           .then_order_by(number_of_subscribers.desc())
148           .filter(user_id.is_null())
149       }
150       SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
151       SortType::TopAll => match from_user_id {
152         Some(from_user_id) => {
153           query = query
154             .filter(user_id.eq(from_user_id))
155             .order_by((subscribed.asc(), number_of_subscribers.desc()))
156         }
157         None => {
158           query = query
159             .order_by(number_of_subscribers.desc())
160             .filter(user_id.is_null())
161         }
162       },
163       _ => (),
164     };
165
166     if !show_nsfw {
167       query = query.filter(nsfw.eq(false));
168     };
169
170     query
171       .limit(limit)
172       .offset(offset)
173       .filter(removed.eq(false))
174       .filter(deleted.eq(false))
175       .load::<Self>(conn)
176   }
177 }
178
179 #[derive(
180   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
181 )]
182 #[table_name = "community_moderator_view"]
183 pub struct CommunityModeratorView {
184   pub id: i32,
185   pub community_id: i32,
186   pub user_id: i32,
187   pub published: chrono::NaiveDateTime,
188   pub user_name: String,
189   pub community_name: String,
190 }
191
192 impl CommunityModeratorView {
193   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
194     use super::community_view::community_moderator_view::dsl::*;
195     community_moderator_view
196       .filter(community_id.eq(from_community_id))
197       .load::<Self>(conn)
198   }
199
200   pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
201     use super::community_view::community_moderator_view::dsl::*;
202     community_moderator_view
203       .filter(user_id.eq(from_user_id))
204       .load::<Self>(conn)
205   }
206 }
207
208 #[derive(
209   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
210 )]
211 #[table_name = "community_follower_view"]
212 pub struct CommunityFollowerView {
213   pub id: i32,
214   pub community_id: i32,
215   pub user_id: i32,
216   pub published: chrono::NaiveDateTime,
217   pub user_name: String,
218   pub community_name: String,
219 }
220
221 impl CommunityFollowerView {
222   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
223     use super::community_view::community_follower_view::dsl::*;
224     community_follower_view
225       .filter(community_id.eq(from_community_id))
226       .load::<Self>(conn)
227   }
228
229   pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
230     use super::community_view::community_follower_view::dsl::*;
231     community_follower_view
232       .filter(user_id.eq(from_user_id))
233       .load::<Self>(conn)
234   }
235 }
236
237 #[derive(
238   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
239 )]
240 #[table_name = "community_user_ban_view"]
241 pub struct CommunityUserBanView {
242   pub id: i32,
243   pub community_id: i32,
244   pub user_id: i32,
245   pub published: chrono::NaiveDateTime,
246   pub user_name: String,
247   pub community_name: String,
248 }
249
250 impl CommunityUserBanView {
251   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
252     use super::community_view::community_user_ban_view::dsl::*;
253     community_user_ban_view
254       .filter(community_id.eq(from_community_id))
255       .load::<Self>(conn)
256   }
257
258   pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
259     use super::community_view::community_user_ban_view::dsl::*;
260     community_user_ban_view
261       .filter(user_id.eq(from_user_id))
262       .load::<Self>(conn)
263   }
264
265   pub fn get(
266     conn: &PgConnection,
267     from_user_id: i32,
268     from_community_id: i32,
269   ) -> Result<Self, Error> {
270     use super::community_view::community_user_ban_view::dsl::*;
271     community_user_ban_view
272       .filter(user_id.eq(from_user_id))
273       .filter(community_id.eq(from_community_id))
274       .first::<Self>(conn)
275   }
276 }
277
278 #[derive(
279   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
280 )]
281 #[table_name = "site_view"]
282 pub struct SiteView {
283   pub id: i32,
284   pub name: String,
285   pub description: Option<String>,
286   pub creator_id: i32,
287   pub published: chrono::NaiveDateTime,
288   pub updated: Option<chrono::NaiveDateTime>,
289   pub creator_name: String,
290   pub number_of_users: i64,
291   pub number_of_posts: i64,
292   pub number_of_comments: i64,
293   pub number_of_communities: i64,
294 }
295
296 impl SiteView {
297   pub fn read(conn: &PgConnection) -> Result<Self, Error> {
298     use super::community_view::site_view::dsl::*;
299     site_view.first::<Self>(conn)
300   }
301 }