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