]> Untitled Git - lemmy.git/blob - server/lemmy_db/src/community_view.rs
Merge remote-tracking branch 'weblate/main' into main
[lemmy.git] / server / 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(
127   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
128 )]
129 #[table_name = "community_fast_view"]
130 pub struct CommunityView {
131   pub id: i32,
132   pub name: String,
133   pub title: String,
134   pub icon: Option<String>,
135   pub banner: Option<String>,
136   pub description: Option<String>,
137   pub category_id: i32,
138   pub creator_id: i32,
139   pub removed: bool,
140   pub published: chrono::NaiveDateTime,
141   pub updated: Option<chrono::NaiveDateTime>,
142   pub deleted: bool,
143   pub nsfw: bool,
144   pub actor_id: String,
145   pub local: bool,
146   pub last_refreshed_at: chrono::NaiveDateTime,
147   pub creator_actor_id: String,
148   pub creator_local: bool,
149   pub creator_name: String,
150   pub creator_preferred_username: Option<String>,
151   pub creator_avatar: Option<String>,
152   pub category_name: String,
153   pub number_of_subscribers: i64,
154   pub number_of_posts: i64,
155   pub number_of_comments: i64,
156   pub hot_rank: i32,
157   pub user_id: Option<i32>,
158   pub subscribed: Option<bool>,
159 }
160
161 pub struct CommunityQueryBuilder<'a> {
162   conn: &'a PgConnection,
163   query: BoxedQuery<'a, Pg>,
164   sort: &'a SortType,
165   from_user_id: Option<i32>,
166   show_nsfw: bool,
167   search_term: Option<String>,
168   page: Option<i64>,
169   limit: Option<i64>,
170 }
171
172 impl<'a> CommunityQueryBuilder<'a> {
173   pub fn create(conn: &'a PgConnection) -> Self {
174     use super::community_view::community_fast_view::dsl::*;
175
176     let query = community_fast_view.into_boxed();
177
178     CommunityQueryBuilder {
179       conn,
180       query,
181       sort: &SortType::Hot,
182       from_user_id: None,
183       show_nsfw: true,
184       search_term: None,
185       page: None,
186       limit: None,
187     }
188   }
189
190   pub fn sort(mut self, sort: &'a SortType) -> Self {
191     self.sort = sort;
192     self
193   }
194
195   pub fn for_user<T: MaybeOptional<i32>>(mut self, from_user_id: T) -> Self {
196     self.from_user_id = from_user_id.get_optional();
197     self
198   }
199
200   pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
201     self.show_nsfw = show_nsfw;
202     self
203   }
204
205   pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
206     self.search_term = search_term.get_optional();
207     self
208   }
209
210   pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
211     self.page = page.get_optional();
212     self
213   }
214
215   pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
216     self.limit = limit.get_optional();
217     self
218   }
219
220   pub fn list(self) -> Result<Vec<CommunityView>, Error> {
221     use super::community_view::community_fast_view::dsl::*;
222
223     let mut query = self.query;
224
225     if let Some(search_term) = self.search_term {
226       let searcher = fuzzy_search(&search_term);
227       query = query
228         .filter(name.ilike(searcher.to_owned()))
229         .or_filter(title.ilike(searcher.to_owned()))
230         .or_filter(description.ilike(searcher));
231     };
232
233     // The view lets you pass a null user_id, if you're not logged in
234     match self.sort {
235       SortType::Hot => {
236         query = query
237           .order_by(hot_rank.desc())
238           .then_order_by(number_of_subscribers.desc())
239           .filter(user_id.is_null())
240       }
241       SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
242       SortType::TopAll => match self.from_user_id {
243         Some(from_user_id) => {
244           query = query
245             .filter(user_id.eq(from_user_id))
246             .order_by((subscribed.asc(), number_of_subscribers.desc()))
247         }
248         None => {
249           query = query
250             .order_by(number_of_subscribers.desc())
251             .filter(user_id.is_null())
252         }
253       },
254       _ => (),
255     };
256
257     if !self.show_nsfw {
258       query = query.filter(nsfw.eq(false));
259     };
260
261     let (limit, offset) = limit_and_offset(self.page, self.limit);
262     query
263       .limit(limit)
264       .offset(offset)
265       .filter(removed.eq(false))
266       .filter(deleted.eq(false))
267       .load::<CommunityView>(self.conn)
268   }
269 }
270
271 impl CommunityView {
272   pub fn read(
273     conn: &PgConnection,
274     from_community_id: i32,
275     from_user_id: Option<i32>,
276   ) -> Result<Self, Error> {
277     use super::community_view::community_fast_view::dsl::*;
278
279     let mut query = community_fast_view.into_boxed();
280
281     query = query.filter(id.eq(from_community_id));
282
283     // The view lets you pass a null user_id, if you're not logged in
284     if let Some(from_user_id) = from_user_id {
285       query = query.filter(user_id.eq(from_user_id));
286     } else {
287       query = query.filter(user_id.is_null());
288     };
289
290     query.first::<Self>(conn)
291   }
292 }
293
294 #[derive(
295   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
296 )]
297 #[table_name = "community_moderator_view"]
298 pub struct CommunityModeratorView {
299   pub id: i32,
300   pub community_id: i32,
301   pub user_id: i32,
302   pub published: chrono::NaiveDateTime,
303   pub user_actor_id: String,
304   pub user_local: bool,
305   pub user_name: String,
306   pub user_preferred_username: Option<String>,
307   pub avatar: Option<String>,
308   pub community_actor_id: String,
309   pub community_local: bool,
310   pub community_name: String,
311   pub community_icon: Option<String>,
312 }
313
314 impl CommunityModeratorView {
315   pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result<Vec<Self>, Error> {
316     use super::community_view::community_moderator_view::dsl::*;
317     community_moderator_view
318       .filter(community_id.eq(for_community_id))
319       .order_by(published)
320       .load::<Self>(conn)
321   }
322
323   pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result<Vec<Self>, Error> {
324     use super::community_view::community_moderator_view::dsl::*;
325     community_moderator_view
326       .filter(user_id.eq(for_user_id))
327       .order_by(published)
328       .load::<Self>(conn)
329   }
330 }
331
332 #[derive(
333   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
334 )]
335 #[table_name = "community_follower_view"]
336 pub struct CommunityFollowerView {
337   pub id: i32,
338   pub community_id: i32,
339   pub user_id: i32,
340   pub published: chrono::NaiveDateTime,
341   pub user_actor_id: String,
342   pub user_local: bool,
343   pub user_name: String,
344   pub user_preferred_username: Option<String>,
345   pub avatar: Option<String>,
346   pub community_actor_id: String,
347   pub community_local: bool,
348   pub community_name: String,
349   pub community_icon: Option<String>,
350 }
351
352 impl CommunityFollowerView {
353   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
354     use super::community_view::community_follower_view::dsl::*;
355     community_follower_view
356       .filter(community_id.eq(from_community_id))
357       .load::<Self>(conn)
358   }
359
360   pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
361     use super::community_view::community_follower_view::dsl::*;
362     community_follower_view
363       .filter(user_id.eq(from_user_id))
364       .load::<Self>(conn)
365   }
366 }
367
368 #[derive(
369   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
370 )]
371 #[table_name = "community_user_ban_view"]
372 pub struct CommunityUserBanView {
373   pub id: i32,
374   pub community_id: i32,
375   pub user_id: i32,
376   pub published: chrono::NaiveDateTime,
377   pub user_actor_id: String,
378   pub user_local: bool,
379   pub user_name: String,
380   pub user_preferred_username: Option<String>,
381   pub avatar: Option<String>,
382   pub community_actor_id: String,
383   pub community_local: bool,
384   pub community_name: String,
385   pub community_icon: Option<String>,
386 }
387
388 impl CommunityUserBanView {
389   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
390     use super::community_view::community_user_ban_view::dsl::*;
391     community_user_ban_view
392       .filter(community_id.eq(from_community_id))
393       .load::<Self>(conn)
394   }
395
396   pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
397     use super::community_view::community_user_ban_view::dsl::*;
398     community_user_ban_view
399       .filter(user_id.eq(from_user_id))
400       .load::<Self>(conn)
401   }
402
403   pub fn get(
404     conn: &PgConnection,
405     from_user_id: i32,
406     from_community_id: i32,
407   ) -> Result<Self, Error> {
408     use super::community_view::community_user_ban_view::dsl::*;
409     community_user_ban_view
410       .filter(user_id.eq(from_user_id))
411       .filter(community_id.eq(from_community_id))
412       .first::<Self>(conn)
413   }
414 }