]> Untitled Git - lemmy.git/blob - crates/db_views/src/post_view.rs
Expose pending 2 (#2282)
[lemmy.git] / crates / db_views / src / post_view.rs
1 use crate::structs::PostView;
2 use diesel::{dsl::*, pg::Pg, result::Error, *};
3 use lemmy_db_schema::{
4   aggregates::structs::PostAggregates,
5   newtypes::{CommunityId, DbUrl, PersonId, PostId},
6   schema::{
7     community,
8     community_block,
9     community_follower,
10     community_person_ban,
11     person,
12     person_block,
13     post,
14     post_aggregates,
15     post_like,
16     post_read,
17     post_saved,
18   },
19   source::{
20     community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
21     person::{Person, PersonSafe},
22     person_block::PersonBlock,
23     post::{Post, PostRead, PostSaved},
24   },
25   traits::{MaybeOptional, ToSafe, ViewToVec},
26   utils::{functions::hot_rank, fuzzy_search, limit_and_offset},
27   ListingType,
28   SortType,
29 };
30 use tracing::debug;
31
32 type PostViewTuple = (
33   Post,
34   PersonSafe,
35   CommunitySafe,
36   Option<CommunityPersonBan>,
37   PostAggregates,
38   Option<CommunityFollower>,
39   Option<PostSaved>,
40   Option<PostRead>,
41   Option<PersonBlock>,
42   Option<i16>,
43 );
44
45 impl PostView {
46   pub fn read(
47     conn: &PgConnection,
48     post_id: PostId,
49     my_person_id: Option<PersonId>,
50   ) -> Result<Self, Error> {
51     // The left join below will return None in this case
52     let person_id_join = my_person_id.unwrap_or(PersonId(-1));
53
54     let (
55       post,
56       creator,
57       community,
58       creator_banned_from_community,
59       counts,
60       follower,
61       saved,
62       read,
63       creator_blocked,
64       post_like,
65     ) = post::table
66       .find(post_id)
67       .inner_join(person::table)
68       .inner_join(community::table)
69       .left_join(
70         community_person_ban::table.on(
71           post::community_id
72             .eq(community_person_ban::community_id)
73             .and(community_person_ban::person_id.eq(post::creator_id))
74             .and(
75               community_person_ban::expires
76                 .is_null()
77                 .or(community_person_ban::expires.gt(now)),
78             ),
79         ),
80       )
81       .inner_join(post_aggregates::table)
82       .left_join(
83         community_follower::table.on(
84           post::community_id
85             .eq(community_follower::community_id)
86             .and(community_follower::person_id.eq(person_id_join)),
87         ),
88       )
89       .left_join(
90         post_saved::table.on(
91           post::id
92             .eq(post_saved::post_id)
93             .and(post_saved::person_id.eq(person_id_join)),
94         ),
95       )
96       .left_join(
97         post_read::table.on(
98           post::id
99             .eq(post_read::post_id)
100             .and(post_read::person_id.eq(person_id_join)),
101         ),
102       )
103       .left_join(
104         person_block::table.on(
105           post::creator_id
106             .eq(person_block::target_id)
107             .and(person_block::person_id.eq(person_id_join)),
108         ),
109       )
110       .left_join(
111         post_like::table.on(
112           post::id
113             .eq(post_like::post_id)
114             .and(post_like::person_id.eq(person_id_join)),
115         ),
116       )
117       .select((
118         post::all_columns,
119         Person::safe_columns_tuple(),
120         Community::safe_columns_tuple(),
121         community_person_ban::all_columns.nullable(),
122         post_aggregates::all_columns,
123         community_follower::all_columns.nullable(),
124         post_saved::all_columns.nullable(),
125         post_read::all_columns.nullable(),
126         person_block::all_columns.nullable(),
127         post_like::score.nullable(),
128       ))
129       .first::<PostViewTuple>(conn)?;
130
131     // If a person is given, then my_vote, if None, should be 0, not null
132     // Necessary to differentiate between other person's votes
133     let my_vote = if my_person_id.is_some() && post_like.is_none() {
134       Some(0)
135     } else {
136       post_like
137     };
138
139     Ok(PostView {
140       post,
141       creator,
142       community,
143       creator_banned_from_community: creator_banned_from_community.is_some(),
144       counts,
145       subscribed: CommunityFollower::to_subscribed_type(&follower),
146       saved: saved.is_some(),
147       read: read.is_some(),
148       creator_blocked: creator_blocked.is_some(),
149       my_vote,
150     })
151   }
152 }
153
154 pub struct PostQueryBuilder<'a> {
155   conn: &'a PgConnection,
156   listing_type: Option<ListingType>,
157   sort: Option<SortType>,
158   creator_id: Option<PersonId>,
159   community_id: Option<CommunityId>,
160   community_actor_id: Option<DbUrl>,
161   my_person_id: Option<PersonId>,
162   search_term: Option<String>,
163   url_search: Option<String>,
164   show_nsfw: Option<bool>,
165   show_bot_accounts: Option<bool>,
166   show_read_posts: Option<bool>,
167   saved_only: Option<bool>,
168   page: Option<i64>,
169   limit: Option<i64>,
170 }
171
172 impl<'a> PostQueryBuilder<'a> {
173   pub fn create(conn: &'a PgConnection) -> Self {
174     PostQueryBuilder {
175       conn,
176       listing_type: None,
177       sort: None,
178       creator_id: None,
179       community_id: None,
180       community_actor_id: None,
181       my_person_id: None,
182       search_term: None,
183       url_search: None,
184       show_nsfw: None,
185       show_bot_accounts: None,
186       show_read_posts: None,
187       saved_only: None,
188       page: None,
189       limit: None,
190     }
191   }
192
193   pub fn listing_type<T: MaybeOptional<ListingType>>(mut self, listing_type: T) -> Self {
194     self.listing_type = listing_type.get_optional();
195     self
196   }
197
198   pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
199     self.sort = sort.get_optional();
200     self
201   }
202
203   pub fn community_id<T: MaybeOptional<CommunityId>>(mut self, community_id: T) -> Self {
204     self.community_id = community_id.get_optional();
205     self
206   }
207
208   pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
209     self.my_person_id = my_person_id.get_optional();
210     self
211   }
212
213   pub fn community_actor_id<T: MaybeOptional<DbUrl>>(mut self, community_actor_id: T) -> Self {
214     self.community_actor_id = community_actor_id.get_optional();
215     self
216   }
217
218   pub fn creator_id<T: MaybeOptional<PersonId>>(mut self, creator_id: T) -> Self {
219     self.creator_id = creator_id.get_optional();
220     self
221   }
222
223   pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
224     self.search_term = search_term.get_optional();
225     self
226   }
227
228   pub fn url_search<T: MaybeOptional<String>>(mut self, url_search: T) -> Self {
229     self.url_search = url_search.get_optional();
230     self
231   }
232
233   pub fn show_nsfw<T: MaybeOptional<bool>>(mut self, show_nsfw: T) -> Self {
234     self.show_nsfw = show_nsfw.get_optional();
235     self
236   }
237
238   pub fn show_bot_accounts<T: MaybeOptional<bool>>(mut self, show_bot_accounts: T) -> Self {
239     self.show_bot_accounts = show_bot_accounts.get_optional();
240     self
241   }
242
243   pub fn show_read_posts<T: MaybeOptional<bool>>(mut self, show_read_posts: T) -> Self {
244     self.show_read_posts = show_read_posts.get_optional();
245     self
246   }
247
248   pub fn saved_only<T: MaybeOptional<bool>>(mut self, saved_only: T) -> Self {
249     self.saved_only = saved_only.get_optional();
250     self
251   }
252
253   pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
254     self.page = page.get_optional();
255     self
256   }
257
258   pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
259     self.limit = limit.get_optional();
260     self
261   }
262
263   pub fn list(self) -> Result<Vec<PostView>, Error> {
264     use diesel::dsl::*;
265
266     // The left join below will return None in this case
267     let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
268
269     let mut query = post::table
270       .inner_join(person::table)
271       .inner_join(community::table)
272       .left_join(
273         community_person_ban::table.on(
274           post::community_id
275             .eq(community_person_ban::community_id)
276             .and(community_person_ban::person_id.eq(post::creator_id))
277             .and(
278               community_person_ban::expires
279                 .is_null()
280                 .or(community_person_ban::expires.gt(now)),
281             ),
282         ),
283       )
284       .inner_join(post_aggregates::table)
285       .left_join(
286         community_follower::table.on(
287           post::community_id
288             .eq(community_follower::community_id)
289             .and(community_follower::person_id.eq(person_id_join)),
290         ),
291       )
292       .left_join(
293         post_saved::table.on(
294           post::id
295             .eq(post_saved::post_id)
296             .and(post_saved::person_id.eq(person_id_join)),
297         ),
298       )
299       .left_join(
300         post_read::table.on(
301           post::id
302             .eq(post_read::post_id)
303             .and(post_read::person_id.eq(person_id_join)),
304         ),
305       )
306       .left_join(
307         person_block::table.on(
308           post::creator_id
309             .eq(person_block::target_id)
310             .and(person_block::person_id.eq(person_id_join)),
311         ),
312       )
313       .left_join(
314         community_block::table.on(
315           community::id
316             .eq(community_block::community_id)
317             .and(community_block::person_id.eq(person_id_join)),
318         ),
319       )
320       .left_join(
321         post_like::table.on(
322           post::id
323             .eq(post_like::post_id)
324             .and(post_like::person_id.eq(person_id_join)),
325         ),
326       )
327       .select((
328         post::all_columns,
329         Person::safe_columns_tuple(),
330         Community::safe_columns_tuple(),
331         community_person_ban::all_columns.nullable(),
332         post_aggregates::all_columns,
333         community_follower::all_columns.nullable(),
334         post_saved::all_columns.nullable(),
335         post_read::all_columns.nullable(),
336         person_block::all_columns.nullable(),
337         post_like::score.nullable(),
338       ))
339       .into_boxed();
340
341     if let Some(listing_type) = self.listing_type {
342       match listing_type {
343         ListingType::Subscribed => {
344           query = query.filter(community_follower::person_id.is_not_null())
345         }
346         ListingType::Local => {
347           query = query.filter(community::local.eq(true)).filter(
348             community::hidden
349               .eq(false)
350               .or(community_follower::person_id.eq(person_id_join)),
351           );
352         }
353         ListingType::All => {
354           query = query.filter(
355             community::hidden
356               .eq(false)
357               .or(community_follower::person_id.eq(person_id_join)),
358           )
359         }
360         ListingType::Community => {
361           if let Some(community_id) = self.community_id {
362             query = query
363               .filter(post::community_id.eq(community_id))
364               .then_order_by(post_aggregates::stickied.desc());
365           }
366         }
367       }
368     }
369
370     if let Some(community_actor_id) = self.community_actor_id {
371       query = query
372         .filter(community::actor_id.eq(community_actor_id))
373         .then_order_by(post_aggregates::stickied.desc());
374     }
375
376     if let Some(url_search) = self.url_search {
377       query = query.filter(post::url.eq(url_search));
378     }
379
380     if let Some(search_term) = self.search_term {
381       let searcher = fuzzy_search(&search_term);
382       query = query.filter(
383         post::name
384           .ilike(searcher.to_owned())
385           .or(post::body.ilike(searcher)),
386       );
387     }
388
389     // If its for a specific person, show the removed / deleted
390     if let Some(creator_id) = self.creator_id {
391       query = query.filter(post::creator_id.eq(creator_id));
392     }
393
394     if !self.show_nsfw.unwrap_or(false) {
395       query = query
396         .filter(post::nsfw.eq(false))
397         .filter(community::nsfw.eq(false));
398     };
399
400     if !self.show_bot_accounts.unwrap_or(true) {
401       query = query.filter(person::bot_account.eq(false));
402     };
403
404     if self.saved_only.unwrap_or(false) {
405       query = query.filter(post_saved::id.is_not_null());
406     }
407     // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read
408     // setting wont be able to see saved posts.
409     else if !self.show_read_posts.unwrap_or(true) {
410       query = query.filter(post_read::id.is_null());
411     }
412
413     // Don't show blocked communities or persons
414     if self.my_person_id.is_some() {
415       query = query.filter(community_block::person_id.is_null());
416       query = query.filter(person_block::person_id.is_null());
417     }
418
419     query = match self.sort.unwrap_or(SortType::Hot) {
420       SortType::Active => query
421         .then_order_by(
422           hot_rank(
423             post_aggregates::score,
424             post_aggregates::newest_comment_time_necro,
425           )
426           .desc(),
427         )
428         .then_order_by(post_aggregates::newest_comment_time_necro.desc()),
429       SortType::Hot => query
430         .then_order_by(hot_rank(post_aggregates::score, post_aggregates::published).desc())
431         .then_order_by(post_aggregates::published.desc()),
432       SortType::New => query.then_order_by(post_aggregates::published.desc()),
433       SortType::NewComments => query.then_order_by(post_aggregates::newest_comment_time.desc()),
434       SortType::MostComments => query
435         .then_order_by(post_aggregates::comments.desc())
436         .then_order_by(post_aggregates::published.desc()),
437       SortType::TopAll => query
438         .then_order_by(post_aggregates::score.desc())
439         .then_order_by(post_aggregates::published.desc()),
440       SortType::TopYear => query
441         .filter(post_aggregates::published.gt(now - 1.years()))
442         .then_order_by(post_aggregates::score.desc())
443         .then_order_by(post_aggregates::published.desc()),
444       SortType::TopMonth => query
445         .filter(post_aggregates::published.gt(now - 1.months()))
446         .then_order_by(post_aggregates::score.desc())
447         .then_order_by(post_aggregates::published.desc()),
448       SortType::TopWeek => query
449         .filter(post_aggregates::published.gt(now - 1.weeks()))
450         .then_order_by(post_aggregates::score.desc())
451         .then_order_by(post_aggregates::published.desc()),
452       SortType::TopDay => query
453         .filter(post_aggregates::published.gt(now - 1.days()))
454         .then_order_by(post_aggregates::score.desc())
455         .then_order_by(post_aggregates::published.desc()),
456     };
457
458     let (limit, offset) = limit_and_offset(self.page, self.limit);
459
460     query = query
461       .limit(limit)
462       .offset(offset)
463       .filter(post::removed.eq(false))
464       .filter(post::deleted.eq(false))
465       .filter(community::removed.eq(false))
466       .filter(community::deleted.eq(false));
467
468     debug!("Post View Query: {:?}", debug_query::<Pg, _>(&query));
469
470     let res = query.load::<PostViewTuple>(self.conn)?;
471
472     Ok(PostView::from_tuple_to_vec(res))
473   }
474 }
475
476 impl ViewToVec for PostView {
477   type DbTuple = PostViewTuple;
478   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
479     items
480       .iter()
481       .map(|a| Self {
482         post: a.0.to_owned(),
483         creator: a.1.to_owned(),
484         community: a.2.to_owned(),
485         creator_banned_from_community: a.3.is_some(),
486         counts: a.4.to_owned(),
487         subscribed: CommunityFollower::to_subscribed_type(&a.5),
488         saved: a.6.is_some(),
489         read: a.7.is_some(),
490         creator_blocked: a.8.is_some(),
491         my_vote: a.9,
492       })
493       .collect::<Vec<Self>>()
494   }
495 }
496
497 #[cfg(test)]
498 mod tests {
499   use crate::post_view::{PostQueryBuilder, PostView};
500   use lemmy_db_schema::{
501     aggregates::structs::PostAggregates,
502     source::{
503       community::*,
504       community_block::{CommunityBlock, CommunityBlockForm},
505       person::*,
506       person_block::{PersonBlock, PersonBlockForm},
507       post::*,
508     },
509     traits::{Blockable, Crud, Likeable},
510     utils::establish_unpooled_connection,
511     ListingType,
512     SortType,
513     SubscribedType,
514   };
515   use serial_test::serial;
516
517   #[test]
518   #[serial]
519   fn test_crud() {
520     let conn = establish_unpooled_connection();
521
522     let person_name = "tegan".to_string();
523     let community_name = "test_community_3".to_string();
524     let post_name = "test post 3".to_string();
525     let bot_post_name = "test bot post".to_string();
526
527     let new_person = PersonForm {
528       name: person_name.to_owned(),
529       ..PersonForm::default()
530     };
531
532     let inserted_person = Person::create(&conn, &new_person).unwrap();
533
534     let new_bot = PersonForm {
535       name: person_name.to_owned(),
536       bot_account: Some(true),
537       ..PersonForm::default()
538     };
539
540     let inserted_bot = Person::create(&conn, &new_bot).unwrap();
541
542     let new_community = CommunityForm {
543       name: community_name.to_owned(),
544       title: "nada".to_owned(),
545       ..CommunityForm::default()
546     };
547
548     let inserted_community = Community::create(&conn, &new_community).unwrap();
549
550     // Test a person block, make sure the post query doesn't include their post
551     let blocked_person = PersonForm {
552       name: person_name.to_owned(),
553       ..PersonForm::default()
554     };
555
556     let inserted_blocked_person = Person::create(&conn, &blocked_person).unwrap();
557
558     let post_from_blocked_person = PostForm {
559       name: "blocked_person_post".to_string(),
560       creator_id: inserted_blocked_person.id,
561       community_id: inserted_community.id,
562       ..PostForm::default()
563     };
564
565     Post::create(&conn, &post_from_blocked_person).unwrap();
566
567     // block that person
568     let person_block = PersonBlockForm {
569       person_id: inserted_person.id,
570       target_id: inserted_blocked_person.id,
571     };
572
573     PersonBlock::block(&conn, &person_block).unwrap();
574
575     // A sample post
576     let new_post = PostForm {
577       name: post_name.to_owned(),
578       creator_id: inserted_person.id,
579       community_id: inserted_community.id,
580       ..PostForm::default()
581     };
582
583     let inserted_post = Post::create(&conn, &new_post).unwrap();
584
585     let new_bot_post = PostForm {
586       name: bot_post_name,
587       creator_id: inserted_bot.id,
588       community_id: inserted_community.id,
589       ..PostForm::default()
590     };
591
592     let _inserted_bot_post = Post::create(&conn, &new_bot_post).unwrap();
593
594     let post_like_form = PostLikeForm {
595       post_id: inserted_post.id,
596       person_id: inserted_person.id,
597       score: 1,
598     };
599
600     let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
601
602     let expected_post_like = PostLike {
603       id: inserted_post_like.id,
604       post_id: inserted_post.id,
605       person_id: inserted_person.id,
606       published: inserted_post_like.published,
607       score: 1,
608     };
609
610     let read_post_listings_with_person = PostQueryBuilder::create(&conn)
611       .listing_type(ListingType::Community)
612       .sort(SortType::New)
613       .show_bot_accounts(false)
614       .community_id(inserted_community.id)
615       .my_person_id(inserted_person.id)
616       .list()
617       .unwrap();
618
619     let read_post_listings_no_person = PostQueryBuilder::create(&conn)
620       .listing_type(ListingType::Community)
621       .sort(SortType::New)
622       .community_id(inserted_community.id)
623       .list()
624       .unwrap();
625
626     let read_post_listing_no_person = PostView::read(&conn, inserted_post.id, None).unwrap();
627     let read_post_listing_with_person =
628       PostView::read(&conn, inserted_post.id, Some(inserted_person.id)).unwrap();
629
630     let agg = PostAggregates::read(&conn, inserted_post.id).unwrap();
631
632     // the non person version
633     let expected_post_listing_no_person = PostView {
634       post: Post {
635         id: inserted_post.id,
636         name: post_name,
637         creator_id: inserted_person.id,
638         url: None,
639         body: None,
640         published: inserted_post.published,
641         updated: None,
642         community_id: inserted_community.id,
643         removed: false,
644         deleted: false,
645         locked: false,
646         stickied: false,
647         nsfw: false,
648         embed_title: None,
649         embed_description: None,
650         embed_video_url: None,
651         thumbnail_url: None,
652         ap_id: inserted_post.ap_id.to_owned(),
653         local: true,
654       },
655       my_vote: None,
656       creator: PersonSafe {
657         id: inserted_person.id,
658         name: person_name,
659         display_name: None,
660         published: inserted_person.published,
661         avatar: None,
662         actor_id: inserted_person.actor_id.to_owned(),
663         local: true,
664         admin: false,
665         bot_account: false,
666         banned: false,
667         deleted: false,
668         bio: None,
669         banner: None,
670         updated: None,
671         inbox_url: inserted_person.inbox_url.to_owned(),
672         shared_inbox_url: None,
673         matrix_user_id: None,
674         ban_expires: None,
675       },
676       creator_banned_from_community: false,
677       community: CommunitySafe {
678         id: inserted_community.id,
679         name: community_name,
680         icon: None,
681         removed: false,
682         deleted: false,
683         nsfw: false,
684         actor_id: inserted_community.actor_id.to_owned(),
685         local: true,
686         title: "nada".to_owned(),
687         description: None,
688         updated: None,
689         banner: None,
690         hidden: false,
691         posting_restricted_to_mods: false,
692         published: inserted_community.published,
693       },
694       counts: PostAggregates {
695         id: agg.id,
696         post_id: inserted_post.id,
697         comments: 0,
698         score: 1,
699         upvotes: 1,
700         downvotes: 0,
701         stickied: false,
702         published: agg.published,
703         newest_comment_time_necro: inserted_post.published,
704         newest_comment_time: inserted_post.published,
705       },
706       subscribed: SubscribedType::NotSubscribed,
707       read: false,
708       saved: false,
709       creator_blocked: false,
710     };
711
712     // Test a community block
713     let community_block = CommunityBlockForm {
714       person_id: inserted_person.id,
715       community_id: inserted_community.id,
716     };
717     CommunityBlock::block(&conn, &community_block).unwrap();
718
719     let read_post_listings_with_person_after_block = PostQueryBuilder::create(&conn)
720       .listing_type(ListingType::Community)
721       .sort(SortType::New)
722       .show_bot_accounts(false)
723       .community_id(inserted_community.id)
724       .my_person_id(inserted_person.id)
725       .list()
726       .unwrap();
727
728     // TODO More needs to be added here
729     let mut expected_post_listing_with_user = expected_post_listing_no_person.to_owned();
730     expected_post_listing_with_user.my_vote = Some(1);
731
732     let like_removed = PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
733     let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
734     PersonBlock::unblock(&conn, &person_block).unwrap();
735     CommunityBlock::unblock(&conn, &community_block).unwrap();
736     Community::delete(&conn, inserted_community.id).unwrap();
737     Person::delete(&conn, inserted_person.id).unwrap();
738     Person::delete(&conn, inserted_bot.id).unwrap();
739     Person::delete(&conn, inserted_blocked_person.id).unwrap();
740
741     // The with user
742     assert_eq!(
743       expected_post_listing_with_user,
744       read_post_listings_with_person[0]
745     );
746     assert_eq!(
747       expected_post_listing_with_user,
748       read_post_listing_with_person
749     );
750
751     // Should be only one person, IE the bot post, and blocked should be missing
752     assert_eq!(1, read_post_listings_with_person.len());
753
754     // Without the user
755     assert_eq!(
756       expected_post_listing_no_person,
757       read_post_listings_no_person[1]
758     );
759     assert_eq!(expected_post_listing_no_person, read_post_listing_no_person);
760
761     // Should be 2 posts, with the bot post, and the blocked
762     assert_eq!(3, read_post_listings_no_person.len());
763
764     // Should be 0 posts after the community block
765     assert_eq!(0, read_post_listings_with_person_after_block.len());
766
767     assert_eq!(expected_post_like, inserted_post_like);
768     assert_eq!(1, like_removed);
769     assert_eq!(1, num_deleted);
770   }
771 }