]> Untitled Git - lemmy.git/blob - server/src/db/post_view.rs
Adding emoji support
[lemmy.git] / server / src / db / post_view.rs
1 use super::*;
2
3 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
4 pub enum PostListingType {
5   All, Subscribed, Community
6 }
7
8 // The faked schema since diesel doesn't do views
9 table! {
10   post_view (id) {
11     id -> Int4,
12     name -> Varchar,
13     url -> Nullable<Text>,
14     body -> Nullable<Text>,
15     creator_id -> Int4,
16     community_id -> Int4,
17     removed -> Bool,
18     locked -> Bool,
19     published -> Timestamp,
20     updated -> Nullable<Timestamp>,
21     deleted -> Bool,
22     creator_name -> Varchar,
23     community_name -> Varchar,
24     community_removed -> Bool,
25     community_deleted -> Bool,
26     number_of_comments -> BigInt,
27     score -> BigInt,
28     upvotes -> BigInt,
29     downvotes -> BigInt,
30     hot_rank -> Int4,
31     user_id -> Nullable<Int4>,
32     my_vote -> Nullable<Int4>,
33     subscribed -> Nullable<Bool>,
34     read -> Nullable<Bool>,
35     saved -> Nullable<Bool>,
36   }
37 }
38
39
40 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
41 #[table_name="post_view"]
42 pub struct PostView {
43   pub id: i32,
44   pub name: String,
45   pub url: Option<String>,
46   pub body: Option<String>,
47   pub creator_id: i32,
48   pub community_id: i32,
49   pub removed: bool,
50   pub locked: bool,
51   pub published: chrono::NaiveDateTime,
52   pub updated: Option<chrono::NaiveDateTime>,
53   pub deleted: bool,
54   pub creator_name: String,
55   pub community_name: String,
56   pub community_removed: bool,
57   pub community_deleted: bool,
58   pub number_of_comments: i64,
59   pub score: i64,
60   pub upvotes: i64,
61   pub downvotes: i64,
62   pub hot_rank: i32,
63   pub user_id: Option<i32>,
64   pub my_vote: Option<i32>,
65   pub subscribed: Option<bool>,
66   pub read: Option<bool>,
67   pub saved: Option<bool>,
68 }
69
70 impl PostView {
71   pub fn list(conn: &PgConnection, 
72               type_: PostListingType, 
73               sort: &SortType, 
74               for_community_id: Option<i32>, 
75               for_creator_id: Option<i32>, 
76               search_term: Option<String>,
77               my_user_id: Option<i32>, 
78               saved_only: bool,
79               unread_only: bool,
80               page: Option<i64>,
81               limit: Option<i64>,
82               ) -> Result<Vec<Self>, Error> {
83     use super::post_view::post_view::dsl::*;
84
85     let (limit, offset) = limit_and_offset(page, limit);
86
87     let mut query = post_view.into_boxed();
88
89     if let Some(for_community_id) = for_community_id {
90       query = query.filter(community_id.eq(for_community_id));
91     };
92
93     if let Some(for_creator_id) = for_creator_id {
94       query = query.filter(creator_id.eq(for_creator_id));
95     };
96
97     if let Some(search_term) = search_term {
98       query = query.filter(name.ilike(fuzzy_search(&search_term)));
99     };
100
101     // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
102     if saved_only {
103       query = query.filter(saved.eq(true));
104     };
105
106     if unread_only {
107       query = query.filter(read.eq(false));
108     };
109
110     match type_ {
111       PostListingType::Subscribed  => {
112         query = query.filter(subscribed.eq(true));
113       },
114       _ => {}
115     };
116
117     // The view lets you pass a null user_id, if you're not logged in
118     if let Some(my_user_id) = my_user_id {
119       query = query.filter(user_id.eq(my_user_id));
120     } else {
121       query = query.filter(user_id.is_null());
122     }
123
124     query = match sort {
125       SortType::Hot => query.order_by(hot_rank.desc()),
126       SortType::New => query.order_by(published.desc()),
127       SortType::TopAll => query.order_by(score.desc()),
128       SortType::TopYear => query
129         .filter(published.gt(now - 1.years()))
130         .order_by(score.desc()),
131         SortType::TopMonth => query
132           .filter(published.gt(now - 1.months()))
133           .order_by(score.desc()),
134           SortType::TopWeek => query
135             .filter(published.gt(now - 1.weeks()))
136             .order_by(score.desc()),
137             SortType::TopDay => query
138               .filter(published.gt(now - 1.days()))
139               .order_by(score.desc())
140     };
141
142     query = query
143       .limit(limit)
144       .offset(offset)
145       .filter(removed.eq(false))
146       .filter(deleted.eq(false))
147       .filter(community_removed.eq(false))
148       .filter(community_deleted.eq(false));
149
150     query.load::<Self>(conn) 
151   }
152
153
154   pub fn read(conn: &PgConnection, from_post_id: i32, my_user_id: Option<i32>) -> Result<Self, Error> {
155
156     use super::post_view::post_view::dsl::*;
157     use diesel::prelude::*;
158
159     let mut query = post_view.into_boxed();
160
161     query = query.filter(id.eq(from_post_id));
162
163     if let Some(my_user_id) = my_user_id {
164       query = query.filter(user_id.eq(my_user_id));
165     } else {
166       query = query.filter(user_id.is_null());
167     };
168
169     query.first::<Self>(conn)
170   }
171 }
172
173
174
175 #[cfg(test)]
176 mod tests {
177   use super::*;
178   use super::super::community::*;
179   use super::super::user::*;
180   use super::super::post::*;
181   #[test]
182   fn test_crud() {
183     let conn = establish_connection();
184
185     let user_name = "tegan".to_string();
186     let community_name = "test_community_3".to_string();
187     let post_name = "test post 3".to_string();
188
189     let new_user = UserForm {
190       name: user_name.to_owned(),
191       fedi_name: "rrf".into(),
192       preferred_username: None,
193       password_encrypted: "nope".into(),
194       email: None,
195       updated: None,
196       admin: false,
197       banned: false,
198     };
199
200     let inserted_user = User_::create(&conn, &new_user).unwrap();
201
202     let new_community = CommunityForm {
203       name: community_name.to_owned(),
204       title: "nada".to_owned(),
205       description: None,
206       creator_id: inserted_user.id,
207       category_id: 1,
208       removed: None,
209       deleted: None,
210       updated: None
211     };
212
213     let inserted_community = Community::create(&conn, &new_community).unwrap();
214
215     let new_post = PostForm {
216       name: post_name.to_owned(),
217       url: None,
218       body: None,
219       creator_id: inserted_user.id,
220       community_id: inserted_community.id,
221       removed: None,
222       deleted: None,
223       locked: None,
224       updated: None
225     };
226
227     let inserted_post = Post::create(&conn, &new_post).unwrap();
228
229     let post_like_form = PostLikeForm {
230       post_id: inserted_post.id,
231       user_id: inserted_user.id,
232       score: 1
233     };
234
235     let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
236
237     let expected_post_like = PostLike {
238       id: inserted_post_like.id,
239       post_id: inserted_post.id,
240       user_id: inserted_user.id,
241       published: inserted_post_like.published,
242       score: 1
243     };
244
245     let post_like_form = PostLikeForm {
246       post_id: inserted_post.id,
247       user_id: inserted_user.id,
248       score: 1
249     };
250
251     // the non user version
252     let expected_post_listing_no_user = PostView {
253       user_id: None,
254       my_vote: None,
255       id: inserted_post.id,
256       name: post_name.to_owned(),
257       url: None,
258       body: None,
259       creator_id: inserted_user.id,
260       creator_name: user_name.to_owned(),
261       community_id: inserted_community.id,
262       removed: false,
263       deleted: false,
264       locked: false,
265       community_name: community_name.to_owned(),
266       community_removed: false,
267       community_deleted: false,
268       number_of_comments: 0,
269       score: 1,
270       upvotes: 1,
271       downvotes: 0,
272       hot_rank: 1728,
273       published: inserted_post.published,
274       updated: None,
275       subscribed: None,
276       read: None,
277       saved: None,
278     };
279
280     let expected_post_listing_with_user = PostView {
281       user_id: Some(inserted_user.id),
282       my_vote: Some(1),
283       id: inserted_post.id,
284       name: post_name.to_owned(),
285       url: None,
286       body: None,
287       removed: false,
288       deleted: false,
289       locked: false,
290       creator_id: inserted_user.id,
291       creator_name: user_name.to_owned(),
292       community_id: inserted_community.id,
293       community_name: community_name.to_owned(),
294       community_removed: false,
295       community_deleted: false,
296       number_of_comments: 0,
297       score: 1,
298       upvotes: 1,
299       downvotes: 0,
300       hot_rank: 1728,
301       published: inserted_post.published,
302       updated: None,
303       subscribed: None,
304       read: None,
305       saved: None,
306     };
307
308
309     let read_post_listings_with_user = PostView::list(&conn, 
310                                                       PostListingType::Community, 
311                                                       &SortType::New, Some(inserted_community.id), 
312                                                       None, 
313                                                       None,
314                                                       Some(inserted_user.id), 
315                                                       false, 
316                                                       false, 
317                                                       None, 
318                                                       None).unwrap();
319     let read_post_listings_no_user = PostView::list(&conn, 
320                                                     PostListingType::Community, 
321                                                     &SortType::New, 
322                                                     Some(inserted_community.id), 
323                                                     None, 
324                                                     None, 
325                                                     None,
326                                                     false, 
327                                                     false, 
328                                                     None, 
329                                                     None).unwrap();
330     let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
331     let read_post_listing_with_user = PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
332
333     let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
334     let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
335     Community::delete(&conn, inserted_community.id).unwrap();
336     User_::delete(&conn, inserted_user.id).unwrap();
337
338     // The with user
339     assert_eq!(expected_post_listing_with_user, read_post_listings_with_user[0]);
340     assert_eq!(expected_post_listing_with_user, read_post_listing_with_user);
341     assert_eq!(1, read_post_listings_with_user.len());
342
343     // Without the user
344     assert_eq!(expected_post_listing_no_user, read_post_listings_no_user[0]);
345     assert_eq!(expected_post_listing_no_user, read_post_listing_no_user);
346     assert_eq!(1, read_post_listings_no_user.len());
347
348     // assert_eq!(expected_post, inserted_post);
349     // assert_eq!(expected_post, updated_post);
350     assert_eq!(expected_post_like, inserted_post_like);
351     assert_eq!(1, like_removed);
352     assert_eq!(1, num_deleted);
353   }
354 }