]> Untitled Git - lemmy.git/blob - crates/db_views/src/comment_view.rs
Add diesel_async, get rid of blocking function (#2510)
[lemmy.git] / crates / db_views / src / comment_view.rs
1 use crate::structs::CommentView;
2 use diesel::{
3   dsl::*,
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, CommunitySafe},
34     local_user::LocalUser,
35     person::{Person, PersonSafe},
36     person_block::PersonBlock,
37     post::Post,
38   },
39   traits::{ToSafe, ViewToVec},
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   PersonSafe,
49   Post,
50   CommunitySafe,
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::safe_columns_tuple(),
130         post::all_columns,
131         Community::safe_columns_tuple(),
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     use diesel::dsl::*;
189     let conn = &mut get_conn(self.pool).await?;
190
191     // The left join below will return None in this case
192     let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
193     let local_user_id_join = self.local_user.map(|l| l.id).unwrap_or(LocalUserId(-1));
194
195     let mut query = comment::table
196       .inner_join(person::table)
197       .inner_join(post::table)
198       .inner_join(community::table.on(post::community_id.eq(community::id)))
199       .inner_join(comment_aggregates::table)
200       .left_join(
201         community_person_ban::table.on(
202           community::id
203             .eq(community_person_ban::community_id)
204             .and(community_person_ban::person_id.eq(comment::creator_id))
205             .and(
206               community_person_ban::expires
207                 .is_null()
208                 .or(community_person_ban::expires.gt(now)),
209             ),
210         ),
211       )
212       .left_join(
213         community_follower::table.on(
214           post::community_id
215             .eq(community_follower::community_id)
216             .and(community_follower::person_id.eq(person_id_join)),
217         ),
218       )
219       .left_join(
220         comment_saved::table.on(
221           comment::id
222             .eq(comment_saved::comment_id)
223             .and(comment_saved::person_id.eq(person_id_join)),
224         ),
225       )
226       .left_join(
227         person_block::table.on(
228           comment::creator_id
229             .eq(person_block::target_id)
230             .and(person_block::person_id.eq(person_id_join)),
231         ),
232       )
233       .left_join(
234         community_block::table.on(
235           community::id
236             .eq(community_block::community_id)
237             .and(community_block::person_id.eq(person_id_join)),
238         ),
239       )
240       .left_join(
241         comment_like::table.on(
242           comment::id
243             .eq(comment_like::comment_id)
244             .and(comment_like::person_id.eq(person_id_join)),
245         ),
246       )
247       .left_join(
248         local_user_language::table.on(
249           comment::language_id
250             .eq(local_user_language::language_id)
251             .and(local_user_language::local_user_id.eq(local_user_id_join)),
252         ),
253       )
254       .select((
255         comment::all_columns,
256         Person::safe_columns_tuple(),
257         post::all_columns,
258         Community::safe_columns_tuple(),
259         comment_aggregates::all_columns,
260         community_person_ban::all_columns.nullable(),
261         community_follower::all_columns.nullable(),
262         comment_saved::all_columns.nullable(),
263         person_block::all_columns.nullable(),
264         comment_like::score.nullable(),
265       ))
266       .into_boxed();
267
268     if let Some(creator_id) = self.creator_id {
269       query = query.filter(comment::creator_id.eq(creator_id));
270     };
271
272     if let Some(post_id) = self.post_id {
273       query = query.filter(comment::post_id.eq(post_id));
274     };
275
276     if let Some(parent_path) = self.parent_path.as_ref() {
277       query = query.filter(comment::path.contained_by(parent_path));
278     };
279
280     if let Some(search_term) = self.search_term {
281       query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
282     };
283
284     if let Some(listing_type) = self.listing_type {
285       match listing_type {
286         ListingType::Subscribed => {
287           query = query.filter(community_follower::person_id.is_not_null())
288         } // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
289         ListingType::Local => {
290           query = query.filter(community::local.eq(true)).filter(
291             community::hidden
292               .eq(false)
293               .or(community_follower::person_id.eq(person_id_join)),
294           )
295         }
296         ListingType::All => {
297           query = query.filter(
298             community::hidden
299               .eq(false)
300               .or(community_follower::person_id.eq(person_id_join)),
301           )
302         }
303       }
304     };
305
306     if let Some(community_id) = self.community_id {
307       query = query.filter(post::community_id.eq(community_id));
308     }
309
310     if let Some(community_actor_id) = self.community_actor_id {
311       query = query.filter(community::actor_id.eq(community_actor_id))
312     }
313
314     if self.saved_only.unwrap_or(false) {
315       query = query.filter(comment_saved::id.is_not_null());
316     }
317
318     if !self.show_deleted_and_removed.unwrap_or(true) {
319       query = query.filter(comment::deleted.eq(false));
320       query = query.filter(comment::removed.eq(false));
321     }
322
323     if !self.local_user.map(|l| l.show_bot_accounts).unwrap_or(true) {
324       query = query.filter(person::bot_account.eq(false));
325     };
326
327     if self.local_user.is_some() {
328       // Filter out the rows with missing languages
329       query = query.filter(local_user_language::id.is_not_null());
330
331       // Don't show blocked communities or persons
332       query = query.filter(community_block::person_id.is_null());
333       query = query.filter(person_block::person_id.is_null());
334     }
335
336     // A Max depth given means its a tree fetch
337     let (limit, offset) = if let Some(max_depth) = self.max_depth {
338       let depth_limit = if let Some(parent_path) = self.parent_path.as_ref() {
339         parent_path.0.split('.').count() as i32 + max_depth
340         // Add one because of root "0"
341       } else {
342         max_depth + 1
343       };
344
345       query = query.filter(nlevel(comment::path).le(depth_limit));
346
347       // Always order by the parent path first
348       query = query.order_by(subpath(comment::path, 0, -1));
349
350       // TODO limit question. Limiting does not work for comment threads ATM, only max_depth
351       // For now, don't do any limiting for tree fetches
352       // https://stackoverflow.com/questions/72983614/postgres-ltree-how-to-limit-the-max-number-of-children-at-any-given-level
353
354       // Don't use the regular error-checking one, many more comments must ofter be fetched.
355       // This does not work for comment trees, and the limit should be manually set to a high number
356       //
357       // If a max depth is given, then you know its a tree fetch, and limits should be ignored
358       (i64::MAX, 0)
359     } else {
360       limit_and_offset_unlimited(self.page, self.limit)
361     };
362
363     query = match self.sort.unwrap_or(CommentSortType::Hot) {
364       CommentSortType::Hot => query
365         .then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
366         .then_order_by(comment_aggregates::published.desc()),
367       CommentSortType::New => query.then_order_by(comment::published.desc()),
368       CommentSortType::Old => query.then_order_by(comment::published.asc()),
369       CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
370     };
371
372     // Note: deleted and removed comments are done on the front side
373     let res = query
374       .limit(limit)
375       .offset(offset)
376       .load::<CommentViewTuple>(conn)
377       .await?;
378
379     Ok(CommentView::from_tuple_to_vec(res))
380   }
381 }
382
383 impl ViewToVec for CommentView {
384   type DbTuple = CommentViewTuple;
385   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
386     items
387       .into_iter()
388       .map(|a| Self {
389         comment: a.0,
390         creator: a.1,
391         post: a.2,
392         community: a.3,
393         counts: a.4,
394         creator_banned_from_community: a.5.is_some(),
395         subscribed: CommunityFollower::to_subscribed_type(&a.6),
396         saved: a.7.is_some(),
397         creator_blocked: a.8.is_some(),
398         my_vote: a.9,
399       })
400       .collect::<Vec<Self>>()
401   }
402 }
403
404 #[cfg(test)]
405 mod tests {
406   use crate::comment_view::*;
407   use lemmy_db_schema::{
408     aggregates::structs::CommentAggregates,
409     newtypes::LanguageId,
410     source::{
411       actor_language::LocalUserLanguage,
412       comment::*,
413       community::*,
414       instance::Instance,
415       language::Language,
416       local_user::LocalUserInsertForm,
417       person::*,
418       person_block::PersonBlockForm,
419       post::*,
420     },
421     traits::{Blockable, Crud, Likeable},
422     utils::build_db_pool_for_tests,
423     SubscribedType,
424   };
425   use serial_test::serial;
426
427   struct Data {
428     inserted_instance: Instance,
429     inserted_comment_0: Comment,
430     inserted_comment_1: Comment,
431     inserted_comment_2: Comment,
432     inserted_post: Post,
433     inserted_person: Person,
434     inserted_local_user: LocalUser,
435     inserted_person_2: Person,
436     inserted_community: Community,
437   }
438
439   async fn init_data(pool: &DbPool) -> Data {
440     let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
441
442     let new_person = PersonInsertForm::builder()
443       .name("timmy".into())
444       .public_key("pubkey".to_string())
445       .instance_id(inserted_instance.id)
446       .build();
447     let inserted_person = Person::create(pool, &new_person).await.unwrap();
448     let local_user_form = LocalUserInsertForm::builder()
449       .person_id(inserted_person.id)
450       .password_encrypted("".to_string())
451       .build();
452     let inserted_local_user = LocalUser::create(pool, &local_user_form).await.unwrap();
453
454     let new_person_2 = PersonInsertForm::builder()
455       .name("sara".into())
456       .public_key("pubkey".to_string())
457       .instance_id(inserted_instance.id)
458       .build();
459     let inserted_person_2 = Person::create(pool, &new_person_2).await.unwrap();
460
461     let new_community = CommunityInsertForm::builder()
462       .name("test community 5".to_string())
463       .title("nada".to_owned())
464       .public_key("pubkey".to_string())
465       .instance_id(inserted_instance.id)
466       .build();
467
468     let inserted_community = Community::create(pool, &new_community).await.unwrap();
469
470     let new_post = PostInsertForm::builder()
471       .name("A test post 2".into())
472       .creator_id(inserted_person.id)
473       .community_id(inserted_community.id)
474       .build();
475
476     let inserted_post = Post::create(pool, &new_post).await.unwrap();
477
478     // Create a comment tree with this hierarchy
479     //       0
480     //     \     \
481     //    1      2
482     //    \
483     //  3  4
484     //     \
485     //     5
486     let comment_form_0 = CommentInsertForm::builder()
487       .content("Comment 0".into())
488       .creator_id(inserted_person.id)
489       .post_id(inserted_post.id)
490       .build();
491
492     let inserted_comment_0 = Comment::create(pool, &comment_form_0, None).await.unwrap();
493
494     let comment_form_1 = CommentInsertForm::builder()
495       .content("Comment 1, A test blocked comment".into())
496       .creator_id(inserted_person_2.id)
497       .post_id(inserted_post.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, "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(Some(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       .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, "pl").await.unwrap();
528     let comment_form_4 = CommentInsertForm::builder()
529       .content("Comment 4".into())
530       .creator_id(inserted_person.id)
531       .post_id(inserted_post.id)
532       .language_id(Some(polish_id))
533       .build();
534
535     let inserted_comment_4 = Comment::create(pool, &comment_form_4, Some(&inserted_comment_1.path))
536       .await
537       .unwrap();
538
539     let comment_form_5 = CommentInsertForm::builder()
540       .content("Comment 5".into())
541       .creator_id(inserted_person.id)
542       .post_id(inserted_post.id)
543       .build();
544
545     let _inserted_comment_5 =
546       Comment::create(pool, &comment_form_5, Some(&inserted_comment_4.path))
547         .await
548         .unwrap();
549
550     let timmy_blocks_sara_form = PersonBlockForm {
551       person_id: inserted_person.id,
552       target_id: inserted_person_2.id,
553     };
554
555     let inserted_block = PersonBlock::block(pool, &timmy_blocks_sara_form)
556       .await
557       .unwrap();
558
559     let expected_block = PersonBlock {
560       id: inserted_block.id,
561       person_id: inserted_person.id,
562       target_id: inserted_person_2.id,
563       published: inserted_block.published,
564     };
565     assert_eq!(expected_block, inserted_block);
566
567     let comment_like_form = CommentLikeForm {
568       comment_id: inserted_comment_0.id,
569       post_id: inserted_post.id,
570       person_id: inserted_person.id,
571       score: 1,
572     };
573
574     let _inserted_comment_like = CommentLike::like(pool, &comment_like_form).await.unwrap();
575
576     Data {
577       inserted_instance,
578       inserted_comment_0,
579       inserted_comment_1,
580       inserted_comment_2,
581       inserted_post,
582       inserted_person,
583       inserted_local_user,
584       inserted_person_2,
585       inserted_community,
586     }
587   }
588
589   #[tokio::test]
590   #[serial]
591   async fn test_crud() {
592     let pool = &build_db_pool_for_tests().await;
593     let data = init_data(pool).await;
594
595     let expected_comment_view_no_person = expected_comment_view(&data, pool).await;
596
597     let mut expected_comment_view_with_person = expected_comment_view_no_person.to_owned();
598     expected_comment_view_with_person.my_vote = Some(1);
599
600     let read_comment_views_no_person = CommentQuery::builder()
601       .pool(pool)
602       .post_id(Some(data.inserted_post.id))
603       .build()
604       .list()
605       .await
606       .unwrap();
607
608     assert_eq!(
609       expected_comment_view_no_person,
610       read_comment_views_no_person[0]
611     );
612
613     let read_comment_views_with_person = CommentQuery::builder()
614       .pool(pool)
615       .post_id(Some(data.inserted_post.id))
616       .local_user(Some(&data.inserted_local_user))
617       .build()
618       .list()
619       .await
620       .unwrap();
621
622     assert_eq!(
623       expected_comment_view_with_person,
624       read_comment_views_with_person[0]
625     );
626
627     // Make sure its 1, not showing the blocked comment
628     assert_eq!(5, read_comment_views_with_person.len());
629
630     let read_comment_from_blocked_person = CommentView::read(
631       pool,
632       data.inserted_comment_1.id,
633       Some(data.inserted_person.id),
634     )
635     .await
636     .unwrap();
637
638     // Make sure block set the creator blocked
639     assert!(read_comment_from_blocked_person.creator_blocked);
640
641     cleanup(data, pool).await;
642   }
643
644   #[tokio::test]
645   #[serial]
646   async fn test_comment_tree() {
647     let pool = &build_db_pool_for_tests().await;
648     let data = init_data(pool).await;
649
650     let top_path = data.inserted_comment_0.path.clone();
651     let read_comment_views_top_path = CommentQuery::builder()
652       .pool(pool)
653       .post_id(Some(data.inserted_post.id))
654       .parent_path(Some(top_path))
655       .build()
656       .list()
657       .await
658       .unwrap();
659
660     let child_path = data.inserted_comment_1.path.clone();
661     let read_comment_views_child_path = CommentQuery::builder()
662       .pool(pool)
663       .post_id(Some(data.inserted_post.id))
664       .parent_path(Some(child_path))
665       .build()
666       .list()
667       .await
668       .unwrap();
669
670     // Make sure the comment parent-limited fetch is correct
671     assert_eq!(6, read_comment_views_top_path.len());
672     assert_eq!(4, read_comment_views_child_path.len());
673
674     // Make sure it contains the parent, but not the comment from the other tree
675     let child_comments = read_comment_views_child_path
676       .into_iter()
677       .map(|c| c.comment)
678       .collect::<Vec<Comment>>();
679     assert!(child_comments.contains(&data.inserted_comment_1));
680     assert!(!child_comments.contains(&data.inserted_comment_2));
681
682     let read_comment_views_top_max_depth = CommentQuery::builder()
683       .pool(pool)
684       .post_id(Some(data.inserted_post.id))
685       .max_depth(Some(1))
686       .build()
687       .list()
688       .await
689       .unwrap();
690
691     // Make sure a depth limited one only has the top comment
692     assert_eq!(
693       expected_comment_view(&data, pool).await,
694       read_comment_views_top_max_depth[0]
695     );
696     assert_eq!(1, read_comment_views_top_max_depth.len());
697
698     let child_path = data.inserted_comment_1.path.clone();
699     let read_comment_views_parent_max_depth = CommentQuery::builder()
700       .pool(pool)
701       .post_id(Some(data.inserted_post.id))
702       .parent_path(Some(child_path))
703       .max_depth(Some(1))
704       .sort(Some(CommentSortType::New))
705       .build()
706       .list()
707       .await
708       .unwrap();
709
710     // Make sure a depth limited one, and given child comment 1, has 3
711     assert!(read_comment_views_parent_max_depth[2]
712       .comment
713       .content
714       .eq("Comment 3"));
715     assert_eq!(3, read_comment_views_parent_max_depth.len());
716
717     cleanup(data, pool).await;
718   }
719
720   #[tokio::test]
721   #[serial]
722   async fn test_languages() {
723     let pool = &build_db_pool_for_tests().await;
724     let data = init_data(pool).await;
725
726     // by default, user has all languages enabled and should see all comments
727     // (except from blocked user)
728     let all_languages = CommentQuery::builder()
729       .pool(pool)
730       .local_user(Some(&data.inserted_local_user))
731       .build()
732       .list()
733       .await
734       .unwrap();
735     assert_eq!(5, all_languages.len());
736
737     // change user lang to finnish, should only show single finnish comment
738     let finnish_id = Language::read_id_from_code(pool, "fi").await.unwrap();
739     LocalUserLanguage::update(pool, vec![finnish_id], data.inserted_local_user.id)
740       .await
741       .unwrap();
742     let finnish_comment = CommentQuery::builder()
743       .pool(pool)
744       .local_user(Some(&data.inserted_local_user))
745       .build()
746       .list()
747       .await
748       .unwrap();
749     assert_eq!(1, finnish_comment.len());
750     assert_eq!(
751       data.inserted_comment_2.content,
752       finnish_comment[0].comment.content
753     );
754     assert_eq!(finnish_id, finnish_comment[0].comment.language_id);
755
756     // now show all comments with undetermined language (which is the default value)
757     let undetermined_id = Language::read_id_from_code(pool, "und").await.unwrap();
758     LocalUserLanguage::update(pool, vec![undetermined_id], data.inserted_local_user.id)
759       .await
760       .unwrap();
761     let undetermined_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!(3, undetermined_comment.len());
769
770     cleanup(data, pool).await;
771   }
772
773   async fn cleanup(data: Data, pool: &DbPool) {
774     CommentLike::remove(pool, data.inserted_person.id, data.inserted_comment_0.id)
775       .await
776       .unwrap();
777     Comment::delete(pool, data.inserted_comment_0.id)
778       .await
779       .unwrap();
780     Comment::delete(pool, data.inserted_comment_1.id)
781       .await
782       .unwrap();
783     Post::delete(pool, data.inserted_post.id).await.unwrap();
784     Community::delete(pool, data.inserted_community.id)
785       .await
786       .unwrap();
787     Person::delete(pool, data.inserted_person.id).await.unwrap();
788     Person::delete(pool, data.inserted_person_2.id)
789       .await
790       .unwrap();
791     Instance::delete(pool, data.inserted_instance.id)
792       .await
793       .unwrap();
794   }
795
796   async fn expected_comment_view(data: &Data, pool: &DbPool) -> CommentView {
797     let agg = CommentAggregates::read(pool, data.inserted_comment_0.id)
798       .await
799       .unwrap();
800     CommentView {
801       creator_banned_from_community: false,
802       my_vote: None,
803       subscribed: SubscribedType::NotSubscribed,
804       saved: false,
805       creator_blocked: false,
806       comment: Comment {
807         id: data.inserted_comment_0.id,
808         content: "Comment 0".into(),
809         creator_id: data.inserted_person.id,
810         post_id: data.inserted_post.id,
811         removed: false,
812         deleted: false,
813         published: data.inserted_comment_0.published,
814         ap_id: data.inserted_comment_0.ap_id.clone(),
815         updated: None,
816         local: true,
817         distinguished: false,
818         path: data.inserted_comment_0.to_owned().path,
819         language_id: LanguageId(0),
820       },
821       creator: PersonSafe {
822         id: data.inserted_person.id,
823         name: "timmy".into(),
824         display_name: None,
825         published: data.inserted_person.published,
826         avatar: None,
827         actor_id: data.inserted_person.actor_id.to_owned(),
828         local: true,
829         banned: false,
830         deleted: false,
831         admin: false,
832         bot_account: false,
833         bio: None,
834         banner: None,
835         updated: None,
836         inbox_url: data.inserted_person.inbox_url.to_owned(),
837         shared_inbox_url: None,
838         matrix_user_id: None,
839         ban_expires: None,
840         instance_id: data.inserted_instance.id,
841       },
842       post: Post {
843         id: data.inserted_post.id,
844         name: data.inserted_post.name.to_owned(),
845         creator_id: data.inserted_person.id,
846         url: None,
847         body: None,
848         published: data.inserted_post.published,
849         updated: None,
850         community_id: data.inserted_community.id,
851         removed: false,
852         deleted: false,
853         locked: false,
854         stickied: false,
855         nsfw: false,
856         embed_title: None,
857         embed_description: None,
858         embed_video_url: None,
859         thumbnail_url: None,
860         ap_id: data.inserted_post.ap_id.to_owned(),
861         local: true,
862         language_id: Default::default(),
863       },
864       community: CommunitySafe {
865         id: data.inserted_community.id,
866         name: "test community 5".to_string(),
867         icon: None,
868         removed: false,
869         deleted: false,
870         nsfw: false,
871         actor_id: data.inserted_community.actor_id.to_owned(),
872         local: true,
873         title: "nada".to_owned(),
874         description: None,
875         updated: None,
876         banner: None,
877         hidden: false,
878         posting_restricted_to_mods: false,
879         published: data.inserted_community.published,
880         instance_id: data.inserted_instance.id,
881       },
882       counts: CommentAggregates {
883         id: agg.id,
884         comment_id: data.inserted_comment_0.id,
885         score: 1,
886         upvotes: 1,
887         downvotes: 0,
888         published: agg.published,
889         child_count: 5,
890       },
891     }
892   }
893 }