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