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