1 use super::post_view::post_fast_view::BoxedQuery;
2 use crate::{fuzzy_search, limit_and_offset, ListingType, MaybeOptional, SortType};
3 use diesel::{dsl::*, pg::Pg, result::Error, *};
4 use serde::{Deserialize, Serialize};
6 // The faked schema since diesel doesn't do views
11 url -> Nullable<Text>,
12 body -> Nullable<Text>,
17 published -> Timestamp,
18 updated -> Nullable<Timestamp>,
22 embed_title -> Nullable<Text>,
23 embed_description -> Nullable<Text>,
24 embed_html -> Nullable<Text>,
25 thumbnail_url -> Nullable<Text>,
28 creator_actor_id -> Text,
29 creator_local -> Bool,
30 creator_name -> Varchar,
31 creator_published -> Timestamp,
32 creator_avatar -> Nullable<Text>,
34 banned_from_community -> Bool,
35 community_actor_id -> Text,
36 community_local -> Bool,
37 community_name -> Varchar,
38 community_removed -> Bool,
39 community_deleted -> Bool,
40 community_nsfw -> Bool,
41 number_of_comments -> BigInt,
46 newest_activity_time -> Timestamp,
47 user_id -> Nullable<Int4>,
48 my_vote -> Nullable<Int4>,
49 subscribed -> Nullable<Bool>,
50 read -> Nullable<Bool>,
51 saved -> Nullable<Bool>,
59 url -> Nullable<Text>,
60 body -> Nullable<Text>,
65 published -> Timestamp,
66 updated -> Nullable<Timestamp>,
70 embed_title -> Nullable<Text>,
71 embed_description -> Nullable<Text>,
72 embed_html -> Nullable<Text>,
73 thumbnail_url -> Nullable<Text>,
76 creator_actor_id -> Text,
77 creator_local -> Bool,
78 creator_name -> Varchar,
79 creator_published -> Timestamp,
80 creator_avatar -> Nullable<Text>,
82 banned_from_community -> Bool,
83 community_actor_id -> Text,
84 community_local -> Bool,
85 community_name -> Varchar,
86 community_removed -> Bool,
87 community_deleted -> Bool,
88 community_nsfw -> Bool,
89 number_of_comments -> BigInt,
94 newest_activity_time -> Timestamp,
95 user_id -> Nullable<Int4>,
96 my_vote -> Nullable<Int4>,
97 subscribed -> Nullable<Bool>,
98 read -> Nullable<Bool>,
99 saved -> Nullable<Bool>,
104 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
106 #[table_name = "post_fast_view"]
107 pub struct PostView {
110 pub url: Option<String>,
111 pub body: Option<String>,
113 pub community_id: i32,
116 pub published: chrono::NaiveDateTime,
117 pub updated: Option<chrono::NaiveDateTime>,
121 pub embed_title: Option<String>,
122 pub embed_description: Option<String>,
123 pub embed_html: Option<String>,
124 pub thumbnail_url: Option<String>,
127 pub creator_actor_id: String,
128 pub creator_local: bool,
129 pub creator_name: String,
130 pub creator_published: chrono::NaiveDateTime,
131 pub creator_avatar: Option<String>,
133 pub banned_from_community: bool,
134 pub community_actor_id: String,
135 pub community_local: bool,
136 pub community_name: String,
137 pub community_removed: bool,
138 pub community_deleted: bool,
139 pub community_nsfw: bool,
140 pub number_of_comments: i64,
145 pub newest_activity_time: chrono::NaiveDateTime,
146 pub user_id: Option<i32>,
147 pub my_vote: Option<i32>,
148 pub subscribed: Option<bool>,
149 pub read: Option<bool>,
150 pub saved: Option<bool>,
153 pub struct PostQueryBuilder<'a> {
154 conn: &'a PgConnection,
155 query: BoxedQuery<'a, Pg>,
156 listing_type: ListingType,
158 my_user_id: Option<i32>,
159 for_creator_id: Option<i32>,
160 for_community_id: Option<i32>,
161 for_community_name: Option<String>,
162 search_term: Option<String>,
163 url_search: Option<String>,
171 impl<'a> PostQueryBuilder<'a> {
172 pub fn create(conn: &'a PgConnection) -> Self {
173 use super::post_view::post_fast_view::dsl::*;
175 let query = post_fast_view.into_boxed();
180 listing_type: ListingType::All,
181 sort: &SortType::Hot,
183 for_creator_id: None,
184 for_community_id: None,
185 for_community_name: None,
196 pub fn listing_type(mut self, listing_type: ListingType) -> Self {
197 self.listing_type = listing_type;
201 pub fn sort(mut self, sort: &'a SortType) -> Self {
206 pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
207 self.for_community_id = for_community_id.get_optional();
211 pub fn for_community_name<T: MaybeOptional<String>>(mut self, for_community_name: T) -> Self {
212 self.for_community_name = for_community_name.get_optional();
216 pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
217 self.for_creator_id = for_creator_id.get_optional();
221 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
222 self.search_term = search_term.get_optional();
226 pub fn url_search<T: MaybeOptional<String>>(mut self, url_search: T) -> Self {
227 self.url_search = url_search.get_optional();
231 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
232 self.my_user_id = my_user_id.get_optional();
236 pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
237 self.show_nsfw = show_nsfw;
241 pub fn saved_only(mut self, saved_only: bool) -> Self {
242 self.saved_only = saved_only;
246 pub fn unread_only(mut self, unread_only: bool) -> Self {
247 self.unread_only = unread_only;
251 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
252 self.page = page.get_optional();
256 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
257 self.limit = limit.get_optional();
261 pub fn list(self) -> Result<Vec<PostView>, Error> {
262 use super::post_view::post_fast_view::dsl::*;
264 let mut query = self.query;
266 if let ListingType::Subscribed = self.listing_type {
267 query = query.filter(subscribed.eq(true));
270 if let Some(for_community_id) = self.for_community_id {
271 query = query.filter(community_id.eq(for_community_id));
272 query = query.then_order_by(stickied.desc());
275 if let Some(for_community_name) = self.for_community_name {
276 query = query.filter(community_name.eq(for_community_name));
277 query = query.then_order_by(stickied.desc());
280 if let Some(url_search) = self.url_search {
281 query = query.filter(url.eq(url_search));
284 if let Some(search_term) = self.search_term {
285 let searcher = fuzzy_search(&search_term);
287 .filter(name.ilike(searcher.to_owned()))
288 .or_filter(body.ilike(searcher));
291 query = match self.sort {
292 SortType::Hot => query
293 .then_order_by(hot_rank.desc())
294 .then_order_by(published.desc()),
295 SortType::New => query.then_order_by(published.desc()),
296 SortType::TopAll => query.then_order_by(score.desc()),
297 SortType::TopYear => query
298 .filter(published.gt(now - 1.years()))
299 .then_order_by(score.desc()),
300 SortType::TopMonth => query
301 .filter(published.gt(now - 1.months()))
302 .then_order_by(score.desc()),
303 SortType::TopWeek => query
304 .filter(published.gt(now - 1.weeks()))
305 .then_order_by(score.desc()),
306 SortType::TopDay => query
307 .filter(published.gt(now - 1.days()))
308 .then_order_by(score.desc()),
311 // The view lets you pass a null user_id, if you're not logged in
312 query = if let Some(my_user_id) = self.my_user_id {
313 query.filter(user_id.eq(my_user_id))
315 query.filter(user_id.is_null())
318 // If its for a specific user, show the removed / deleted
319 if let Some(for_creator_id) = self.for_creator_id {
320 query = query.filter(creator_id.eq(for_creator_id));
323 .filter(removed.eq(false))
324 .filter(deleted.eq(false))
325 .filter(community_removed.eq(false))
326 .filter(community_deleted.eq(false));
331 .filter(nsfw.eq(false))
332 .filter(community_nsfw.eq(false));
335 // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
337 query = query.filter(saved.eq(true));
340 if self.unread_only {
341 query = query.filter(read.eq(false));
344 let (limit, offset) = limit_and_offset(self.page, self.limit);
348 .filter(removed.eq(false))
349 .filter(deleted.eq(false))
350 .filter(community_removed.eq(false))
351 .filter(community_deleted.eq(false));
353 query.load::<PostView>(self.conn)
361 my_user_id: Option<i32>,
362 ) -> Result<Self, Error> {
363 use super::post_view::post_fast_view::dsl::*;
364 use diesel::prelude::*;
366 let mut query = post_fast_view.into_boxed();
368 query = query.filter(id.eq(from_post_id));
370 if let Some(my_user_id) = my_user_id {
371 query = query.filter(user_id.eq(my_user_id));
373 query = query.filter(user_id.is_null());
376 query.first::<Self>(conn)
386 tests::establish_unpooled_connection,
395 let conn = establish_unpooled_connection();
397 let user_name = "tegan".to_string();
398 let community_name = "test_community_3".to_string();
399 let post_name = "test post 3".to_string();
401 let new_user = UserForm {
402 name: user_name.to_owned(),
403 preferred_username: None,
404 password_encrypted: "nope".into(),
406 matrix_user_id: None,
412 theme: "darkly".into(),
413 default_sort_type: SortType::Hot as i16,
414 default_listing_type: ListingType::Subscribed as i16,
415 lang: "browser".into(),
417 send_notifications_to_email: false,
418 actor_id: "changeme_8282738268".into(),
423 last_refreshed_at: None,
426 let inserted_user = User_::create(&conn, &new_user).unwrap();
428 let new_community = CommunityForm {
429 name: community_name.to_owned(),
430 title: "nada".to_owned(),
432 creator_id: inserted_user.id,
438 actor_id: "changeme_2763".into(),
442 last_refreshed_at: None,
446 let inserted_community = Community::create(&conn, &new_community).unwrap();
448 let new_post = PostForm {
449 name: post_name.to_owned(),
452 creator_id: inserted_user.id,
453 community_id: inserted_community.id,
461 embed_description: None,
464 ap_id: "http://fake.com".into(),
469 let inserted_post = Post::create(&conn, &new_post).unwrap();
471 let post_like_form = PostLikeForm {
472 post_id: inserted_post.id,
473 user_id: inserted_user.id,
477 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
479 let expected_post_like = PostLike {
480 id: inserted_post_like.id,
481 post_id: inserted_post.id,
482 user_id: inserted_user.id,
483 published: inserted_post_like.published,
487 let post_like_form = PostLikeForm {
488 post_id: inserted_post.id,
489 user_id: inserted_user.id,
493 let read_post_listings_with_user = PostQueryBuilder::create(&conn)
494 .listing_type(ListingType::Community)
495 .sort(&SortType::New)
496 .for_community_id(inserted_community.id)
497 .my_user_id(inserted_user.id)
501 let read_post_listings_no_user = PostQueryBuilder::create(&conn)
502 .listing_type(ListingType::Community)
503 .sort(&SortType::New)
504 .for_community_id(inserted_community.id)
508 let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
509 let read_post_listing_with_user =
510 PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
512 // the non user version
513 let expected_post_listing_no_user = PostView {
516 id: inserted_post.id,
517 name: post_name.to_owned(),
520 creator_id: inserted_user.id,
521 creator_name: user_name.to_owned(),
522 creator_published: inserted_user.published,
523 creator_avatar: None,
525 banned_from_community: false,
526 community_id: inserted_community.id,
531 community_name: community_name.to_owned(),
532 community_removed: false,
533 community_deleted: false,
534 community_nsfw: false,
535 number_of_comments: 0,
539 hot_rank: read_post_listing_no_user.hot_rank,
540 published: inserted_post.published,
541 newest_activity_time: inserted_post.published,
548 embed_description: None,
551 ap_id: "http://fake.com".to_string(),
553 creator_actor_id: inserted_user.actor_id.to_owned(),
555 community_actor_id: inserted_community.actor_id.to_owned(),
556 community_local: true,
559 let expected_post_listing_with_user = PostView {
560 user_id: Some(inserted_user.id),
562 id: inserted_post.id,
570 creator_id: inserted_user.id,
571 creator_name: user_name,
572 creator_published: inserted_user.published,
573 creator_avatar: None,
575 banned_from_community: false,
576 community_id: inserted_community.id,
578 community_removed: false,
579 community_deleted: false,
580 community_nsfw: false,
581 number_of_comments: 0,
585 hot_rank: read_post_listing_with_user.hot_rank,
586 published: inserted_post.published,
587 newest_activity_time: inserted_post.published,
589 subscribed: Some(false),
594 embed_description: None,
597 ap_id: "http://fake.com".to_string(),
599 creator_actor_id: inserted_user.actor_id.to_owned(),
601 community_actor_id: inserted_community.actor_id.to_owned(),
602 community_local: true,
605 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
606 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
607 Community::delete(&conn, inserted_community.id).unwrap();
608 User_::delete(&conn, inserted_user.id).unwrap();
612 expected_post_listing_with_user,
613 read_post_listings_with_user[0]
615 assert_eq!(expected_post_listing_with_user, read_post_listing_with_user);
616 assert_eq!(1, read_post_listings_with_user.len());
619 assert_eq!(expected_post_listing_no_user, read_post_listings_no_user[0]);
620 assert_eq!(expected_post_listing_no_user, read_post_listing_no_user);
621 assert_eq!(1, read_post_listings_no_user.len());
623 // assert_eq!(expected_post, inserted_post);
624 // assert_eq!(expected_post, updated_post);
625 assert_eq!(expected_post_like, inserted_post_like);
626 assert_eq!(1, like_removed);
627 assert_eq!(1, num_deleted);