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