]> Untitled Git - lemmy.git/blob - crates/db_views/src/comment_view.rs
f3b1f83a87e4a7d3b9cc3666879fcc4a9e82d610
[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::{fuzzy_search, get_conn, limit_and_offset, 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       if self.post_id.is_none() {
316         query = query.filter(community_block::person_id.is_null());
317       }
318       query = query.filter(person_block::person_id.is_null());
319     }
320
321     // A Max depth given means its a tree fetch
322     let (limit, offset) = if let Some(max_depth) = self.max_depth {
323       let depth_limit = if let Some(parent_path) = self.parent_path.as_ref() {
324         parent_path.0.split('.').count() as i32 + max_depth
325         // Add one because of root "0"
326       } else {
327         max_depth + 1
328       };
329
330       query = query.filter(nlevel(comment::path).le(depth_limit));
331
332       // Always order by the parent path first
333       query = query.order_by(subpath(comment::path, 0, -1));
334
335       // TODO limit question. Limiting does not work for comment threads ATM, only max_depth
336       // For now, don't do any limiting for tree fetches
337       // https://stackoverflow.com/questions/72983614/postgres-ltree-how-to-limit-the-max-number-of-children-at-any-given-level
338
339       // Don't use the regular error-checking one, many more comments must ofter be fetched.
340       // This does not work for comment trees, and the limit should be manually set to a high number
341       //
342       // If a max depth is given, then you know its a tree fetch, and limits should be ignored
343       // TODO a kludge to prevent attacks. Limit comments to 300 for now.
344       // (i64::MAX, 0)
345       (300, 0)
346     } else {
347       // limit_and_offset_unlimited(self.page, self.limit)
348       limit_and_offset(self.page, self.limit)?
349     };
350
351     query = match self.sort.unwrap_or(CommentSortType::Hot) {
352       CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()),
353       CommentSortType::New => query.then_order_by(comment::published.desc()),
354       CommentSortType::Old => query.then_order_by(comment::published.asc()),
355       CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
356     };
357
358     // Note: deleted and removed comments are done on the front side
359     let res = query
360       .limit(limit)
361       .offset(offset)
362       .load::<CommentViewTuple>(conn)
363       .await?;
364
365     Ok(res.into_iter().map(CommentView::from_tuple).collect())
366   }
367 }
368
369 impl JoinView for CommentView {
370   type JoinTuple = CommentViewTuple;
371   fn from_tuple(a: Self::JoinTuple) -> Self {
372     Self {
373       comment: a.0,
374       creator: a.1,
375       post: a.2,
376       community: a.3,
377       counts: a.4,
378       creator_banned_from_community: a.5.is_some(),
379       subscribed: CommunityFollower::to_subscribed_type(&a.6),
380       saved: a.7.is_some(),
381       creator_blocked: a.8.is_some(),
382       my_vote: a.9,
383     }
384   }
385 }
386
387 #[cfg(test)]
388 mod tests {
389   use crate::comment_view::{
390     Comment,
391     CommentQuery,
392     CommentSortType,
393     CommentView,
394     Community,
395     DbPool,
396     LocalUser,
397     Person,
398     PersonBlock,
399     Post,
400   };
401   use lemmy_db_schema::{
402     aggregates::structs::CommentAggregates,
403     impls::actor_language::UNDETERMINED_ID,
404     newtypes::LanguageId,
405     source::{
406       actor_language::LocalUserLanguage,
407       comment::{CommentInsertForm, CommentLike, CommentLikeForm},
408       community::CommunityInsertForm,
409       instance::Instance,
410       language::Language,
411       local_user::LocalUserInsertForm,
412       person::PersonInsertForm,
413       person_block::PersonBlockForm,
414       post::PostInsertForm,
415     },
416     traits::{Blockable, Crud, Likeable},
417     utils::build_db_pool_for_tests,
418     SubscribedType,
419   };
420   use serial_test::serial;
421
422   struct Data {
423     inserted_instance: Instance,
424     inserted_comment_0: Comment,
425     inserted_comment_1: Comment,
426     inserted_comment_2: Comment,
427     inserted_post: Post,
428     inserted_person: Person,
429     inserted_local_user: LocalUser,
430     inserted_person_2: Person,
431     inserted_community: Community,
432   }
433
434   async fn init_data(pool: &DbPool) -> Data {
435     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
436       .await
437       .unwrap();
438
439     let new_person = PersonInsertForm::builder()
440       .name("timmy".into())
441       .public_key("pubkey".to_string())
442       .instance_id(inserted_instance.id)
443       .build();
444     let inserted_person = Person::create(pool, &new_person).await.unwrap();
445     let local_user_form = LocalUserInsertForm::builder()
446       .person_id(inserted_person.id)
447       .password_encrypted(String::new())
448       .build();
449     let inserted_local_user = LocalUser::create(pool, &local_user_form).await.unwrap();
450
451     let new_person_2 = PersonInsertForm::builder()
452       .name("sara".into())
453       .public_key("pubkey".to_string())
454       .instance_id(inserted_instance.id)
455       .build();
456     let inserted_person_2 = Person::create(pool, &new_person_2).await.unwrap();
457
458     let new_community = CommunityInsertForm::builder()
459       .name("test community 5".to_string())
460       .title("nada".to_owned())
461       .public_key("pubkey".to_string())
462       .instance_id(inserted_instance.id)
463       .build();
464
465     let inserted_community = Community::create(pool, &new_community).await.unwrap();
466
467     let new_post = PostInsertForm::builder()
468       .name("A test post 2".into())
469       .creator_id(inserted_person.id)
470       .community_id(inserted_community.id)
471       .build();
472
473     let inserted_post = Post::create(pool, &new_post).await.unwrap();
474     let english_id = Language::read_id_from_code(pool, Some("en")).await.unwrap();
475
476     // Create a comment tree with this hierarchy
477     //       0
478     //     \     \
479     //    1      2
480     //    \
481     //  3  4
482     //     \
483     //     5
484     let comment_form_0 = CommentInsertForm::builder()
485       .content("Comment 0".into())
486       .creator_id(inserted_person.id)
487       .post_id(inserted_post.id)
488       .language_id(english_id)
489       .build();
490
491     let inserted_comment_0 = Comment::create(pool, &comment_form_0, None).await.unwrap();
492
493     let comment_form_1 = CommentInsertForm::builder()
494       .content("Comment 1, A test blocked comment".into())
495       .creator_id(inserted_person_2.id)
496       .post_id(inserted_post.id)
497       .language_id(english_id)
498       .build();
499
500     let inserted_comment_1 = Comment::create(pool, &comment_form_1, Some(&inserted_comment_0.path))
501       .await
502       .unwrap();
503
504     let finnish_id = Language::read_id_from_code(pool, Some("fi")).await.unwrap();
505     let comment_form_2 = CommentInsertForm::builder()
506       .content("Comment 2".into())
507       .creator_id(inserted_person.id)
508       .post_id(inserted_post.id)
509       .language_id(finnish_id)
510       .build();
511
512     let inserted_comment_2 = Comment::create(pool, &comment_form_2, Some(&inserted_comment_0.path))
513       .await
514       .unwrap();
515
516     let comment_form_3 = CommentInsertForm::builder()
517       .content("Comment 3".into())
518       .creator_id(inserted_person.id)
519       .post_id(inserted_post.id)
520       .language_id(english_id)
521       .build();
522
523     let _inserted_comment_3 =
524       Comment::create(pool, &comment_form_3, Some(&inserted_comment_1.path))
525         .await
526         .unwrap();
527
528     let polish_id = Language::read_id_from_code(pool, Some("pl"))
529       .await
530       .unwrap()
531       .unwrap();
532     let comment_form_4 = CommentInsertForm::builder()
533       .content("Comment 4".into())
534       .creator_id(inserted_person.id)
535       .post_id(inserted_post.id)
536       .language_id(Some(polish_id))
537       .build();
538
539     let inserted_comment_4 = Comment::create(pool, &comment_form_4, Some(&inserted_comment_1.path))
540       .await
541       .unwrap();
542
543     let comment_form_5 = CommentInsertForm::builder()
544       .content("Comment 5".into())
545       .creator_id(inserted_person.id)
546       .post_id(inserted_post.id)
547       .build();
548
549     let _inserted_comment_5 =
550       Comment::create(pool, &comment_form_5, Some(&inserted_comment_4.path))
551         .await
552         .unwrap();
553
554     let timmy_blocks_sara_form = PersonBlockForm {
555       person_id: inserted_person.id,
556       target_id: inserted_person_2.id,
557     };
558
559     let inserted_block = PersonBlock::block(pool, &timmy_blocks_sara_form)
560       .await
561       .unwrap();
562
563     let expected_block = PersonBlock {
564       id: inserted_block.id,
565       person_id: inserted_person.id,
566       target_id: inserted_person_2.id,
567       published: inserted_block.published,
568     };
569     assert_eq!(expected_block, inserted_block);
570
571     let comment_like_form = CommentLikeForm {
572       comment_id: inserted_comment_0.id,
573       post_id: inserted_post.id,
574       person_id: inserted_person.id,
575       score: 1,
576     };
577
578     let _inserted_comment_like = CommentLike::like(pool, &comment_like_form).await.unwrap();
579
580     Data {
581       inserted_instance,
582       inserted_comment_0,
583       inserted_comment_1,
584       inserted_comment_2,
585       inserted_post,
586       inserted_person,
587       inserted_local_user,
588       inserted_person_2,
589       inserted_community,
590     }
591   }
592
593   #[tokio::test]
594   #[serial]
595   async fn test_crud() {
596     let pool = &build_db_pool_for_tests().await;
597     let data = init_data(pool).await;
598
599     let expected_comment_view_no_person = expected_comment_view(&data, pool).await;
600
601     let mut expected_comment_view_with_person = expected_comment_view_no_person.clone();
602     expected_comment_view_with_person.my_vote = Some(1);
603
604     let read_comment_views_no_person = CommentQuery::builder()
605       .pool(pool)
606       .sort(Some(CommentSortType::Old))
607       .post_id(Some(data.inserted_post.id))
608       .build()
609       .list()
610       .await
611       .unwrap();
612
613     assert_eq!(
614       expected_comment_view_no_person,
615       read_comment_views_no_person[0]
616     );
617
618     let read_comment_views_with_person = CommentQuery::builder()
619       .pool(pool)
620       .sort(Some(CommentSortType::Old))
621       .post_id(Some(data.inserted_post.id))
622       .local_user(Some(&data.inserted_local_user))
623       .build()
624       .list()
625       .await
626       .unwrap();
627
628     assert_eq!(
629       expected_comment_view_with_person,
630       read_comment_views_with_person[0]
631     );
632
633     // Make sure its 1, not showing the blocked comment
634     assert_eq!(5, read_comment_views_with_person.len());
635
636     let read_comment_from_blocked_person = CommentView::read(
637       pool,
638       data.inserted_comment_1.id,
639       Some(data.inserted_person.id),
640     )
641     .await
642     .unwrap();
643
644     // Make sure block set the creator blocked
645     assert!(read_comment_from_blocked_person.creator_blocked);
646
647     cleanup(data, pool).await;
648   }
649
650   #[tokio::test]
651   #[serial]
652   async fn test_comment_tree() {
653     let pool = &build_db_pool_for_tests().await;
654     let data = init_data(pool).await;
655
656     let top_path = data.inserted_comment_0.path.clone();
657     let read_comment_views_top_path = CommentQuery::builder()
658       .pool(pool)
659       .post_id(Some(data.inserted_post.id))
660       .parent_path(Some(top_path))
661       .build()
662       .list()
663       .await
664       .unwrap();
665
666     let child_path = data.inserted_comment_1.path.clone();
667     let read_comment_views_child_path = CommentQuery::builder()
668       .pool(pool)
669       .post_id(Some(data.inserted_post.id))
670       .parent_path(Some(child_path))
671       .build()
672       .list()
673       .await
674       .unwrap();
675
676     // Make sure the comment parent-limited fetch is correct
677     assert_eq!(6, read_comment_views_top_path.len());
678     assert_eq!(4, read_comment_views_child_path.len());
679
680     // Make sure it contains the parent, but not the comment from the other tree
681     let child_comments = read_comment_views_child_path
682       .into_iter()
683       .map(|c| c.comment)
684       .collect::<Vec<Comment>>();
685     assert!(child_comments.contains(&data.inserted_comment_1));
686     assert!(!child_comments.contains(&data.inserted_comment_2));
687
688     let read_comment_views_top_max_depth = CommentQuery::builder()
689       .pool(pool)
690       .post_id(Some(data.inserted_post.id))
691       .max_depth(Some(1))
692       .build()
693       .list()
694       .await
695       .unwrap();
696
697     // Make sure a depth limited one only has the top comment
698     assert_eq!(
699       expected_comment_view(&data, pool).await,
700       read_comment_views_top_max_depth[0]
701     );
702     assert_eq!(1, read_comment_views_top_max_depth.len());
703
704     let child_path = data.inserted_comment_1.path.clone();
705     let read_comment_views_parent_max_depth = CommentQuery::builder()
706       .pool(pool)
707       .post_id(Some(data.inserted_post.id))
708       .parent_path(Some(child_path))
709       .max_depth(Some(1))
710       .sort(Some(CommentSortType::New))
711       .build()
712       .list()
713       .await
714       .unwrap();
715
716     // Make sure a depth limited one, and given child comment 1, has 3
717     assert!(read_comment_views_parent_max_depth[2]
718       .comment
719       .content
720       .eq("Comment 3"));
721     assert_eq!(3, read_comment_views_parent_max_depth.len());
722
723     cleanup(data, pool).await;
724   }
725
726   #[tokio::test]
727   #[serial]
728   async fn test_languages() {
729     let pool = &build_db_pool_for_tests().await;
730     let data = init_data(pool).await;
731
732     // by default, user has all languages enabled and should see all comments
733     // (except from blocked user)
734     let all_languages = CommentQuery::builder()
735       .pool(pool)
736       .local_user(Some(&data.inserted_local_user))
737       .build()
738       .list()
739       .await
740       .unwrap();
741     assert_eq!(5, all_languages.len());
742
743     // change user lang to finnish, should only show one post in finnish and one undetermined
744     let finnish_id = Language::read_id_from_code(pool, Some("fi"))
745       .await
746       .unwrap()
747       .unwrap();
748     LocalUserLanguage::update(pool, vec![finnish_id], data.inserted_local_user.id)
749       .await
750       .unwrap();
751     let finnish_comments = CommentQuery::builder()
752       .pool(pool)
753       .local_user(Some(&data.inserted_local_user))
754       .build()
755       .list()
756       .await
757       .unwrap();
758     assert_eq!(2, finnish_comments.len());
759     let finnish_comment = finnish_comments
760       .iter()
761       .find(|c| c.comment.language_id == finnish_id);
762     assert!(finnish_comment.is_some());
763     assert_eq!(
764       data.inserted_comment_2.content,
765       finnish_comment.unwrap().comment.content
766     );
767
768     // now show all comments with undetermined language (which is the default value)
769     LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id)
770       .await
771       .unwrap();
772     let undetermined_comment = CommentQuery::builder()
773       .pool(pool)
774       .local_user(Some(&data.inserted_local_user))
775       .build()
776       .list()
777       .await
778       .unwrap();
779     assert_eq!(1, undetermined_comment.len());
780
781     cleanup(data, pool).await;
782   }
783
784   async fn cleanup(data: Data, pool: &DbPool) {
785     CommentLike::remove(pool, data.inserted_person.id, data.inserted_comment_0.id)
786       .await
787       .unwrap();
788     Comment::delete(pool, data.inserted_comment_0.id)
789       .await
790       .unwrap();
791     Comment::delete(pool, data.inserted_comment_1.id)
792       .await
793       .unwrap();
794     Post::delete(pool, data.inserted_post.id).await.unwrap();
795     Community::delete(pool, data.inserted_community.id)
796       .await
797       .unwrap();
798     Person::delete(pool, data.inserted_person.id).await.unwrap();
799     Person::delete(pool, data.inserted_person_2.id)
800       .await
801       .unwrap();
802     Instance::delete(pool, data.inserted_instance.id)
803       .await
804       .unwrap();
805   }
806
807   async fn expected_comment_view(data: &Data, pool: &DbPool) -> CommentView {
808     let agg = CommentAggregates::read(pool, data.inserted_comment_0.id)
809       .await
810       .unwrap();
811     CommentView {
812       creator_banned_from_community: false,
813       my_vote: None,
814       subscribed: SubscribedType::NotSubscribed,
815       saved: false,
816       creator_blocked: false,
817       comment: Comment {
818         id: data.inserted_comment_0.id,
819         content: "Comment 0".into(),
820         creator_id: data.inserted_person.id,
821         post_id: data.inserted_post.id,
822         removed: false,
823         deleted: false,
824         published: data.inserted_comment_0.published,
825         ap_id: data.inserted_comment_0.ap_id.clone(),
826         updated: None,
827         local: true,
828         distinguished: false,
829         path: data.inserted_comment_0.clone().path,
830         language_id: LanguageId(37),
831       },
832       creator: Person {
833         id: data.inserted_person.id,
834         name: "timmy".into(),
835         display_name: None,
836         published: data.inserted_person.published,
837         avatar: None,
838         actor_id: data.inserted_person.actor_id.clone(),
839         local: true,
840         banned: false,
841         deleted: false,
842         admin: false,
843         bot_account: false,
844         bio: None,
845         banner: None,
846         updated: None,
847         inbox_url: data.inserted_person.inbox_url.clone(),
848         shared_inbox_url: None,
849         matrix_user_id: None,
850         ban_expires: None,
851         instance_id: data.inserted_instance.id,
852         private_key: data.inserted_person.private_key.clone(),
853         public_key: data.inserted_person.public_key.clone(),
854         last_refreshed_at: data.inserted_person.last_refreshed_at,
855       },
856       post: Post {
857         id: data.inserted_post.id,
858         name: data.inserted_post.name.clone(),
859         creator_id: data.inserted_person.id,
860         url: None,
861         body: None,
862         published: data.inserted_post.published,
863         updated: None,
864         community_id: data.inserted_community.id,
865         removed: false,
866         deleted: false,
867         locked: false,
868         nsfw: false,
869         embed_title: None,
870         embed_description: None,
871         embed_video_url: None,
872         thumbnail_url: None,
873         ap_id: data.inserted_post.ap_id.clone(),
874         local: true,
875         language_id: Default::default(),
876         featured_community: false,
877         featured_local: false,
878       },
879       community: Community {
880         id: data.inserted_community.id,
881         name: "test community 5".to_string(),
882         icon: None,
883         removed: false,
884         deleted: false,
885         nsfw: false,
886         actor_id: data.inserted_community.actor_id.clone(),
887         local: true,
888         title: "nada".to_owned(),
889         description: None,
890         updated: None,
891         banner: None,
892         hidden: false,
893         posting_restricted_to_mods: false,
894         published: data.inserted_community.published,
895         instance_id: data.inserted_instance.id,
896         private_key: data.inserted_community.private_key.clone(),
897         public_key: data.inserted_community.public_key.clone(),
898         last_refreshed_at: data.inserted_community.last_refreshed_at,
899         followers_url: data.inserted_community.followers_url.clone(),
900         inbox_url: data.inserted_community.inbox_url.clone(),
901         shared_inbox_url: data.inserted_community.shared_inbox_url.clone(),
902         moderators_url: data.inserted_community.moderators_url.clone(),
903         featured_url: data.inserted_community.featured_url.clone(),
904       },
905       counts: CommentAggregates {
906         id: agg.id,
907         comment_id: data.inserted_comment_0.id,
908         score: 1,
909         upvotes: 1,
910         downvotes: 0,
911         published: agg.published,
912         child_count: 5,
913         hot_rank: 1728,
914       },
915     }
916   }
917 }