]> Untitled Git - lemmy.git/blob - lemmy_db/src/community_view.rs
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / lemmy_db / src / community_view.rs
1 use super::community_view::community_fast_view::BoxedQuery;
2 use crate::{fuzzy_search, limit_and_offset, MaybeOptional, SortType};
3 use diesel::{pg::Pg, result::Error, *};
4 use serde::{Deserialize, Serialize};
5
6 table! {
7   community_view (id) {
8     id -> Int4,
9     name -> Varchar,
10     title -> Varchar,
11     icon -> Nullable<Text>,
12     banner -> Nullable<Text>,
13     description -> Nullable<Text>,
14     category_id -> Int4,
15     creator_id -> Int4,
16     removed -> Bool,
17     published -> Timestamp,
18     updated -> Nullable<Timestamp>,
19     deleted -> Bool,
20     nsfw -> Bool,
21     actor_id -> Text,
22     local -> Bool,
23     last_refreshed_at -> Timestamp,
24     creator_actor_id -> Text,
25     creator_local -> Bool,
26     creator_name -> Varchar,
27     creator_preferred_username -> Nullable<Varchar>,
28     creator_avatar -> Nullable<Text>,
29     category_name -> Varchar,
30     number_of_subscribers -> BigInt,
31     number_of_posts -> BigInt,
32     number_of_comments -> BigInt,
33     hot_rank -> Int4,
34     user_id -> Nullable<Int4>,
35     subscribed -> Nullable<Bool>,
36   }
37 }
38
39 table! {
40   community_fast_view (id) {
41     id -> Int4,
42     name -> Varchar,
43     title -> Varchar,
44     icon -> Nullable<Text>,
45     banner -> Nullable<Text>,
46     description -> Nullable<Text>,
47     category_id -> Int4,
48     creator_id -> Int4,
49     removed -> Bool,
50     published -> Timestamp,
51     updated -> Nullable<Timestamp>,
52     deleted -> Bool,
53     nsfw -> Bool,
54     actor_id -> Text,
55     local -> Bool,
56     last_refreshed_at -> Timestamp,
57     creator_actor_id -> Text,
58     creator_local -> Bool,
59     creator_name -> Varchar,
60     creator_preferred_username -> Nullable<Varchar>,
61     creator_avatar -> Nullable<Text>,
62     category_name -> Varchar,
63     number_of_subscribers -> BigInt,
64     number_of_posts -> BigInt,
65     number_of_comments -> BigInt,
66     hot_rank -> Int4,
67     user_id -> Nullable<Int4>,
68     subscribed -> Nullable<Bool>,
69   }
70 }
71
72 table! {
73   community_moderator_view (id) {
74     id -> Int4,
75     community_id -> Int4,
76     user_id -> Int4,
77     published -> Timestamp,
78     user_actor_id -> Text,
79     user_local -> Bool,
80     user_name -> Varchar,
81     user_preferred_username -> Nullable<Varchar>,
82     avatar -> Nullable<Text>,
83     community_actor_id -> Text,
84     community_local -> Bool,
85     community_name -> Varchar,
86     community_icon -> Nullable<Text>,
87   }
88 }
89
90 table! {
91   community_follower_view (id) {
92     id -> Int4,
93     community_id -> Int4,
94     user_id -> Int4,
95     published -> Timestamp,
96     user_actor_id -> Text,
97     user_local -> Bool,
98     user_name -> Varchar,
99     user_preferred_username -> Nullable<Varchar>,
100     avatar -> Nullable<Text>,
101     community_actor_id -> Text,
102     community_local -> Bool,
103     community_name -> Varchar,
104     community_icon -> Nullable<Text>,
105   }
106 }
107
108 table! {
109   community_user_ban_view (id) {
110     id -> Int4,
111     community_id -> Int4,
112     user_id -> Int4,
113     published -> Timestamp,
114     user_actor_id -> Text,
115     user_local -> Bool,
116     user_name -> Varchar,
117     user_preferred_username -> Nullable<Varchar>,
118     avatar -> Nullable<Text>,
119     community_actor_id -> Text,
120     community_local -> Bool,
121     community_name -> Varchar,
122     community_icon -> Nullable<Text>,
123   }
124 }
125
126 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)]
127 #[table_name = "community_fast_view"]
128 pub struct CommunityView {
129   pub id: i32,
130   pub name: String,
131   pub title: String,
132   pub icon: Option<String>,
133   pub banner: Option<String>,
134   pub description: Option<String>,
135   pub category_id: i32,
136   pub creator_id: i32,
137   pub removed: bool,
138   pub published: chrono::NaiveDateTime,
139   pub updated: Option<chrono::NaiveDateTime>,
140   pub deleted: bool,
141   pub nsfw: bool,
142   pub actor_id: String,
143   pub local: bool,
144   pub last_refreshed_at: chrono::NaiveDateTime,
145   pub creator_actor_id: String,
146   pub creator_local: bool,
147   pub creator_name: String,
148   pub creator_preferred_username: Option<String>,
149   pub creator_avatar: Option<String>,
150   pub category_name: String,
151   pub number_of_subscribers: i64,
152   pub number_of_posts: i64,
153   pub number_of_comments: i64,
154   pub hot_rank: i32,
155   pub user_id: Option<i32>,
156   pub subscribed: Option<bool>,
157 }
158
159 pub struct CommunityQueryBuilder<'a> {
160   conn: &'a PgConnection,
161   query: BoxedQuery<'a, Pg>,
162   sort: &'a SortType,
163   from_user_id: Option<i32>,
164   show_nsfw: bool,
165   search_term: Option<String>,
166   page: Option<i64>,
167   limit: Option<i64>,
168 }
169
170 impl<'a> CommunityQueryBuilder<'a> {
171   pub fn create(conn: &'a PgConnection) -> Self {
172     use super::community_view::community_fast_view::dsl::*;
173
174     let query = community_fast_view.into_boxed();
175
176     CommunityQueryBuilder {
177       conn,
178       query,
179       sort: &SortType::Hot,
180       from_user_id: None,
181       show_nsfw: true,
182       search_term: None,
183       page: None,
184       limit: None,
185     }
186   }
187
188   pub fn sort(mut self, sort: &'a SortType) -> Self {
189     self.sort = sort;
190     self
191   }
192
193   pub fn for_user<T: MaybeOptional<i32>>(mut self, from_user_id: T) -> Self {
194     self.from_user_id = from_user_id.get_optional();
195     self
196   }
197
198   pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
199     self.show_nsfw = show_nsfw;
200     self
201   }
202
203   pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
204     self.search_term = search_term.get_optional();
205     self
206   }
207
208   pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
209     self.page = page.get_optional();
210     self
211   }
212
213   pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
214     self.limit = limit.get_optional();
215     self
216   }
217
218   pub fn list(self) -> Result<Vec<CommunityView>, Error> {
219     use super::community_view::community_fast_view::dsl::*;
220
221     let mut query = self.query;
222
223     if let Some(search_term) = self.search_term {
224       let searcher = fuzzy_search(&search_term);
225       query = query
226         .filter(name.ilike(searcher.to_owned()))
227         .or_filter(title.ilike(searcher.to_owned()))
228         .or_filter(description.ilike(searcher));
229     };
230
231     // The view lets you pass a null user_id, if you're not logged in
232     match self.sort {
233       SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
234       SortType::TopAll => match self.from_user_id {
235         Some(from_user_id) => {
236           query = query
237             .filter(user_id.eq(from_user_id))
238             .order_by((subscribed.asc(), number_of_subscribers.desc()))
239         }
240         None => {
241           query = query
242             .order_by(number_of_subscribers.desc())
243             .filter(user_id.is_null())
244         }
245       },
246       // Covers all other sorts, including hot
247       _ => {
248         query = query
249           .order_by(hot_rank.desc())
250           .then_order_by(number_of_subscribers.desc())
251           .filter(user_id.is_null())
252       }
253     };
254
255     if !self.show_nsfw {
256       query = query.filter(nsfw.eq(false));
257     };
258
259     let (limit, offset) = limit_and_offset(self.page, self.limit);
260     query
261       .limit(limit)
262       .offset(offset)
263       .filter(removed.eq(false))
264       .filter(deleted.eq(false))
265       .load::<CommunityView>(self.conn)
266   }
267 }
268
269 impl CommunityView {
270   pub fn read(
271     conn: &PgConnection,
272     from_community_id: i32,
273     from_user_id: Option<i32>,
274   ) -> Result<Self, Error> {
275     use super::community_view::community_fast_view::dsl::*;
276
277     let mut query = community_fast_view.into_boxed();
278
279     query = query.filter(id.eq(from_community_id));
280
281     // The view lets you pass a null user_id, if you're not logged in
282     if let Some(from_user_id) = from_user_id {
283       query = query.filter(user_id.eq(from_user_id));
284     } else {
285       query = query.filter(user_id.is_null());
286     };
287
288     query.first::<Self>(conn)
289   }
290 }
291
292 #[derive(
293   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
294 )]
295 #[table_name = "community_moderator_view"]
296 pub struct CommunityModeratorView {
297   pub id: i32,
298   pub community_id: i32,
299   pub user_id: i32,
300   pub published: chrono::NaiveDateTime,
301   pub user_actor_id: String,
302   pub user_local: bool,
303   pub user_name: String,
304   pub user_preferred_username: Option<String>,
305   pub avatar: Option<String>,
306   pub community_actor_id: String,
307   pub community_local: bool,
308   pub community_name: String,
309   pub community_icon: Option<String>,
310 }
311
312 impl CommunityModeratorView {
313   pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result<Vec<Self>, Error> {
314     use super::community_view::community_moderator_view::dsl::*;
315     community_moderator_view
316       .filter(community_id.eq(for_community_id))
317       .order_by(published)
318       .load::<Self>(conn)
319   }
320
321   pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result<Vec<Self>, Error> {
322     use super::community_view::community_moderator_view::dsl::*;
323     community_moderator_view
324       .filter(user_id.eq(for_user_id))
325       .order_by(published)
326       .load::<Self>(conn)
327   }
328 }
329
330 #[derive(
331   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
332 )]
333 #[table_name = "community_follower_view"]
334 pub struct CommunityFollowerView {
335   pub id: i32,
336   pub community_id: i32,
337   pub user_id: i32,
338   pub published: chrono::NaiveDateTime,
339   pub user_actor_id: String,
340   pub user_local: bool,
341   pub user_name: String,
342   pub user_preferred_username: Option<String>,
343   pub avatar: Option<String>,
344   pub community_actor_id: String,
345   pub community_local: bool,
346   pub community_name: String,
347   pub community_icon: Option<String>,
348 }
349
350 impl CommunityFollowerView {
351   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
352     use super::community_view::community_follower_view::dsl::*;
353     community_follower_view
354       .filter(community_id.eq(from_community_id))
355       .load::<Self>(conn)
356   }
357
358   pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
359     use super::community_view::community_follower_view::dsl::*;
360     community_follower_view
361       .filter(user_id.eq(from_user_id))
362       .load::<Self>(conn)
363   }
364 }
365
366 #[derive(
367   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
368 )]
369 #[table_name = "community_user_ban_view"]
370 pub struct CommunityUserBanView {
371   pub id: i32,
372   pub community_id: i32,
373   pub user_id: i32,
374   pub published: chrono::NaiveDateTime,
375   pub user_actor_id: String,
376   pub user_local: bool,
377   pub user_name: String,
378   pub user_preferred_username: Option<String>,
379   pub avatar: Option<String>,
380   pub community_actor_id: String,
381   pub community_local: bool,
382   pub community_name: String,
383   pub community_icon: Option<String>,
384 }
385
386 impl CommunityUserBanView {
387   pub fn get(
388     conn: &PgConnection,
389     from_user_id: i32,
390     from_community_id: i32,
391   ) -> Result<Self, Error> {
392     use super::community_view::community_user_ban_view::dsl::*;
393     community_user_ban_view
394       .filter(user_id.eq(from_user_id))
395       .filter(community_id.eq(from_community_id))
396       .first::<Self>(conn)
397   }
398 }