]> Untitled Git - lemmy.git/blob - server/src/db/community_view.rs
Adding emoji support.
[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(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
77 #[table_name="community_view"]
78 pub struct CommunityView {
79   pub id: i32,
80   pub name: String,
81   pub title: String,
82   pub description: Option<String>,
83   pub category_id: i32,
84   pub creator_id: i32,
85   pub removed: bool,
86   pub published: chrono::NaiveDateTime,
87   pub updated: Option<chrono::NaiveDateTime>,
88   pub deleted: bool,
89   pub nsfw: bool,
90   pub creator_name: String,
91   pub category_name: String,
92   pub number_of_subscribers: i64,
93   pub number_of_posts: i64,
94   pub number_of_comments: i64,
95   pub hot_rank: i32,
96   pub user_id: Option<i32>,
97   pub subscribed: Option<bool>,
98 }
99
100 impl CommunityView {
101   pub fn read(conn: &PgConnection, from_community_id: i32, from_user_id: Option<i32>) -> Result<Self, Error> {
102     use super::community_view::community_view::dsl::*;
103
104     let mut query = community_view.into_boxed();
105
106     query = query.filter(id.eq(from_community_id));
107
108     // The view lets you pass a null user_id, if you're not logged in
109     if let Some(from_user_id) = from_user_id {
110       query = query.filter(user_id.eq(from_user_id));
111     } else {
112       query = query.filter(user_id.is_null());
113     };
114
115     query.first::<Self>(conn)
116   }
117
118   pub fn list(
119     conn: &PgConnection, 
120     sort: &SortType, 
121     from_user_id: Option<i32>, 
122     show_nsfw: bool,
123     search_term: Option<String>,
124     page: Option<i64>,
125     limit: Option<i64>,
126     ) -> Result<Vec<Self>, Error> {
127     use super::community_view::community_view::dsl::*;
128     let mut query = community_view.into_boxed();
129
130     let (limit, offset) = limit_and_offset(page, limit);
131
132     if let Some(search_term) = search_term {
133       query = query.filter(name.ilike(fuzzy_search(&search_term)));
134     };
135
136     // The view lets you pass a null user_id, if you're not logged in
137     match sort {
138       SortType::Hot => query = query.order_by(hot_rank.desc())
139         .then_order_by(number_of_subscribers.desc())
140         .filter(user_id.is_null()),
141         SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
142         SortType::TopAll => {
143           match from_user_id {
144             Some(from_user_id) => query = query.filter(user_id.eq(from_user_id)).order_by((subscribed.asc(), number_of_subscribers.desc())),
145             None => query = query.order_by(number_of_subscribers.desc()).filter(user_id.is_null())
146           }
147         }
148       _ => ()
149     };
150
151     if !show_nsfw {
152       query = query.filter(nsfw.eq(false));
153     };
154
155     query
156       .limit(limit)
157       .offset(offset)
158       .filter(removed.eq(false))
159       .filter(deleted.eq(false))
160       .load::<Self>(conn) 
161   }
162 }
163
164
165 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
166 #[table_name="community_moderator_view"]
167 pub struct CommunityModeratorView {
168   pub id: i32,
169   pub community_id: i32,
170   pub user_id: i32,
171   pub published: chrono::NaiveDateTime,
172   pub user_name : String,
173   pub community_name: String,
174 }
175
176 impl CommunityModeratorView {
177   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
178     use super::community_view::community_moderator_view::dsl::*;
179     community_moderator_view.filter(community_id.eq(from_community_id)).load::<Self>(conn)
180   }
181
182   pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
183     use super::community_view::community_moderator_view::dsl::*;
184     community_moderator_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
185   }
186 }
187
188 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
189 #[table_name="community_follower_view"]
190 pub struct CommunityFollowerView {
191   pub id: i32,
192   pub community_id: i32,
193   pub user_id: i32,
194   pub published: chrono::NaiveDateTime,
195   pub user_name : String,
196   pub community_name: String,
197 }
198
199 impl CommunityFollowerView {
200   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
201     use super::community_view::community_follower_view::dsl::*;
202     community_follower_view.filter(community_id.eq(from_community_id)).load::<Self>(conn)
203   }
204
205   pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
206     use super::community_view::community_follower_view::dsl::*;
207     community_follower_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
208   }
209 }
210
211
212 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
213 #[table_name="community_user_ban_view"]
214 pub struct CommunityUserBanView {
215   pub id: i32,
216   pub community_id: i32,
217   pub user_id: i32,
218   pub published: chrono::NaiveDateTime,
219   pub user_name : String,
220   pub community_name: String,
221 }
222
223 impl CommunityUserBanView {
224   pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
225     use super::community_view::community_user_ban_view::dsl::*;
226     community_user_ban_view.filter(community_id.eq(from_community_id)).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_user_ban_view::dsl::*;
231     community_user_ban_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
232   }
233
234   pub fn get(conn: &PgConnection, from_user_id: i32, from_community_id: i32) -> Result<Self, Error> {
235     use super::community_view::community_user_ban_view::dsl::*;
236     community_user_ban_view
237       .filter(user_id.eq(from_user_id))
238       .filter(community_id.eq(from_community_id))
239       .first::<Self>(conn)
240   }
241 }
242
243
244 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
245 #[table_name="site_view"]
246 pub struct SiteView {
247   pub id: i32,
248   pub name: String,
249   pub description: Option<String>,
250   pub creator_id: i32,
251   pub published: chrono::NaiveDateTime,
252   pub updated: Option<chrono::NaiveDateTime>,
253   pub creator_name: String,
254   pub number_of_users: i64,
255   pub number_of_posts: i64,
256   pub number_of_comments: i64,
257   pub number_of_communities: i64,
258 }
259
260 impl SiteView {
261   pub fn read(conn: &PgConnection) -> Result<Self, Error> {
262     use super::community_view::site_view::dsl::*;
263     site_view.first::<Self>(conn)
264   }
265 }