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