3 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
4 pub enum PostListingType {
5 All, Subscribed, Community
8 // The faked schema since diesel doesn't do views
13 url -> Nullable<Text>,
14 body -> Nullable<Text>,
19 published -> Timestamp,
20 updated -> Nullable<Timestamp>,
23 creator_name -> Varchar,
24 community_name -> Varchar,
25 community_removed -> Bool,
26 community_deleted -> Bool,
27 community_nsfw -> Bool,
28 number_of_comments -> BigInt,
33 user_id -> Nullable<Int4>,
34 my_vote -> Nullable<Int4>,
35 subscribed -> Nullable<Bool>,
36 read -> Nullable<Bool>,
37 saved -> Nullable<Bool>,
42 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
43 #[table_name="post_view"]
47 pub url: Option<String>,
48 pub body: Option<String>,
50 pub community_id: i32,
53 pub published: chrono::NaiveDateTime,
54 pub updated: Option<chrono::NaiveDateTime>,
57 pub creator_name: String,
58 pub community_name: String,
59 pub community_removed: bool,
60 pub community_deleted: bool,
61 pub community_nsfw: bool,
62 pub number_of_comments: i64,
67 pub user_id: Option<i32>,
68 pub my_vote: Option<i32>,
69 pub subscribed: Option<bool>,
70 pub read: Option<bool>,
71 pub saved: Option<bool>,
77 type_: PostListingType,
79 for_community_id: Option<i32>,
80 for_creator_id: Option<i32>,
81 search_term: Option<String>,
82 url_search: Option<String>,
83 my_user_id: Option<i32>,
89 ) -> Result<Vec<Self>, Error> {
90 use super::post_view::post_view::dsl::*;
92 let (limit, offset) = limit_and_offset(page, limit);
94 let mut query = post_view.into_boxed();
96 if let Some(for_community_id) = for_community_id {
97 query = query.filter(community_id.eq(for_community_id));
100 if let Some(for_creator_id) = for_creator_id {
101 query = query.filter(creator_id.eq(for_creator_id));
104 if let Some(search_term) = search_term {
105 query = query.filter(name.ilike(fuzzy_search(&search_term)));
108 if let Some(url_search) = url_search {
109 query = query.filter(url.eq(url_search));
112 // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
114 query = query.filter(saved.eq(true));
118 query = query.filter(read.eq(false));
122 PostListingType::Subscribed => {
123 query = query.filter(subscribed.eq(true));
128 // The view lets you pass a null user_id, if you're not logged in
129 if let Some(my_user_id) = my_user_id {
130 query = query.filter(user_id.eq(my_user_id));
132 query = query.filter(user_id.is_null());
137 .filter(nsfw.eq(false))
138 .filter(community_nsfw.eq(false));
142 SortType::Hot => query.order_by(hot_rank.desc())
143 .then_order_by(published.desc()),
144 SortType::New => query.order_by(published.desc()),
145 SortType::TopAll => query.order_by(score.desc()),
146 SortType::TopYear => query
147 .filter(published.gt(now - 1.years()))
148 .order_by(score.desc()),
149 SortType::TopMonth => query
150 .filter(published.gt(now - 1.months()))
151 .order_by(score.desc()),
152 SortType::TopWeek => query
153 .filter(published.gt(now - 1.weeks()))
154 .order_by(score.desc()),
155 SortType::TopDay => query
156 .filter(published.gt(now - 1.days()))
157 .order_by(score.desc())
163 .filter(removed.eq(false))
164 .filter(deleted.eq(false))
165 .filter(community_removed.eq(false))
166 .filter(community_deleted.eq(false));
168 query.load::<Self>(conn)
172 pub fn read(conn: &PgConnection, from_post_id: i32, my_user_id: Option<i32>) -> Result<Self, Error> {
174 use super::post_view::post_view::dsl::*;
175 use diesel::prelude::*;
177 let mut query = post_view.into_boxed();
179 query = query.filter(id.eq(from_post_id));
181 if let Some(my_user_id) = my_user_id {
182 query = query.filter(user_id.eq(my_user_id));
184 query = query.filter(user_id.is_null());
187 query.first::<Self>(conn)
196 use super::super::community::*;
197 use super::super::user::*;
198 use super::super::post::*;
201 let conn = establish_connection();
203 let user_name = "tegan".to_string();
204 let community_name = "test_community_3".to_string();
205 let post_name = "test post 3".to_string();
207 let new_user = UserForm {
208 name: user_name.to_owned(),
209 fedi_name: "rrf".into(),
210 preferred_username: None,
211 password_encrypted: "nope".into(),
219 let inserted_user = User_::create(&conn, &new_user).unwrap();
221 let new_community = CommunityForm {
222 name: community_name.to_owned(),
223 title: "nada".to_owned(),
225 creator_id: inserted_user.id,
233 let inserted_community = Community::create(&conn, &new_community).unwrap();
235 let new_post = PostForm {
236 name: post_name.to_owned(),
239 creator_id: inserted_user.id,
240 community_id: inserted_community.id,
248 let inserted_post = Post::create(&conn, &new_post).unwrap();
250 let post_like_form = PostLikeForm {
251 post_id: inserted_post.id,
252 user_id: inserted_user.id,
256 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
258 let expected_post_like = PostLike {
259 id: inserted_post_like.id,
260 post_id: inserted_post.id,
261 user_id: inserted_user.id,
262 published: inserted_post_like.published,
266 let post_like_form = PostLikeForm {
267 post_id: inserted_post.id,
268 user_id: inserted_user.id,
272 // the non user version
273 let expected_post_listing_no_user = PostView {
276 id: inserted_post.id,
277 name: post_name.to_owned(),
280 creator_id: inserted_user.id,
281 creator_name: user_name.to_owned(),
282 community_id: inserted_community.id,
286 community_name: community_name.to_owned(),
287 community_removed: false,
288 community_deleted: false,
289 community_nsfw: false,
290 number_of_comments: 0,
295 published: inserted_post.published,
303 let expected_post_listing_with_user = PostView {
304 user_id: Some(inserted_user.id),
306 id: inserted_post.id,
307 name: post_name.to_owned(),
313 creator_id: inserted_user.id,
314 creator_name: user_name.to_owned(),
315 community_id: inserted_community.id,
316 community_name: community_name.to_owned(),
317 community_removed: false,
318 community_deleted: false,
319 community_nsfw: false,
320 number_of_comments: 0,
325 published: inserted_post.published,
334 let read_post_listings_with_user = PostView::list(
336 PostListingType::Community,
338 Some(inserted_community.id),
342 Some(inserted_user.id),
348 let read_post_listings_no_user = PostView::list(
350 PostListingType::Community,
352 Some(inserted_community.id),
362 let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
363 let read_post_listing_with_user = PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
365 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
366 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
367 Community::delete(&conn, inserted_community.id).unwrap();
368 User_::delete(&conn, inserted_user.id).unwrap();
371 assert_eq!(expected_post_listing_with_user, read_post_listings_with_user[0]);
372 assert_eq!(expected_post_listing_with_user, read_post_listing_with_user);
373 assert_eq!(1, read_post_listings_with_user.len());
376 assert_eq!(expected_post_listing_no_user, read_post_listings_no_user[0]);
377 assert_eq!(expected_post_listing_no_user, read_post_listing_no_user);
378 assert_eq!(1, read_post_listings_no_user.len());
380 // assert_eq!(expected_post, inserted_post);
381 // assert_eq!(expected_post, updated_post);
382 assert_eq!(expected_post_like, inserted_post_like);
383 assert_eq!(1, like_removed);
384 assert_eq!(1, num_deleted);