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