]> Untitled Git - lemmy.git/blob - crates/db_views/src/post_view.rs
Moving settings to Database. (#2492)
[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       actor_language::LocalUserLanguage,
458       community::*,
459       community_block::{CommunityBlock, CommunityBlockForm},
460       instance::Instance,
461       language::Language,
462       local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
463       person::*,
464       person_block::{PersonBlock, PersonBlockForm},
465       post::*,
466     },
467     traits::{Blockable, Crud, Likeable},
468     utils::establish_unpooled_connection,
469     SortType,
470     SubscribedType,
471   };
472   use serial_test::serial;
473
474   struct Data {
475     inserted_instance: Instance,
476     inserted_person: Person,
477     inserted_local_user: LocalUser,
478     inserted_blocked_person: Person,
479     inserted_bot: Person,
480     inserted_community: Community,
481     inserted_post: Post,
482   }
483
484   fn init_data(conn: &mut PgConnection) -> Data {
485     let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
486
487     let person_name = "tegan".to_string();
488
489     let new_person = PersonInsertForm::builder()
490       .name(person_name.to_owned())
491       .public_key("pubkey".to_string())
492       .instance_id(inserted_instance.id)
493       .build();
494
495     let inserted_person = Person::create(conn, &new_person).unwrap();
496
497     let local_user_form = LocalUserInsertForm::builder()
498       .person_id(inserted_person.id)
499       .password_encrypted("".to_string())
500       .build();
501     let inserted_local_user = LocalUser::create(conn, &local_user_form).unwrap();
502
503     let new_bot = PersonInsertForm::builder()
504       .name("mybot".to_string())
505       .bot_account(Some(true))
506       .public_key("pubkey".to_string())
507       .instance_id(inserted_instance.id)
508       .build();
509
510     let inserted_bot = Person::create(conn, &new_bot).unwrap();
511
512     let new_community = CommunityInsertForm::builder()
513       .name("test_community_3".to_string())
514       .title("nada".to_owned())
515       .public_key("pubkey".to_string())
516       .instance_id(inserted_instance.id)
517       .build();
518
519     let inserted_community = Community::create(conn, &new_community).unwrap();
520
521     // Test a person block, make sure the post query doesn't include their post
522     let blocked_person = PersonInsertForm::builder()
523       .name(person_name)
524       .public_key("pubkey".to_string())
525       .instance_id(inserted_instance.id)
526       .build();
527
528     let inserted_blocked_person = Person::create(conn, &blocked_person).unwrap();
529
530     let post_from_blocked_person = PostInsertForm::builder()
531       .name("blocked_person_post".to_string())
532       .creator_id(inserted_blocked_person.id)
533       .community_id(inserted_community.id)
534       .language_id(Some(LanguageId(1)))
535       .build();
536
537     Post::create(conn, &post_from_blocked_person).unwrap();
538
539     // block that person
540     let person_block = PersonBlockForm {
541       person_id: inserted_person.id,
542       target_id: inserted_blocked_person.id,
543     };
544
545     PersonBlock::block(conn, &person_block).unwrap();
546
547     // A sample post
548     let new_post = PostInsertForm::builder()
549       .name("test post 3".to_string())
550       .creator_id(inserted_person.id)
551       .community_id(inserted_community.id)
552       .language_id(Some(LanguageId(47)))
553       .build();
554
555     let inserted_post = Post::create(conn, &new_post).unwrap();
556
557     let new_bot_post = PostInsertForm::builder()
558       .name("test bot post".to_string())
559       .creator_id(inserted_bot.id)
560       .community_id(inserted_community.id)
561       .build();
562
563     let _inserted_bot_post = Post::create(conn, &new_bot_post).unwrap();
564
565     Data {
566       inserted_instance,
567       inserted_person,
568       inserted_local_user,
569       inserted_blocked_person,
570       inserted_bot,
571       inserted_community,
572       inserted_post,
573     }
574   }
575
576   #[test]
577   #[serial]
578   fn post_listing_with_person() {
579     let conn = &mut establish_unpooled_connection();
580     let data = init_data(conn);
581
582     let local_user_form = LocalUserUpdateForm::builder()
583       .show_bot_accounts(Some(false))
584       .build();
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 = LocalUserUpdateForm::builder()
613       .show_bot_accounts(Some(true))
614       .build();
615     let inserted_local_user =
616       LocalUser::update(conn, data.inserted_local_user.id, &local_user_form).unwrap();
617
618     let post_listings_with_bots = PostQuery::builder()
619       .conn(conn)
620       .sort(Some(SortType::New))
621       .community_id(Some(data.inserted_community.id))
622       .local_user(Some(&inserted_local_user))
623       .build()
624       .list()
625       .unwrap();
626     // should include bot post which has "undetermined" language
627     assert_eq!(2, post_listings_with_bots.len());
628
629     cleanup(data, conn);
630   }
631
632   #[test]
633   #[serial]
634   fn post_listing_no_person() {
635     let conn = &mut establish_unpooled_connection();
636     let data = init_data(conn);
637
638     let read_post_listing_multiple_no_person = PostQuery::builder()
639       .conn(conn)
640       .sort(Some(SortType::New))
641       .community_id(Some(data.inserted_community.id))
642       .build()
643       .list()
644       .unwrap();
645
646     let read_post_listing_single_no_person =
647       PostView::read(conn, data.inserted_post.id, None).unwrap();
648
649     let expected_post_listing_no_person = expected_post_view(&data, conn);
650
651     // Should be 2 posts, with the bot post, and the blocked
652     assert_eq!(3, read_post_listing_multiple_no_person.len());
653
654     assert_eq!(
655       expected_post_listing_no_person,
656       read_post_listing_multiple_no_person[1]
657     );
658     assert_eq!(
659       expected_post_listing_no_person,
660       read_post_listing_single_no_person
661     );
662
663     cleanup(data, conn);
664   }
665
666   #[test]
667   #[serial]
668   fn post_listing_block_community() {
669     let conn = &mut establish_unpooled_connection();
670     let data = init_data(conn);
671
672     let community_block = CommunityBlockForm {
673       person_id: data.inserted_person.id,
674       community_id: data.inserted_community.id,
675     };
676     CommunityBlock::block(conn, &community_block).unwrap();
677
678     let read_post_listings_with_person_after_block = PostQuery::builder()
679       .conn(conn)
680       .sort(Some(SortType::New))
681       .community_id(Some(data.inserted_community.id))
682       .local_user(Some(&data.inserted_local_user))
683       .build()
684       .list()
685       .unwrap();
686     // Should be 0 posts after the community block
687     assert_eq!(0, read_post_listings_with_person_after_block.len());
688
689     CommunityBlock::unblock(conn, &community_block).unwrap();
690     cleanup(data, conn);
691   }
692
693   #[test]
694   #[serial]
695   fn post_listing_like() {
696     let conn = &mut establish_unpooled_connection();
697     let data = init_data(conn);
698
699     let post_like_form = PostLikeForm {
700       post_id: data.inserted_post.id,
701       person_id: data.inserted_person.id,
702       score: 1,
703     };
704
705     let inserted_post_like = PostLike::like(conn, &post_like_form).unwrap();
706
707     let expected_post_like = PostLike {
708       id: inserted_post_like.id,
709       post_id: data.inserted_post.id,
710       person_id: data.inserted_person.id,
711       published: inserted_post_like.published,
712       score: 1,
713     };
714     assert_eq!(expected_post_like, inserted_post_like);
715
716     let like_removed =
717       PostLike::remove(conn, data.inserted_person.id, data.inserted_post.id).unwrap();
718     assert_eq!(1, like_removed);
719     cleanup(data, conn);
720   }
721
722   #[test]
723   #[serial]
724   fn post_listing_person_language() {
725     let conn = &mut establish_unpooled_connection();
726     let data = init_data(conn);
727
728     let spanish_id = Language::read_id_from_code(conn, "es").unwrap();
729     let post_spanish = PostInsertForm::builder()
730       .name("asffgdsc".to_string())
731       .creator_id(data.inserted_person.id)
732       .community_id(data.inserted_community.id)
733       .language_id(Some(spanish_id))
734       .build();
735
736     Post::create(conn, &post_spanish).unwrap();
737
738     let post_listings_all = PostQuery::builder()
739       .conn(conn)
740       .sort(Some(SortType::New))
741       .local_user(Some(&data.inserted_local_user))
742       .build()
743       .list()
744       .unwrap();
745
746     // no language filters specified, all posts should be returned
747     assert_eq!(3, post_listings_all.len());
748
749     let french_id = Language::read_id_from_code(conn, "fr").unwrap();
750     LocalUserLanguage::update(conn, vec![french_id], data.inserted_local_user.id).unwrap();
751
752     let post_listing_french = PostQuery::builder()
753       .conn(conn)
754       .sort(Some(SortType::New))
755       .local_user(Some(&data.inserted_local_user))
756       .build()
757       .list()
758       .unwrap();
759
760     // only one french language post should be returned
761     assert_eq!(1, post_listing_french.len());
762     assert_eq!(french_id, post_listing_french[0].post.language_id);
763
764     let undetermined_id = Language::read_id_from_code(conn, "und").unwrap();
765     LocalUserLanguage::update(
766       conn,
767       vec![french_id, undetermined_id],
768       data.inserted_local_user.id,
769     )
770     .unwrap();
771     let post_listings_french_und = PostQuery::builder()
772       .conn(conn)
773       .sort(Some(SortType::New))
774       .local_user(Some(&data.inserted_local_user))
775       .build()
776       .list()
777       .unwrap();
778
779     // french post and undetermined language post should be returned
780     assert_eq!(2, post_listings_french_und.len());
781     assert_eq!(
782       undetermined_id,
783       post_listings_french_und[0].post.language_id
784     );
785     assert_eq!(french_id, post_listings_french_und[1].post.language_id);
786
787     cleanup(data, conn);
788   }
789
790   fn cleanup(data: Data, conn: &mut PgConnection) {
791     let num_deleted = Post::delete(conn, data.inserted_post.id).unwrap();
792     Community::delete(conn, data.inserted_community.id).unwrap();
793     Person::delete(conn, data.inserted_person.id).unwrap();
794     Person::delete(conn, data.inserted_bot.id).unwrap();
795     Person::delete(conn, data.inserted_blocked_person.id).unwrap();
796     Instance::delete(conn, data.inserted_instance.id).unwrap();
797     assert_eq!(1, num_deleted);
798   }
799
800   fn expected_post_view(data: &Data, conn: &mut PgConnection) -> PostView {
801     let (inserted_person, inserted_community, inserted_post) = (
802       &data.inserted_person,
803       &data.inserted_community,
804       &data.inserted_post,
805     );
806     let agg = PostAggregates::read(conn, inserted_post.id).unwrap();
807
808     PostView {
809       post: Post {
810         id: inserted_post.id,
811         name: inserted_post.name.clone(),
812         creator_id: inserted_person.id,
813         url: None,
814         body: None,
815         published: inserted_post.published,
816         updated: None,
817         community_id: inserted_community.id,
818         removed: false,
819         deleted: false,
820         locked: false,
821         stickied: false,
822         nsfw: false,
823         embed_title: None,
824         embed_description: None,
825         embed_video_url: None,
826         thumbnail_url: None,
827         ap_id: inserted_post.ap_id.to_owned(),
828         local: true,
829         language_id: LanguageId(47),
830       },
831       my_vote: None,
832       unread_comments: 0,
833       creator: PersonSafe {
834         id: inserted_person.id,
835         name: inserted_person.name.clone(),
836         display_name: None,
837         published: inserted_person.published,
838         avatar: None,
839         actor_id: inserted_person.actor_id.to_owned(),
840         local: true,
841         admin: false,
842         bot_account: false,
843         banned: false,
844         deleted: false,
845         bio: None,
846         banner: None,
847         updated: None,
848         inbox_url: inserted_person.inbox_url.to_owned(),
849         shared_inbox_url: None,
850         matrix_user_id: None,
851         ban_expires: None,
852         instance_id: data.inserted_instance.id,
853       },
854       creator_banned_from_community: false,
855       community: CommunitySafe {
856         id: inserted_community.id,
857         name: inserted_community.name.clone(),
858         icon: None,
859         removed: false,
860         deleted: false,
861         nsfw: false,
862         actor_id: inserted_community.actor_id.to_owned(),
863         local: true,
864         title: "nada".to_owned(),
865         description: None,
866         updated: None,
867         banner: None,
868         hidden: false,
869         posting_restricted_to_mods: false,
870         published: inserted_community.published,
871         instance_id: data.inserted_instance.id,
872       },
873       counts: PostAggregates {
874         id: agg.id,
875         post_id: inserted_post.id,
876         comments: 0,
877         score: 0,
878         upvotes: 0,
879         downvotes: 0,
880         stickied: false,
881         published: agg.published,
882         newest_comment_time_necro: inserted_post.published,
883         newest_comment_time: inserted_post.published,
884       },
885       subscribed: SubscribedType::NotSubscribed,
886       read: false,
887       saved: false,
888       creator_blocked: false,
889     }
890   }
891 }