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 embed_title -> Nullable<Text>,
22 embed_description -> Nullable<Text>,
23 embed_html -> Nullable<Text>,
24 thumbnail_url -> Nullable<Text>,
26 banned_from_community -> Bool,
27 creator_name -> Varchar,
28 creator_avatar -> Nullable<Text>,
29 community_name -> Varchar,
30 community_removed -> Bool,
31 community_deleted -> Bool,
32 community_nsfw -> Bool,
33 number_of_comments -> BigInt,
38 newest_activity_time -> Timestamp,
39 user_id -> Nullable<Int4>,
40 my_vote -> Nullable<Int4>,
41 subscribed -> Nullable<Bool>,
42 read -> Nullable<Bool>,
43 saved -> Nullable<Bool>,
51 url -> Nullable<Text>,
52 body -> Nullable<Text>,
57 published -> Timestamp,
58 updated -> Nullable<Timestamp>,
62 embed_title -> Nullable<Text>,
63 embed_description -> Nullable<Text>,
64 embed_html -> Nullable<Text>,
65 thumbnail_url -> Nullable<Text>,
67 banned_from_community -> Bool,
68 creator_name -> Varchar,
69 creator_avatar -> Nullable<Text>,
70 community_name -> Varchar,
71 community_removed -> Bool,
72 community_deleted -> Bool,
73 community_nsfw -> Bool,
74 number_of_comments -> BigInt,
79 newest_activity_time -> Timestamp,
80 user_id -> Nullable<Int4>,
81 my_vote -> Nullable<Int4>,
82 subscribed -> Nullable<Bool>,
83 read -> Nullable<Bool>,
84 saved -> Nullable<Bool>,
89 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
91 #[table_name = "post_view"]
95 pub url: Option<String>,
96 pub body: Option<String>,
98 pub community_id: i32,
101 pub published: chrono::NaiveDateTime,
102 pub updated: Option<chrono::NaiveDateTime>,
106 pub embed_title: Option<String>,
107 pub embed_description: Option<String>,
108 pub embed_html: Option<String>,
109 pub thumbnail_url: Option<String>,
111 pub banned_from_community: bool,
112 pub creator_name: String,
113 pub creator_avatar: Option<String>,
114 pub community_name: String,
115 pub community_removed: bool,
116 pub community_deleted: bool,
117 pub community_nsfw: bool,
118 pub number_of_comments: i64,
123 pub newest_activity_time: chrono::NaiveDateTime,
124 pub user_id: Option<i32>,
125 pub my_vote: Option<i32>,
126 pub subscribed: Option<bool>,
127 pub read: Option<bool>,
128 pub saved: Option<bool>,
131 pub struct PostQueryBuilder<'a> {
132 conn: &'a PgConnection,
133 query: BoxedQuery<'a, Pg>,
134 listing_type: ListingType,
136 my_user_id: Option<i32>,
137 for_creator_id: Option<i32>,
138 for_community_id: Option<i32>,
139 search_term: Option<String>,
140 url_search: Option<String>,
148 impl<'a> PostQueryBuilder<'a> {
149 pub fn create(conn: &'a PgConnection) -> Self {
150 use super::post_view::post_mview::dsl::*;
152 let query = post_mview.into_boxed();
157 listing_type: ListingType::All,
158 sort: &SortType::Hot,
160 for_creator_id: None,
161 for_community_id: None,
172 pub fn listing_type(mut self, listing_type: ListingType) -> Self {
173 self.listing_type = listing_type;
177 pub fn sort(mut self, sort: &'a SortType) -> Self {
182 pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
183 self.for_community_id = for_community_id.get_optional();
187 pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
188 self.for_creator_id = for_creator_id.get_optional();
192 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
193 self.search_term = search_term.get_optional();
197 pub fn url_search<T: MaybeOptional<String>>(mut self, url_search: T) -> Self {
198 self.url_search = url_search.get_optional();
202 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
203 self.my_user_id = my_user_id.get_optional();
207 pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
208 self.show_nsfw = show_nsfw;
212 pub fn saved_only(mut self, saved_only: bool) -> Self {
213 self.saved_only = saved_only;
217 pub fn unread_only(mut self, unread_only: bool) -> Self {
218 self.unread_only = unread_only;
222 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
223 self.page = page.get_optional();
227 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
228 self.limit = limit.get_optional();
232 pub fn list(self) -> Result<Vec<PostView>, Error> {
233 use super::post_view::post_mview::dsl::*;
235 let mut query = self.query;
237 if let ListingType::Subscribed = self.listing_type {
238 query = query.filter(subscribed.eq(true));
241 if let Some(for_community_id) = self.for_community_id {
242 query = query.filter(community_id.eq(for_community_id));
243 query = query.then_order_by(stickied.desc());
246 if let Some(url_search) = self.url_search {
247 query = query.filter(url.eq(url_search));
250 if let Some(search_term) = self.search_term {
251 let searcher = fuzzy_search(&search_term);
253 .filter(name.ilike(searcher.to_owned()))
254 .or_filter(body.ilike(searcher));
257 query = match self.sort {
258 SortType::Hot => query
259 .then_order_by(hot_rank.desc())
260 .then_order_by(published.desc()),
261 SortType::New => query.then_order_by(published.desc()),
262 SortType::TopAll => query.then_order_by(score.desc()),
263 SortType::TopYear => query
264 .filter(published.gt(now - 1.years()))
265 .then_order_by(score.desc()),
266 SortType::TopMonth => query
267 .filter(published.gt(now - 1.months()))
268 .then_order_by(score.desc()),
269 SortType::TopWeek => query
270 .filter(published.gt(now - 1.weeks()))
271 .then_order_by(score.desc()),
272 SortType::TopDay => query
273 .filter(published.gt(now - 1.days()))
274 .then_order_by(score.desc()),
277 // The view lets you pass a null user_id, if you're not logged in
278 query = if let Some(my_user_id) = self.my_user_id {
279 query.filter(user_id.eq(my_user_id))
281 query.filter(user_id.is_null())
284 // If its for a specific user, show the removed / deleted
285 if let Some(for_creator_id) = self.for_creator_id {
286 query = query.filter(creator_id.eq(for_creator_id));
289 .filter(removed.eq(false))
290 .filter(deleted.eq(false))
291 .filter(community_removed.eq(false))
292 .filter(community_deleted.eq(false));
297 .filter(nsfw.eq(false))
298 .filter(community_nsfw.eq(false));
301 // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
303 query = query.filter(saved.eq(true));
306 if self.unread_only {
307 query = query.filter(read.eq(false));
310 let (limit, offset) = limit_and_offset(self.page, self.limit);
314 .filter(removed.eq(false))
315 .filter(deleted.eq(false))
316 .filter(community_removed.eq(false))
317 .filter(community_deleted.eq(false));
319 query.load::<PostView>(self.conn)
327 my_user_id: Option<i32>,
328 ) -> Result<Self, Error> {
329 use super::post_view::post_mview::dsl::*;
330 use diesel::prelude::*;
332 let mut query = post_mview.into_boxed();
334 query = query.filter(id.eq(from_post_id));
336 if let Some(my_user_id) = my_user_id {
337 query = query.filter(user_id.eq(my_user_id));
339 query = query.filter(user_id.is_null());
342 query.first::<Self>(conn)
348 use super::super::community::*;
349 use super::super::post::*;
350 use super::super::user::*;
354 let conn = establish_unpooled_connection();
356 let user_name = "tegan".to_string();
357 let community_name = "test_community_3".to_string();
358 let post_name = "test post 3".to_string();
360 let new_user = UserForm {
361 name: user_name.to_owned(),
362 fedi_name: "rrf".into(),
363 preferred_username: None,
364 password_encrypted: "nope".into(),
366 matrix_user_id: None,
372 theme: "darkly".into(),
373 default_sort_type: SortType::Hot as i16,
374 default_listing_type: ListingType::Subscribed as i16,
375 lang: "browser".into(),
377 send_notifications_to_email: false,
380 let inserted_user = User_::create(&conn, &new_user).unwrap();
382 let new_community = CommunityForm {
383 name: community_name.to_owned(),
384 title: "nada".to_owned(),
386 creator_id: inserted_user.id,
394 let inserted_community = Community::create(&conn, &new_community).unwrap();
396 let new_post = PostForm {
397 name: post_name.to_owned(),
400 creator_id: inserted_user.id,
401 community_id: inserted_community.id,
409 embed_description: None,
414 let inserted_post = Post::create(&conn, &new_post).unwrap();
416 let post_like_form = PostLikeForm {
417 post_id: inserted_post.id,
418 user_id: inserted_user.id,
422 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
424 let expected_post_like = PostLike {
425 id: inserted_post_like.id,
426 post_id: inserted_post.id,
427 user_id: inserted_user.id,
428 published: inserted_post_like.published,
432 let post_like_form = PostLikeForm {
433 post_id: inserted_post.id,
434 user_id: inserted_user.id,
438 // the non user version
439 let expected_post_listing_no_user = PostView {
442 id: inserted_post.id,
443 name: post_name.to_owned(),
446 creator_id: inserted_user.id,
447 creator_name: user_name.to_owned(),
448 creator_avatar: None,
450 banned_from_community: false,
451 community_id: inserted_community.id,
456 community_name: community_name.to_owned(),
457 community_removed: false,
458 community_deleted: false,
459 community_nsfw: false,
460 number_of_comments: 0,
465 published: inserted_post.published,
466 newest_activity_time: inserted_post.published,
473 embed_description: None,
478 let expected_post_listing_with_user = PostView {
479 user_id: Some(inserted_user.id),
481 id: inserted_post.id,
489 creator_id: inserted_user.id,
490 creator_name: user_name,
491 creator_avatar: None,
493 banned_from_community: false,
494 community_id: inserted_community.id,
496 community_removed: false,
497 community_deleted: false,
498 community_nsfw: false,
499 number_of_comments: 0,
504 published: inserted_post.published,
505 newest_activity_time: inserted_post.published,
512 embed_description: None,
517 let read_post_listings_with_user = PostQueryBuilder::create(&conn)
518 .listing_type(ListingType::Community)
519 .sort(&SortType::New)
520 .for_community_id(inserted_community.id)
521 .my_user_id(inserted_user.id)
525 let read_post_listings_no_user = PostQueryBuilder::create(&conn)
526 .listing_type(ListingType::Community)
527 .sort(&SortType::New)
528 .for_community_id(inserted_community.id)
532 let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
533 let read_post_listing_with_user =
534 PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
536 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
537 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
538 Community::delete(&conn, inserted_community.id).unwrap();
539 User_::delete(&conn, inserted_user.id).unwrap();
543 expected_post_listing_with_user,
544 read_post_listings_with_user[0]
546 assert_eq!(expected_post_listing_with_user, read_post_listing_with_user);
547 assert_eq!(1, read_post_listings_with_user.len());
550 assert_eq!(expected_post_listing_no_user, read_post_listings_no_user[0]);
551 assert_eq!(expected_post_listing_no_user, read_post_listing_no_user);
552 assert_eq!(1, read_post_listings_no_user.len());
554 // assert_eq!(expected_post, inserted_post);
555 // assert_eq!(expected_post, updated_post);
556 assert_eq!(expected_post_like, inserted_post_like);
557 assert_eq!(1, like_removed);
558 assert_eq!(1, num_deleted);