]> Untitled Git - lemmy.git/blob - crates/db_views/src/comment_view.rs
Make functions work with both connection and pool (#3420)
[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: &mut 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, 'b: 'a> {
162   #[builder(!default)]
163   pool: &'a mut DbPool<'b>,
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, 'b: 'a> CommentQuery<'a, 'b> {
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: &mut 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 pool = &mut pool.into();
598     let data = init_data(pool).await;
599
600     let expected_comment_view_no_person = expected_comment_view(&data, pool).await;
601
602     let mut expected_comment_view_with_person = expected_comment_view_no_person.clone();
603     expected_comment_view_with_person.my_vote = Some(1);
604
605     let read_comment_views_no_person = CommentQuery::builder()
606       .pool(pool)
607       .sort(Some(CommentSortType::Old))
608       .post_id(Some(data.inserted_post.id))
609       .build()
610       .list()
611       .await
612       .unwrap();
613
614     assert_eq!(
615       expected_comment_view_no_person,
616       read_comment_views_no_person[0]
617     );
618
619     let read_comment_views_with_person = CommentQuery::builder()
620       .pool(pool)
621       .sort(Some(CommentSortType::Old))
622       .post_id(Some(data.inserted_post.id))
623       .local_user(Some(&data.inserted_local_user))
624       .build()
625       .list()
626       .await
627       .unwrap();
628
629     assert_eq!(
630       expected_comment_view_with_person,
631       read_comment_views_with_person[0]
632     );
633
634     // Make sure its 1, not showing the blocked comment
635     assert_eq!(5, read_comment_views_with_person.len());
636
637     let read_comment_from_blocked_person = CommentView::read(
638       pool,
639       data.inserted_comment_1.id,
640       Some(data.inserted_person.id),
641     )
642     .await
643     .unwrap();
644
645     // Make sure block set the creator blocked
646     assert!(read_comment_from_blocked_person.creator_blocked);
647
648     cleanup(data, pool).await;
649   }
650
651   #[tokio::test]
652   #[serial]
653   async fn test_comment_tree() {
654     let pool = &build_db_pool_for_tests().await;
655     let pool = &mut pool.into();
656     let data = init_data(pool).await;
657
658     let top_path = data.inserted_comment_0.path.clone();
659     let read_comment_views_top_path = CommentQuery::builder()
660       .pool(pool)
661       .post_id(Some(data.inserted_post.id))
662       .parent_path(Some(top_path))
663       .build()
664       .list()
665       .await
666       .unwrap();
667
668     let child_path = data.inserted_comment_1.path.clone();
669     let read_comment_views_child_path = CommentQuery::builder()
670       .pool(pool)
671       .post_id(Some(data.inserted_post.id))
672       .parent_path(Some(child_path))
673       .build()
674       .list()
675       .await
676       .unwrap();
677
678     // Make sure the comment parent-limited fetch is correct
679     assert_eq!(6, read_comment_views_top_path.len());
680     assert_eq!(4, read_comment_views_child_path.len());
681
682     // Make sure it contains the parent, but not the comment from the other tree
683     let child_comments = read_comment_views_child_path
684       .into_iter()
685       .map(|c| c.comment)
686       .collect::<Vec<Comment>>();
687     assert!(child_comments.contains(&data.inserted_comment_1));
688     assert!(!child_comments.contains(&data.inserted_comment_2));
689
690     let read_comment_views_top_max_depth = CommentQuery::builder()
691       .pool(pool)
692       .post_id(Some(data.inserted_post.id))
693       .max_depth(Some(1))
694       .build()
695       .list()
696       .await
697       .unwrap();
698
699     // Make sure a depth limited one only has the top comment
700     assert_eq!(
701       expected_comment_view(&data, pool).await,
702       read_comment_views_top_max_depth[0]
703     );
704     assert_eq!(1, read_comment_views_top_max_depth.len());
705
706     let child_path = data.inserted_comment_1.path.clone();
707     let read_comment_views_parent_max_depth = CommentQuery::builder()
708       .pool(pool)
709       .post_id(Some(data.inserted_post.id))
710       .parent_path(Some(child_path))
711       .max_depth(Some(1))
712       .sort(Some(CommentSortType::New))
713       .build()
714       .list()
715       .await
716       .unwrap();
717
718     // Make sure a depth limited one, and given child comment 1, has 3
719     assert!(read_comment_views_parent_max_depth[2]
720       .comment
721       .content
722       .eq("Comment 3"));
723     assert_eq!(3, read_comment_views_parent_max_depth.len());
724
725     cleanup(data, pool).await;
726   }
727
728   #[tokio::test]
729   #[serial]
730   async fn test_languages() {
731     let pool = &build_db_pool_for_tests().await;
732     let pool = &mut pool.into();
733     let data = init_data(pool).await;
734
735     // by default, user has all languages enabled and should see all comments
736     // (except from blocked user)
737     let all_languages = CommentQuery::builder()
738       .pool(pool)
739       .local_user(Some(&data.inserted_local_user))
740       .build()
741       .list()
742       .await
743       .unwrap();
744     assert_eq!(5, all_languages.len());
745
746     // change user lang to finnish, should only show one post in finnish and one undetermined
747     let finnish_id = Language::read_id_from_code(pool, Some("fi"))
748       .await
749       .unwrap()
750       .unwrap();
751     LocalUserLanguage::update(pool, vec![finnish_id], data.inserted_local_user.id)
752       .await
753       .unwrap();
754     let finnish_comments = CommentQuery::builder()
755       .pool(pool)
756       .local_user(Some(&data.inserted_local_user))
757       .build()
758       .list()
759       .await
760       .unwrap();
761     assert_eq!(2, finnish_comments.len());
762     let finnish_comment = finnish_comments
763       .iter()
764       .find(|c| c.comment.language_id == finnish_id);
765     assert!(finnish_comment.is_some());
766     assert_eq!(
767       data.inserted_comment_2.content,
768       finnish_comment.unwrap().comment.content
769     );
770
771     // now show all comments with undetermined language (which is the default value)
772     LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id)
773       .await
774       .unwrap();
775     let undetermined_comment = CommentQuery::builder()
776       .pool(pool)
777       .local_user(Some(&data.inserted_local_user))
778       .build()
779       .list()
780       .await
781       .unwrap();
782     assert_eq!(1, undetermined_comment.len());
783
784     cleanup(data, pool).await;
785   }
786
787   async fn cleanup(data: Data, pool: &mut DbPool<'_>) {
788     CommentLike::remove(pool, data.inserted_person.id, data.inserted_comment_0.id)
789       .await
790       .unwrap();
791     Comment::delete(pool, data.inserted_comment_0.id)
792       .await
793       .unwrap();
794     Comment::delete(pool, data.inserted_comment_1.id)
795       .await
796       .unwrap();
797     Post::delete(pool, data.inserted_post.id).await.unwrap();
798     Community::delete(pool, data.inserted_community.id)
799       .await
800       .unwrap();
801     Person::delete(pool, data.inserted_person.id).await.unwrap();
802     Person::delete(pool, data.inserted_person_2.id)
803       .await
804       .unwrap();
805     Instance::delete(pool, data.inserted_instance.id)
806       .await
807       .unwrap();
808   }
809
810   async fn expected_comment_view(data: &Data, pool: &mut DbPool<'_>) -> CommentView {
811     let agg = CommentAggregates::read(pool, data.inserted_comment_0.id)
812       .await
813       .unwrap();
814     CommentView {
815       creator_banned_from_community: false,
816       my_vote: None,
817       subscribed: SubscribedType::NotSubscribed,
818       saved: false,
819       creator_blocked: false,
820       comment: Comment {
821         id: data.inserted_comment_0.id,
822         content: "Comment 0".into(),
823         creator_id: data.inserted_person.id,
824         post_id: data.inserted_post.id,
825         removed: false,
826         deleted: false,
827         published: data.inserted_comment_0.published,
828         ap_id: data.inserted_comment_0.ap_id.clone(),
829         updated: None,
830         local: true,
831         distinguished: false,
832         path: data.inserted_comment_0.clone().path,
833         language_id: LanguageId(37),
834       },
835       creator: Person {
836         id: data.inserted_person.id,
837         name: "timmy".into(),
838         display_name: None,
839         published: data.inserted_person.published,
840         avatar: None,
841         actor_id: data.inserted_person.actor_id.clone(),
842         local: true,
843         banned: false,
844         deleted: false,
845         admin: false,
846         bot_account: false,
847         bio: None,
848         banner: None,
849         updated: None,
850         inbox_url: data.inserted_person.inbox_url.clone(),
851         shared_inbox_url: None,
852         matrix_user_id: None,
853         ban_expires: None,
854         instance_id: data.inserted_instance.id,
855         private_key: data.inserted_person.private_key.clone(),
856         public_key: data.inserted_person.public_key.clone(),
857         last_refreshed_at: data.inserted_person.last_refreshed_at,
858       },
859       post: Post {
860         id: data.inserted_post.id,
861         name: data.inserted_post.name.clone(),
862         creator_id: data.inserted_person.id,
863         url: None,
864         body: None,
865         published: data.inserted_post.published,
866         updated: None,
867         community_id: data.inserted_community.id,
868         removed: false,
869         deleted: false,
870         locked: false,
871         nsfw: false,
872         embed_title: None,
873         embed_description: None,
874         embed_video_url: None,
875         thumbnail_url: None,
876         ap_id: data.inserted_post.ap_id.clone(),
877         local: true,
878         language_id: Default::default(),
879         featured_community: false,
880         featured_local: false,
881       },
882       community: Community {
883         id: data.inserted_community.id,
884         name: "test community 5".to_string(),
885         icon: None,
886         removed: false,
887         deleted: false,
888         nsfw: false,
889         actor_id: data.inserted_community.actor_id.clone(),
890         local: true,
891         title: "nada".to_owned(),
892         description: None,
893         updated: None,
894         banner: None,
895         hidden: false,
896         posting_restricted_to_mods: false,
897         published: data.inserted_community.published,
898         instance_id: data.inserted_instance.id,
899         private_key: data.inserted_community.private_key.clone(),
900         public_key: data.inserted_community.public_key.clone(),
901         last_refreshed_at: data.inserted_community.last_refreshed_at,
902         followers_url: data.inserted_community.followers_url.clone(),
903         inbox_url: data.inserted_community.inbox_url.clone(),
904         shared_inbox_url: data.inserted_community.shared_inbox_url.clone(),
905         moderators_url: data.inserted_community.moderators_url.clone(),
906         featured_url: data.inserted_community.featured_url.clone(),
907       },
908       counts: CommentAggregates {
909         id: agg.id,
910         comment_id: data.inserted_comment_0.id,
911         score: 1,
912         upvotes: 1,
913         downvotes: 0,
914         published: agg.published,
915         child_count: 5,
916         hot_rank: 1728,
917       },
918     }
919   }
920 }