1 use super::post_view::post_mview::BoxedQuery;
5 // The faked schema since diesel doesn't do views
10 url -> Nullable<Text>,
11 body -> Nullable<Text>,
16 published -> Timestamp,
17 updated -> Nullable<Timestamp>,
21 banned_from_community -> Bool,
23 creator_name -> Varchar,
24 creator_avatar -> Nullable<Text>,
25 community_name -> Varchar,
26 community_removed -> Bool,
27 community_deleted -> Bool,
28 community_nsfw -> Bool,
29 number_of_comments -> BigInt,
34 newest_activity_time -> Timestamp,
35 user_id -> Nullable<Int4>,
36 my_vote -> Nullable<Int4>,
37 subscribed -> Nullable<Bool>,
38 read -> Nullable<Bool>,
39 saved -> Nullable<Bool>,
44 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
46 #[table_name = "post_view"]
50 pub url: Option<String>,
51 pub body: Option<String>,
53 pub community_id: i32,
56 pub published: chrono::NaiveDateTime,
57 pub updated: Option<chrono::NaiveDateTime>,
61 pub banned_from_community: bool,
63 pub creator_name: String,
64 pub creator_avatar: Option<String>,
65 pub community_name: String,
66 pub community_removed: bool,
67 pub community_deleted: bool,
68 pub community_nsfw: bool,
69 pub number_of_comments: i64,
74 pub newest_activity_time: chrono::NaiveDateTime,
75 pub user_id: Option<i32>,
76 pub my_vote: Option<i32>,
77 pub subscribed: Option<bool>,
78 pub read: Option<bool>,
79 pub saved: Option<bool>,
82 // The faked schema since diesel doesn't do views
87 url -> Nullable<Text>,
88 body -> Nullable<Text>,
93 published -> Timestamp,
94 updated -> Nullable<Timestamp>,
98 banned_from_community -> Bool,
100 creator_name -> Varchar,
101 creator_avatar -> Nullable<Text>,
102 community_name -> Varchar,
103 community_removed -> Bool,
104 community_deleted -> Bool,
105 community_nsfw -> Bool,
106 number_of_comments -> BigInt,
111 newest_activity_time -> Timestamp,
112 user_id -> Nullable<Int4>,
113 my_vote -> Nullable<Int4>,
114 subscribed -> Nullable<Bool>,
115 read -> Nullable<Bool>,
116 saved -> Nullable<Bool>,
120 pub struct PostQueryBuilder<'a> {
121 conn: &'a PgConnection,
122 query: BoxedQuery<'a, Pg>,
123 listing_type: ListingType,
125 my_user_id: Option<i32>,
126 for_creator_id: Option<i32>,
127 for_community_id: Option<i32>,
128 search_term: Option<String>,
129 url_search: Option<String>,
137 impl<'a> PostQueryBuilder<'a> {
138 pub fn create(conn: &'a PgConnection) -> Self {
139 use super::post_view::post_mview::dsl::*;
141 let query = post_mview.into_boxed();
146 listing_type: ListingType::All,
147 sort: &SortType::Hot,
149 for_creator_id: None,
150 for_community_id: None,
161 pub fn listing_type(mut self, listing_type: ListingType) -> Self {
162 self.listing_type = listing_type;
166 pub fn sort(mut self, sort: &'a SortType) -> Self {
171 pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
172 self.for_community_id = for_community_id.get_optional();
176 pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
177 self.for_creator_id = for_creator_id.get_optional();
181 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
182 self.search_term = search_term.get_optional();
186 pub fn url_search<T: MaybeOptional<String>>(mut self, url_search: T) -> Self {
187 self.url_search = url_search.get_optional();
191 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
192 self.my_user_id = my_user_id.get_optional();
196 pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
197 self.show_nsfw = show_nsfw;
201 pub fn saved_only(mut self, saved_only: bool) -> Self {
202 self.saved_only = saved_only;
206 pub fn unread_only(mut self, unread_only: bool) -> Self {
207 self.unread_only = unread_only;
211 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
212 self.page = page.get_optional();
216 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
217 self.limit = limit.get_optional();
221 pub fn list(self) -> Result<Vec<PostView>, Error> {
222 use super::post_view::post_mview::dsl::*;
224 let mut query = self.query;
226 if let ListingType::Subscribed = self.listing_type {
227 query = query.filter(subscribed.eq(true));
230 if let Some(for_community_id) = self.for_community_id {
231 query = query.filter(community_id.eq(for_community_id));
232 query = query.then_order_by(stickied.desc());
235 if let Some(url_search) = self.url_search {
236 query = query.filter(url.eq(url_search));
239 if let Some(search_term) = self.search_term {
240 let searcher = fuzzy_search(&search_term);
242 .filter(name.ilike(searcher.to_owned()))
243 .or_filter(body.ilike(searcher));
246 query = match self.sort {
247 SortType::Hot => query
248 .then_order_by(hot_rank.desc())
249 .then_order_by(published.desc()),
250 SortType::New => query.then_order_by(published.desc()),
251 SortType::TopAll => query.then_order_by(score.desc()),
252 SortType::TopYear => query
253 .filter(published.gt(now - 1.years()))
254 .then_order_by(score.desc()),
255 SortType::TopMonth => query
256 .filter(published.gt(now - 1.months()))
257 .then_order_by(score.desc()),
258 SortType::TopWeek => query
259 .filter(published.gt(now - 1.weeks()))
260 .then_order_by(score.desc()),
261 SortType::TopDay => query
262 .filter(published.gt(now - 1.days()))
263 .then_order_by(score.desc()),
266 // The view lets you pass a null user_id, if you're not logged in
267 query = if let Some(my_user_id) = self.my_user_id {
268 query.filter(user_id.eq(my_user_id))
270 query.filter(user_id.is_null())
273 // If its for a specific user, show the removed / deleted
274 if let Some(for_creator_id) = self.for_creator_id {
275 query = query.filter(creator_id.eq(for_creator_id));
278 .filter(removed.eq(false))
279 .filter(deleted.eq(false))
280 .filter(community_removed.eq(false))
281 .filter(community_deleted.eq(false));
286 .filter(nsfw.eq(false))
287 .filter(community_nsfw.eq(false));
290 // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
292 query = query.filter(saved.eq(true));
295 if self.unread_only {
296 query = query.filter(read.eq(false));
299 let (limit, offset) = limit_and_offset(self.page, self.limit);
303 .filter(removed.eq(false))
304 .filter(deleted.eq(false))
305 .filter(community_removed.eq(false))
306 .filter(community_deleted.eq(false));
308 query.load::<PostView>(self.conn)
316 my_user_id: Option<i32>,
317 ) -> Result<Self, Error> {
318 use super::post_view::post_mview::dsl::*;
319 use diesel::prelude::*;
321 let mut query = post_mview.into_boxed();
323 query = query.filter(id.eq(from_post_id));
325 if let Some(my_user_id) = my_user_id {
326 query = query.filter(user_id.eq(my_user_id));
328 query = query.filter(user_id.is_null());
331 query.first::<Self>(conn)
337 use super::super::community::*;
338 use super::super::post::*;
339 use super::super::user::*;
343 let conn = establish_unpooled_connection();
345 let user_name = "tegan".to_string();
346 let community_name = "test_community_3".to_string();
347 let post_name = "test post 3".to_string();
349 let new_user = UserForm {
350 name: user_name.to_owned(),
351 fedi_name: "rrf".into(),
352 preferred_username: None,
353 password_encrypted: "nope".into(),
355 matrix_user_id: None,
361 theme: "darkly".into(),
362 default_sort_type: SortType::Hot as i16,
363 default_listing_type: ListingType::Subscribed as i16,
364 lang: "browser".into(),
366 send_notifications_to_email: false,
369 let inserted_user = User_::create(&conn, &new_user).unwrap();
371 let new_community = CommunityForm {
372 name: community_name.to_owned(),
373 title: "nada".to_owned(),
375 creator_id: inserted_user.id,
383 let inserted_community = Community::create(&conn, &new_community).unwrap();
385 let new_post = PostForm {
386 name: post_name.to_owned(),
389 creator_id: inserted_user.id,
390 community_id: inserted_community.id,
399 let inserted_post = Post::create(&conn, &new_post).unwrap();
401 let post_like_form = PostLikeForm {
402 post_id: inserted_post.id,
403 user_id: inserted_user.id,
407 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
409 let expected_post_like = PostLike {
410 id: inserted_post_like.id,
411 post_id: inserted_post.id,
412 user_id: inserted_user.id,
413 published: inserted_post_like.published,
417 let post_like_form = PostLikeForm {
418 post_id: inserted_post.id,
419 user_id: inserted_user.id,
423 // the non user version
424 let expected_post_listing_no_user = PostView {
427 id: inserted_post.id,
428 name: post_name.to_owned(),
431 creator_id: inserted_user.id,
432 creator_name: user_name.to_owned(),
433 creator_avatar: None,
435 banned_from_community: false,
436 community_id: inserted_community.id,
441 community_name: community_name.to_owned(),
442 community_removed: false,
443 community_deleted: false,
444 community_nsfw: false,
445 number_of_comments: 0,
450 published: inserted_post.published,
451 newest_activity_time: inserted_post.published,
459 let expected_post_listing_with_user = PostView {
460 user_id: Some(inserted_user.id),
462 id: inserted_post.id,
470 creator_id: inserted_user.id,
471 creator_name: user_name,
472 creator_avatar: None,
474 banned_from_community: false,
475 community_id: inserted_community.id,
477 community_removed: false,
478 community_deleted: false,
479 community_nsfw: false,
480 number_of_comments: 0,
485 published: inserted_post.published,
486 newest_activity_time: inserted_post.published,
494 let read_post_listings_with_user = PostQueryBuilder::create(&conn)
495 .listing_type(ListingType::Community)
496 .sort(&SortType::New)
497 .for_community_id(inserted_community.id)
498 .my_user_id(inserted_user.id)
502 let read_post_listings_no_user = PostQueryBuilder::create(&conn)
503 .listing_type(ListingType::Community)
504 .sort(&SortType::New)
505 .for_community_id(inserted_community.id)
509 let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
510 let read_post_listing_with_user =
511 PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
513 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
514 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
515 Community::delete(&conn, inserted_community.id).unwrap();
516 User_::delete(&conn, inserted_user.id).unwrap();
520 expected_post_listing_with_user,
521 read_post_listings_with_user[0]
523 assert_eq!(expected_post_listing_with_user, read_post_listing_with_user);
524 assert_eq!(1, read_post_listings_with_user.len());
527 assert_eq!(expected_post_listing_no_user, read_post_listings_no_user[0]);
528 assert_eq!(expected_post_listing_no_user, read_post_listing_no_user);
529 assert_eq!(1, read_post_listings_no_user.len());
531 // assert_eq!(expected_post, inserted_post);
532 // assert_eq!(expected_post, updated_post);
533 assert_eq!(expected_post_like, inserted_post_like);
534 assert_eq!(1, like_removed);
535 assert_eq!(1, num_deleted);