]> Untitled Git - lemmy.git/blob - crates/db_views/src/comment_view.rs
Force enable undetermined language (#2851)
[lemmy.git] / crates / db_views / src / comment_view.rs
1 use crate::structs::CommentView;
2 use diesel::{
3   result::Error,
4   BoolExpressionMethods,
5   ExpressionMethods,
6   JoinOnDsl,
7   NullableExpressionMethods,
8   PgTextExpressionMethods,
9   QueryDsl,
10 };
11 use diesel_async::RunQueryDsl;
12 use diesel_ltree::{nlevel, subpath, Ltree, LtreeExtensions};
13 use lemmy_db_schema::{
14   aggregates::structs::CommentAggregates,
15   newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId},
16   schema::{
17     comment,
18     comment_aggregates,
19     comment_like,
20     comment_saved,
21     community,
22     community_block,
23     community_follower,
24     community_person_ban,
25     local_user_language,
26     person,
27     person_block,
28     post,
29   },
30   source::{
31     comment::{Comment, CommentSaved},
32     community::{Community, CommunityFollower, CommunityPersonBan},
33     local_user::LocalUser,
34     person::Person,
35     person_block::PersonBlock,
36     post::Post,
37   },
38   traits::JoinView,
39   utils::{functions::hot_rank, fuzzy_search, get_conn, limit_and_offset_unlimited, DbPool},
40   CommentSortType,
41   ListingType,
42 };
43 use typed_builder::TypedBuilder;
44
45 type CommentViewTuple = (
46   Comment,
47   Person,
48   Post,
49   Community,
50   CommentAggregates,
51   Option<CommunityPersonBan>,
52   Option<CommunityFollower>,
53   Option<CommentSaved>,
54   Option<PersonBlock>,
55   Option<i16>,
56 );
57
58 impl CommentView {
59   pub async fn read(
60     pool: &DbPool,
61     comment_id: CommentId,
62     my_person_id: Option<PersonId>,
63   ) -> Result<Self, Error> {
64     let conn = &mut get_conn(pool).await?;
65
66     // The left join below will return None in this case
67     let person_id_join = my_person_id.unwrap_or(PersonId(-1));
68
69     let (
70       comment,
71       creator,
72       post,
73       community,
74       counts,
75       creator_banned_from_community,
76       follower,
77       saved,
78       creator_blocked,
79       comment_like,
80     ) = comment::table
81       .find(comment_id)
82       .inner_join(person::table)
83       .inner_join(post::table)
84       .inner_join(community::table.on(post::community_id.eq(community::id)))
85       .inner_join(comment_aggregates::table)
86       .left_join(
87         community_person_ban::table.on(
88           community::id
89             .eq(community_person_ban::community_id)
90             .and(community_person_ban::person_id.eq(comment::creator_id)),
91         ),
92       )
93       .left_join(
94         community_follower::table.on(
95           post::community_id
96             .eq(community_follower::community_id)
97             .and(community_follower::person_id.eq(person_id_join)),
98         ),
99       )
100       .left_join(
101         comment_saved::table.on(
102           comment::id
103             .eq(comment_saved::comment_id)
104             .and(comment_saved::person_id.eq(person_id_join)),
105         ),
106       )
107       .left_join(
108         person_block::table.on(
109           comment::creator_id
110             .eq(person_block::target_id)
111             .and(person_block::person_id.eq(person_id_join)),
112         ),
113       )
114       .left_join(
115         comment_like::table.on(
116           comment::id
117             .eq(comment_like::comment_id)
118             .and(comment_like::person_id.eq(person_id_join)),
119         ),
120       )
121       .select((
122         comment::all_columns,
123         person::all_columns,
124         post::all_columns,
125         community::all_columns,
126         comment_aggregates::all_columns,
127         community_person_ban::all_columns.nullable(),
128         community_follower::all_columns.nullable(),
129         comment_saved::all_columns.nullable(),
130         person_block::all_columns.nullable(),
131         comment_like::score.nullable(),
132       ))
133       .first::<CommentViewTuple>(conn)
134       .await?;
135
136     // If a person is given, then my_vote, if None, should be 0, not null
137     // Necessary to differentiate between other person's votes
138     let my_vote = if my_person_id.is_some() && comment_like.is_none() {
139       Some(0)
140     } else {
141       comment_like
142     };
143
144     Ok(CommentView {
145       comment,
146       post,
147       creator,
148       community,
149       counts,
150       creator_banned_from_community: creator_banned_from_community.is_some(),
151       subscribed: CommunityFollower::to_subscribed_type(&follower),
152       saved: saved.is_some(),
153       creator_blocked: creator_blocked.is_some(),
154       my_vote,
155     })
156   }
157 }
158
159 #[derive(TypedBuilder)]
160 #[builder(field_defaults(default))]
161 pub struct CommentQuery<'a> {
162   #[builder(!default)]
163   pool: &'a DbPool,
164   listing_type: Option<ListingType>,
165   sort: Option<CommentSortType>,
166   community_id: Option<CommunityId>,
167   post_id: Option<PostId>,
168   parent_path: Option<Ltree>,
169   creator_id: Option<PersonId>,
170   local_user: Option<&'a LocalUser>,
171   search_term: Option<String>,
172   saved_only: Option<bool>,
173   show_deleted_and_removed: Option<bool>,
174   page: Option<i64>,
175   limit: Option<i64>,
176   max_depth: Option<i32>,
177 }
178
179 impl<'a> CommentQuery<'a> {
180   pub async fn list(self) -> Result<Vec<CommentView>, Error> {
181     let conn = &mut get_conn(self.pool).await?;
182
183     // The left join below will return None in this case
184     let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
185     let local_user_id_join = self.local_user.map(|l| l.id).unwrap_or(LocalUserId(-1));
186
187     let mut query = comment::table
188       .inner_join(person::table)
189       .inner_join(post::table)
190       .inner_join(community::table.on(post::community_id.eq(community::id)))
191       .inner_join(comment_aggregates::table)
192       .left_join(
193         community_person_ban::table.on(
194           community::id
195             .eq(community_person_ban::community_id)
196             .and(community_person_ban::person_id.eq(comment::creator_id)),
197         ),
198       )
199       .left_join(
200         community_follower::table.on(
201           post::community_id
202             .eq(community_follower::community_id)
203             .and(community_follower::person_id.eq(person_id_join)),
204         ),
205       )
206       .left_join(
207         comment_saved::table.on(
208           comment::id
209             .eq(comment_saved::comment_id)
210             .and(comment_saved::person_id.eq(person_id_join)),
211         ),
212       )
213       .left_join(
214         person_block::table.on(
215           comment::creator_id
216             .eq(person_block::target_id)
217             .and(person_block::person_id.eq(person_id_join)),
218         ),
219       )
220       .left_join(
221         community_block::table.on(
222           community::id
223             .eq(community_block::community_id)
224             .and(community_block::person_id.eq(person_id_join)),
225         ),
226       )
227       .left_join(
228         comment_like::table.on(
229           comment::id
230             .eq(comment_like::comment_id)
231             .and(comment_like::person_id.eq(person_id_join)),
232         ),
233       )
234       .left_join(
235         local_user_language::table.on(
236           comment::language_id
237             .eq(local_user_language::language_id)
238             .and(local_user_language::local_user_id.eq(local_user_id_join)),
239         ),
240       )
241       .select((
242         comment::all_columns,
243         person::all_columns,
244         post::all_columns,
245         community::all_columns,
246         comment_aggregates::all_columns,
247         community_person_ban::all_columns.nullable(),
248         community_follower::all_columns.nullable(),
249         comment_saved::all_columns.nullable(),
250         person_block::all_columns.nullable(),
251         comment_like::score.nullable(),
252       ))
253       .into_boxed();
254
255     if let Some(creator_id) = self.creator_id {
256       query = query.filter(comment::creator_id.eq(creator_id));
257     };
258
259     if let Some(post_id) = self.post_id {
260       query = query.filter(comment::post_id.eq(post_id));
261     };
262
263     if let Some(parent_path) = self.parent_path.as_ref() {
264       query = query.filter(comment::path.contained_by(parent_path));
265     };
266
267     if let Some(search_term) = self.search_term {
268       query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
269     };
270
271     if let Some(community_id) = self.community_id {
272       query = query.filter(post::community_id.eq(community_id));
273     }
274
275     if let Some(listing_type) = self.listing_type {
276       match listing_type {
277         ListingType::Subscribed => {
278           query = query.filter(community_follower::person_id.is_not_null())
279         } // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
280         ListingType::Local => {
281           query = query.filter(community::local.eq(true)).filter(
282             community::hidden
283               .eq(false)
284               .or(community_follower::person_id.eq(person_id_join)),
285           )
286         }
287         ListingType::All => {
288           query = query.filter(
289             community::hidden
290               .eq(false)
291               .or(community_follower::person_id.eq(person_id_join)),
292           )
293         }
294       }
295     }
296
297     if self.saved_only.unwrap_or(false) {
298       query = query.filter(comment_saved::comment_id.is_not_null());
299     }
300
301     if !self.show_deleted_and_removed.unwrap_or(true) {
302       query = query.filter(comment::deleted.eq(false));
303       query = query.filter(comment::removed.eq(false));
304     }
305
306     if !self.local_user.map(|l| l.show_bot_accounts).unwrap_or(true) {
307       query = query.filter(person::bot_account.eq(false));
308     };
309
310     if self.local_user.is_some() {
311       // Filter out the rows with missing languages
312       query = query.filter(local_user_language::language_id.is_not_null());
313
314       // Don't show blocked communities or persons
315       query = query.filter(community_block::person_id.is_null());
316       query = query.filter(person_block::person_id.is_null());
317     }
318
319     // A Max depth given means its a tree fetch
320     let (limit, offset) = if let Some(max_depth) = self.max_depth {
321       let depth_limit = if let Some(parent_path) = self.parent_path.as_ref() {
322         parent_path.0.split('.').count() as i32 + max_depth
323         // Add one because of root "0"
324       } else {
325         max_depth + 1
326       };
327
328       query = query.filter(nlevel(comment::path).le(depth_limit));
329
330       // Always order by the parent path first
331       query = query.order_by(subpath(comment::path, 0, -1));
332
333       // TODO limit question. Limiting does not work for comment threads ATM, only max_depth
334       // For now, don't do any limiting for tree fetches
335       // https://stackoverflow.com/questions/72983614/postgres-ltree-how-to-limit-the-max-number-of-children-at-any-given-level
336
337       // Don't use the regular error-checking one, many more comments must ofter be fetched.
338       // This does not work for comment trees, and the limit should be manually set to a high number
339       //
340       // If a max depth is given, then you know its a tree fetch, and limits should be ignored
341       (i64::MAX, 0)
342     } else {
343       limit_and_offset_unlimited(self.page, self.limit)
344     };
345
346     query = match self.sort.unwrap_or(CommentSortType::Hot) {
347       CommentSortType::Hot => query
348         .then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
349         .then_order_by(comment_aggregates::published.desc()),
350       CommentSortType::New => query.then_order_by(comment::published.desc()),
351       CommentSortType::Old => query.then_order_by(comment::published.asc()),
352       CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
353     };
354
355     // Note: deleted and removed comments are done on the front side
356     let res = query
357       .limit(limit)
358       .offset(offset)
359       .load::<CommentViewTuple>(conn)
360       .await?;
361
362     Ok(res.into_iter().map(CommentView::from_tuple).collect())
363   }
364 }
365
366 impl JoinView for CommentView {
367   type JoinTuple = CommentViewTuple;
368   fn from_tuple(a: Self::JoinTuple) -> Self {
369     Self {
370       comment: a.0,
371       creator: a.1,
372       post: a.2,
373       community: a.3,
374       counts: a.4,
375       creator_banned_from_community: a.5.is_some(),
376       subscribed: CommunityFollower::to_subscribed_type(&a.6),
377       saved: a.7.is_some(),
378       creator_blocked: a.8.is_some(),
379       my_vote: a.9,
380     }
381   }
382 }
383
384 #[cfg(test)]
385 mod tests {
386   use crate::comment_view::{
387     Comment,
388     CommentQuery,
389     CommentSortType,
390     CommentView,
391     Community,
392     DbPool,
393     LocalUser,
394     Person,
395     PersonBlock,
396     Post,
397   };
398   use lemmy_db_schema::{
399     aggregates::structs::CommentAggregates,
400     impls::actor_language::UNDETERMINED_ID,
401     newtypes::LanguageId,
402     source::{
403       actor_language::LocalUserLanguage,
404       comment::{CommentInsertForm, CommentLike, CommentLikeForm},
405       community::CommunityInsertForm,
406       instance::Instance,
407       language::Language,
408       local_user::LocalUserInsertForm,
409       person::PersonInsertForm,
410       person_block::PersonBlockForm,
411       post::PostInsertForm,
412     },
413     traits::{Blockable, Crud, Likeable},
414     utils::build_db_pool_for_tests,
415     SubscribedType,
416   };
417   use serial_test::serial;
418
419   struct Data {
420     inserted_instance: Instance,
421     inserted_comment_0: Comment,
422     inserted_comment_1: Comment,
423     inserted_comment_2: Comment,
424     inserted_post: Post,
425     inserted_person: Person,
426     inserted_local_user: LocalUser,
427     inserted_person_2: Person,
428     inserted_community: Community,
429   }
430
431   async fn init_data(pool: &DbPool) -> Data {
432     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
433       .await
434       .unwrap();
435
436     let new_person = PersonInsertForm::builder()
437       .name("timmy".into())
438       .public_key("pubkey".to_string())
439       .instance_id(inserted_instance.id)
440       .build();
441     let inserted_person = Person::create(pool, &new_person).await.unwrap();
442     let local_user_form = LocalUserInsertForm::builder()
443       .person_id(inserted_person.id)
444       .password_encrypted(String::new())
445       .build();
446     let inserted_local_user = LocalUser::create(pool, &local_user_form).await.unwrap();
447
448     let new_person_2 = PersonInsertForm::builder()
449       .name("sara".into())
450       .public_key("pubkey".to_string())
451       .instance_id(inserted_instance.id)
452       .build();
453     let inserted_person_2 = Person::create(pool, &new_person_2).await.unwrap();
454
455     let new_community = CommunityInsertForm::builder()
456       .name("test community 5".to_string())
457       .title("nada".to_owned())
458       .public_key("pubkey".to_string())
459       .instance_id(inserted_instance.id)
460       .build();
461
462     let inserted_community = Community::create(pool, &new_community).await.unwrap();
463
464     let new_post = PostInsertForm::builder()
465       .name("A test post 2".into())
466       .creator_id(inserted_person.id)
467       .community_id(inserted_community.id)
468       .build();
469
470     let inserted_post = Post::create(pool, &new_post).await.unwrap();
471     let english_id = Language::read_id_from_code(pool, Some("en")).await.unwrap();
472
473     // Create a comment tree with this hierarchy
474     //       0
475     //     \     \
476     //    1      2
477     //    \
478     //  3  4
479     //     \
480     //     5
481     let comment_form_0 = CommentInsertForm::builder()
482       .content("Comment 0".into())
483       .creator_id(inserted_person.id)
484       .post_id(inserted_post.id)
485       .language_id(english_id)
486       .build();
487
488     let inserted_comment_0 = Comment::create(pool, &comment_form_0, None).await.unwrap();
489
490     let comment_form_1 = CommentInsertForm::builder()
491       .content("Comment 1, A test blocked comment".into())
492       .creator_id(inserted_person_2.id)
493       .post_id(inserted_post.id)
494       .language_id(english_id)
495       .build();
496
497     let inserted_comment_1 = Comment::create(pool, &comment_form_1, Some(&inserted_comment_0.path))
498       .await
499       .unwrap();
500
501     let finnish_id = Language::read_id_from_code(pool, Some("fi")).await.unwrap();
502     let comment_form_2 = CommentInsertForm::builder()
503       .content("Comment 2".into())
504       .creator_id(inserted_person.id)
505       .post_id(inserted_post.id)
506       .language_id(finnish_id)
507       .build();
508
509     let inserted_comment_2 = Comment::create(pool, &comment_form_2, Some(&inserted_comment_0.path))
510       .await
511       .unwrap();
512
513     let comment_form_3 = CommentInsertForm::builder()
514       .content("Comment 3".into())
515       .creator_id(inserted_person.id)
516       .post_id(inserted_post.id)
517       .language_id(english_id)
518       .build();
519
520     let _inserted_comment_3 =
521       Comment::create(pool, &comment_form_3, Some(&inserted_comment_1.path))
522         .await
523         .unwrap();
524
525     let polish_id = Language::read_id_from_code(pool, Some("pl"))
526       .await
527       .unwrap()
528       .unwrap();
529     let comment_form_4 = CommentInsertForm::builder()
530       .content("Comment 4".into())
531       .creator_id(inserted_person.id)
532       .post_id(inserted_post.id)
533       .language_id(Some(polish_id))
534       .build();
535
536     let inserted_comment_4 = Comment::create(pool, &comment_form_4, Some(&inserted_comment_1.path))
537       .await
538       .unwrap();
539
540     let comment_form_5 = CommentInsertForm::builder()
541       .content("Comment 5".into())
542       .creator_id(inserted_person.id)
543       .post_id(inserted_post.id)
544       .build();
545
546     let _inserted_comment_5 =
547       Comment::create(pool, &comment_form_5, Some(&inserted_comment_4.path))
548         .await
549         .unwrap();
550
551     let timmy_blocks_sara_form = PersonBlockForm {
552       person_id: inserted_person.id,
553       target_id: inserted_person_2.id,
554     };
555
556     let inserted_block = PersonBlock::block(pool, &timmy_blocks_sara_form)
557       .await
558       .unwrap();
559
560     let expected_block = PersonBlock {
561       id: inserted_block.id,
562       person_id: inserted_person.id,
563       target_id: inserted_person_2.id,
564       published: inserted_block.published,
565     };
566     assert_eq!(expected_block, inserted_block);
567
568     let comment_like_form = CommentLikeForm {
569       comment_id: inserted_comment_0.id,
570       post_id: inserted_post.id,
571       person_id: inserted_person.id,
572       score: 1,
573     };
574
575     let _inserted_comment_like = CommentLike::like(pool, &comment_like_form).await.unwrap();
576
577     Data {
578       inserted_instance,
579       inserted_comment_0,
580       inserted_comment_1,
581       inserted_comment_2,
582       inserted_post,
583       inserted_person,
584       inserted_local_user,
585       inserted_person_2,
586       inserted_community,
587     }
588   }
589
590   #[tokio::test]
591   #[serial]
592   async fn test_crud() {
593     let pool = &build_db_pool_for_tests().await;
594     let data = init_data(pool).await;
595
596     let expected_comment_view_no_person = expected_comment_view(&data, pool).await;
597
598     let mut expected_comment_view_with_person = expected_comment_view_no_person.clone();
599     expected_comment_view_with_person.my_vote = Some(1);
600
601     let read_comment_views_no_person = CommentQuery::builder()
602       .pool(pool)
603       .sort(Some(CommentSortType::Hot))
604       .post_id(Some(data.inserted_post.id))
605       .build()
606       .list()
607       .await
608       .unwrap();
609
610     assert_eq!(
611       expected_comment_view_no_person,
612       read_comment_views_no_person[0]
613     );
614
615     let read_comment_views_with_person = CommentQuery::builder()
616       .pool(pool)
617       .sort(Some(CommentSortType::Hot))
618       .post_id(Some(data.inserted_post.id))
619       .local_user(Some(&data.inserted_local_user))
620       .build()
621       .list()
622       .await
623       .unwrap();
624
625     assert_eq!(
626       expected_comment_view_with_person,
627       read_comment_views_with_person[0]
628     );
629
630     // Make sure its 1, not showing the blocked comment
631     assert_eq!(5, read_comment_views_with_person.len());
632
633     let read_comment_from_blocked_person = CommentView::read(
634       pool,
635       data.inserted_comment_1.id,
636       Some(data.inserted_person.id),
637     )
638     .await
639     .unwrap();
640
641     // Make sure block set the creator blocked
642     assert!(read_comment_from_blocked_person.creator_blocked);
643
644     cleanup(data, pool).await;
645   }
646
647   #[tokio::test]
648   #[serial]
649   async fn test_comment_tree() {
650     let pool = &build_db_pool_for_tests().await;
651     let data = init_data(pool).await;
652
653     let top_path = data.inserted_comment_0.path.clone();
654     let read_comment_views_top_path = CommentQuery::builder()
655       .pool(pool)
656       .post_id(Some(data.inserted_post.id))
657       .parent_path(Some(top_path))
658       .build()
659       .list()
660       .await
661       .unwrap();
662
663     let child_path = data.inserted_comment_1.path.clone();
664     let read_comment_views_child_path = CommentQuery::builder()
665       .pool(pool)
666       .post_id(Some(data.inserted_post.id))
667       .parent_path(Some(child_path))
668       .build()
669       .list()
670       .await
671       .unwrap();
672
673     // Make sure the comment parent-limited fetch is correct
674     assert_eq!(6, read_comment_views_top_path.len());
675     assert_eq!(4, read_comment_views_child_path.len());
676
677     // Make sure it contains the parent, but not the comment from the other tree
678     let child_comments = read_comment_views_child_path
679       .into_iter()
680       .map(|c| c.comment)
681       .collect::<Vec<Comment>>();
682     assert!(child_comments.contains(&data.inserted_comment_1));
683     assert!(!child_comments.contains(&data.inserted_comment_2));
684
685     let read_comment_views_top_max_depth = CommentQuery::builder()
686       .pool(pool)
687       .post_id(Some(data.inserted_post.id))
688       .max_depth(Some(1))
689       .build()
690       .list()
691       .await
692       .unwrap();
693
694     // Make sure a depth limited one only has the top comment
695     assert_eq!(
696       expected_comment_view(&data, pool).await,
697       read_comment_views_top_max_depth[0]
698     );
699     assert_eq!(1, read_comment_views_top_max_depth.len());
700
701     let child_path = data.inserted_comment_1.path.clone();
702     let read_comment_views_parent_max_depth = CommentQuery::builder()
703       .pool(pool)
704       .post_id(Some(data.inserted_post.id))
705       .parent_path(Some(child_path))
706       .max_depth(Some(1))
707       .sort(Some(CommentSortType::New))
708       .build()
709       .list()
710       .await
711       .unwrap();
712
713     // Make sure a depth limited one, and given child comment 1, has 3
714     assert!(read_comment_views_parent_max_depth[2]
715       .comment
716       .content
717       .eq("Comment 3"));
718     assert_eq!(3, read_comment_views_parent_max_depth.len());
719
720     cleanup(data, pool).await;
721   }
722
723   #[tokio::test]
724   #[serial]
725   async fn test_languages() {
726     let pool = &build_db_pool_for_tests().await;
727     let data = init_data(pool).await;
728
729     // by default, user has all languages enabled and should see all comments
730     // (except from blocked user)
731     let all_languages = CommentQuery::builder()
732       .pool(pool)
733       .local_user(Some(&data.inserted_local_user))
734       .build()
735       .list()
736       .await
737       .unwrap();
738     assert_eq!(5, all_languages.len());
739
740     // change user lang to finnish, should only show one post in finnish and one undetermined
741     let finnish_id = Language::read_id_from_code(pool, Some("fi"))
742       .await
743       .unwrap()
744       .unwrap();
745     LocalUserLanguage::update(pool, vec![finnish_id], data.inserted_local_user.id)
746       .await
747       .unwrap();
748     let finnish_comments = CommentQuery::builder()
749       .pool(pool)
750       .local_user(Some(&data.inserted_local_user))
751       .build()
752       .list()
753       .await
754       .unwrap();
755     assert_eq!(2, finnish_comments.len());
756     let finnish_comment = finnish_comments
757       .iter()
758       .find(|c| c.comment.language_id == finnish_id);
759     assert!(finnish_comment.is_some());
760     assert_eq!(
761       data.inserted_comment_2.content,
762       finnish_comment.unwrap().comment.content
763     );
764
765     // now show all comments with undetermined language (which is the default value)
766     LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id)
767       .await
768       .unwrap();
769     let undetermined_comment = CommentQuery::builder()
770       .pool(pool)
771       .local_user(Some(&data.inserted_local_user))
772       .build()
773       .list()
774       .await
775       .unwrap();
776     assert_eq!(1, undetermined_comment.len());
777
778     cleanup(data, pool).await;
779   }
780
781   async fn cleanup(data: Data, pool: &DbPool) {
782     CommentLike::remove(pool, data.inserted_person.id, data.inserted_comment_0.id)
783       .await
784       .unwrap();
785     Comment::delete(pool, data.inserted_comment_0.id)
786       .await
787       .unwrap();
788     Comment::delete(pool, data.inserted_comment_1.id)
789       .await
790       .unwrap();
791     Post::delete(pool, data.inserted_post.id).await.unwrap();
792     Community::delete(pool, data.inserted_community.id)
793       .await
794       .unwrap();
795     Person::delete(pool, data.inserted_person.id).await.unwrap();
796     Person::delete(pool, data.inserted_person_2.id)
797       .await
798       .unwrap();
799     Instance::delete(pool, data.inserted_instance.id)
800       .await
801       .unwrap();
802   }
803
804   async fn expected_comment_view(data: &Data, pool: &DbPool) -> CommentView {
805     let agg = CommentAggregates::read(pool, data.inserted_comment_0.id)
806       .await
807       .unwrap();
808     CommentView {
809       creator_banned_from_community: false,
810       my_vote: None,
811       subscribed: SubscribedType::NotSubscribed,
812       saved: false,
813       creator_blocked: false,
814       comment: Comment {
815         id: data.inserted_comment_0.id,
816         content: "Comment 0".into(),
817         creator_id: data.inserted_person.id,
818         post_id: data.inserted_post.id,
819         removed: false,
820         deleted: false,
821         published: data.inserted_comment_0.published,
822         ap_id: data.inserted_comment_0.ap_id.clone(),
823         updated: None,
824         local: true,
825         distinguished: false,
826         path: data.inserted_comment_0.clone().path,
827         language_id: LanguageId(37),
828       },
829       creator: Person {
830         id: data.inserted_person.id,
831         name: "timmy".into(),
832         display_name: None,
833         published: data.inserted_person.published,
834         avatar: None,
835         actor_id: data.inserted_person.actor_id.clone(),
836         local: true,
837         banned: false,
838         deleted: false,
839         admin: false,
840         bot_account: false,
841         bio: None,
842         banner: None,
843         updated: None,
844         inbox_url: data.inserted_person.inbox_url.clone(),
845         shared_inbox_url: None,
846         matrix_user_id: None,
847         ban_expires: None,
848         instance_id: data.inserted_instance.id,
849         private_key: data.inserted_person.private_key.clone(),
850         public_key: data.inserted_person.public_key.clone(),
851         last_refreshed_at: data.inserted_person.last_refreshed_at,
852       },
853       post: Post {
854         id: data.inserted_post.id,
855         name: data.inserted_post.name.clone(),
856         creator_id: data.inserted_person.id,
857         url: None,
858         body: None,
859         published: data.inserted_post.published,
860         updated: None,
861         community_id: data.inserted_community.id,
862         removed: false,
863         deleted: false,
864         locked: false,
865         nsfw: false,
866         embed_title: None,
867         embed_description: None,
868         embed_video_url: None,
869         thumbnail_url: None,
870         ap_id: data.inserted_post.ap_id.clone(),
871         local: true,
872         language_id: Default::default(),
873         featured_community: false,
874         featured_local: false,
875       },
876       community: Community {
877         id: data.inserted_community.id,
878         name: "test community 5".to_string(),
879         icon: None,
880         removed: false,
881         deleted: false,
882         nsfw: false,
883         actor_id: data.inserted_community.actor_id.clone(),
884         local: true,
885         title: "nada".to_owned(),
886         description: None,
887         updated: None,
888         banner: None,
889         hidden: false,
890         posting_restricted_to_mods: false,
891         published: data.inserted_community.published,
892         instance_id: data.inserted_instance.id,
893         private_key: data.inserted_community.private_key.clone(),
894         public_key: data.inserted_community.public_key.clone(),
895         last_refreshed_at: data.inserted_community.last_refreshed_at,
896         followers_url: data.inserted_community.followers_url.clone(),
897         inbox_url: data.inserted_community.inbox_url.clone(),
898         shared_inbox_url: data.inserted_community.shared_inbox_url.clone(),
899         moderators_url: data.inserted_community.moderators_url.clone(),
900         featured_url: data.inserted_community.featured_url.clone(),
901       },
902       counts: CommentAggregates {
903         id: agg.id,
904         comment_id: data.inserted_comment_0.id,
905         score: 1,
906         upvotes: 1,
907         downvotes: 0,
908         published: agg.published,
909         child_count: 5,
910       },
911     }
912   }
913 }