]> Untitled Git - lemmy.git/blob - crates/db_views/src/comment_view.rs
2d233438ab44e41bd9958fd7d609e941619a7237
[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       // only order if filtering by a post id. DOS potential otherwise and max_depth + !post_id isn't used anyways (afaik)
344       if self.post_id.is_some() {
345         // Always order by the parent path first
346         query = query.order_by(subpath(comment::path, 0, -1));
347       }
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       // TODO a kludge to prevent attacks. Limit comments to 300 for now.
358       // (i64::MAX, 0)
359       (300, 0)
360     } else {
361       // limit_and_offset_unlimited(self.page, self.limit)
362       limit_and_offset(self.page, self.limit)?
363     };
364
365     query = match self.sort.unwrap_or(CommentSortType::Hot) {
366       CommentSortType::Hot => query
367         .then_order_by(comment_aggregates::hot_rank.desc())
368         .then_order_by(comment_aggregates::score.desc()),
369       CommentSortType::Controversial => {
370         query.then_order_by(comment_aggregates::controversy_rank.desc())
371       }
372       CommentSortType::New => query.then_order_by(comment::published.desc()),
373       CommentSortType::Old => query.then_order_by(comment::published.asc()),
374       CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
375     };
376
377     // Note: deleted and removed comments are done on the front side
378     let res = query
379       .limit(limit)
380       .offset(offset)
381       .load::<CommentViewTuple>(conn)
382       .await?;
383
384     Ok(res.into_iter().map(CommentView::from_tuple).collect())
385   }
386 }
387
388 impl JoinView for CommentView {
389   type JoinTuple = CommentViewTuple;
390   fn from_tuple(a: Self::JoinTuple) -> Self {
391     Self {
392       comment: a.0,
393       creator: a.1,
394       post: a.2,
395       community: a.3,
396       counts: a.4,
397       creator_banned_from_community: a.5.is_some(),
398       subscribed: CommunityFollower::to_subscribed_type(&a.6),
399       saved: a.7.is_some(),
400       creator_blocked: a.8.is_some(),
401       my_vote: a.9,
402     }
403   }
404 }
405
406 #[cfg(test)]
407 mod tests {
408   #![allow(clippy::unwrap_used)]
409   #![allow(clippy::indexing_slicing)]
410
411   use crate::{
412     comment_view::{
413       Comment,
414       CommentQuery,
415       CommentSortType,
416       CommentView,
417       Community,
418       DbPool,
419       Person,
420       PersonBlock,
421       Post,
422     },
423     structs::LocalUserView,
424   };
425   use lemmy_db_schema::{
426     aggregates::structs::CommentAggregates,
427     impls::actor_language::UNDETERMINED_ID,
428     newtypes::LanguageId,
429     source::{
430       actor_language::LocalUserLanguage,
431       comment::{CommentInsertForm, CommentLike, CommentLikeForm},
432       community::CommunityInsertForm,
433       instance::Instance,
434       language::Language,
435       local_user::{LocalUser, LocalUserInsertForm},
436       person::PersonInsertForm,
437       person_block::PersonBlockForm,
438       post::PostInsertForm,
439     },
440     traits::{Blockable, Crud, Likeable},
441     utils::build_db_pool_for_tests,
442     SubscribedType,
443   };
444   use serial_test::serial;
445
446   struct Data {
447     inserted_instance: Instance,
448     inserted_comment_0: Comment,
449     inserted_comment_1: Comment,
450     inserted_comment_2: Comment,
451     inserted_post: Post,
452     local_user_view: LocalUserView,
453     inserted_person_2: Person,
454     inserted_community: Community,
455   }
456
457   async fn init_data(pool: &mut DbPool<'_>) -> Data {
458     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
459       .await
460       .unwrap();
461
462     let new_person = PersonInsertForm::builder()
463       .name("timmy".into())
464       .public_key("pubkey".to_string())
465       .instance_id(inserted_instance.id)
466       .build();
467     let inserted_person = Person::create(pool, &new_person).await.unwrap();
468     let local_user_form = LocalUserInsertForm::builder()
469       .person_id(inserted_person.id)
470       .password_encrypted(String::new())
471       .build();
472     let inserted_local_user = LocalUser::create(pool, &local_user_form).await.unwrap();
473
474     let new_person_2 = PersonInsertForm::builder()
475       .name("sara".into())
476       .public_key("pubkey".to_string())
477       .instance_id(inserted_instance.id)
478       .build();
479     let inserted_person_2 = Person::create(pool, &new_person_2).await.unwrap();
480
481     let new_community = CommunityInsertForm::builder()
482       .name("test community 5".to_string())
483       .title("nada".to_owned())
484       .public_key("pubkey".to_string())
485       .instance_id(inserted_instance.id)
486       .build();
487
488     let inserted_community = Community::create(pool, &new_community).await.unwrap();
489
490     let new_post = PostInsertForm::builder()
491       .name("A test post 2".into())
492       .creator_id(inserted_person.id)
493       .community_id(inserted_community.id)
494       .build();
495
496     let inserted_post = Post::create(pool, &new_post).await.unwrap();
497     let english_id = Language::read_id_from_code(pool, Some("en")).await.unwrap();
498
499     // Create a comment tree with this hierarchy
500     //       0
501     //     \     \
502     //    1      2
503     //    \
504     //  3  4
505     //     \
506     //     5
507     let comment_form_0 = CommentInsertForm::builder()
508       .content("Comment 0".into())
509       .creator_id(inserted_person.id)
510       .post_id(inserted_post.id)
511       .language_id(english_id)
512       .build();
513
514     let inserted_comment_0 = Comment::create(pool, &comment_form_0, None).await.unwrap();
515
516     let comment_form_1 = CommentInsertForm::builder()
517       .content("Comment 1, A test blocked comment".into())
518       .creator_id(inserted_person_2.id)
519       .post_id(inserted_post.id)
520       .language_id(english_id)
521       .build();
522
523     let inserted_comment_1 = Comment::create(pool, &comment_form_1, Some(&inserted_comment_0.path))
524       .await
525       .unwrap();
526
527     let finnish_id = Language::read_id_from_code(pool, Some("fi")).await.unwrap();
528     let comment_form_2 = CommentInsertForm::builder()
529       .content("Comment 2".into())
530       .creator_id(inserted_person.id)
531       .post_id(inserted_post.id)
532       .language_id(finnish_id)
533       .build();
534
535     let inserted_comment_2 = Comment::create(pool, &comment_form_2, Some(&inserted_comment_0.path))
536       .await
537       .unwrap();
538
539     let comment_form_3 = CommentInsertForm::builder()
540       .content("Comment 3".into())
541       .creator_id(inserted_person.id)
542       .post_id(inserted_post.id)
543       .language_id(english_id)
544       .build();
545
546     let _inserted_comment_3 =
547       Comment::create(pool, &comment_form_3, Some(&inserted_comment_1.path))
548         .await
549         .unwrap();
550
551     let polish_id = Language::read_id_from_code(pool, Some("pl"))
552       .await
553       .unwrap()
554       .unwrap();
555     let comment_form_4 = CommentInsertForm::builder()
556       .content("Comment 4".into())
557       .creator_id(inserted_person.id)
558       .post_id(inserted_post.id)
559       .language_id(Some(polish_id))
560       .build();
561
562     let inserted_comment_4 = Comment::create(pool, &comment_form_4, Some(&inserted_comment_1.path))
563       .await
564       .unwrap();
565
566     let comment_form_5 = CommentInsertForm::builder()
567       .content("Comment 5".into())
568       .creator_id(inserted_person.id)
569       .post_id(inserted_post.id)
570       .build();
571
572     let _inserted_comment_5 =
573       Comment::create(pool, &comment_form_5, Some(&inserted_comment_4.path))
574         .await
575         .unwrap();
576
577     let timmy_blocks_sara_form = PersonBlockForm {
578       person_id: inserted_person.id,
579       target_id: inserted_person_2.id,
580     };
581
582     let inserted_block = PersonBlock::block(pool, &timmy_blocks_sara_form)
583       .await
584       .unwrap();
585
586     let expected_block = PersonBlock {
587       id: inserted_block.id,
588       person_id: inserted_person.id,
589       target_id: inserted_person_2.id,
590       published: inserted_block.published,
591     };
592     assert_eq!(expected_block, inserted_block);
593
594     let comment_like_form = CommentLikeForm {
595       comment_id: inserted_comment_0.id,
596       post_id: inserted_post.id,
597       person_id: inserted_person.id,
598       score: 1,
599     };
600
601     let _inserted_comment_like = CommentLike::like(pool, &comment_like_form).await.unwrap();
602
603     let local_user_view = LocalUserView {
604       local_user: inserted_local_user.clone(),
605       person: inserted_person.clone(),
606       counts: Default::default(),
607     };
608     Data {
609       inserted_instance,
610       inserted_comment_0,
611       inserted_comment_1,
612       inserted_comment_2,
613       inserted_post,
614       local_user_view,
615       inserted_person_2,
616       inserted_community,
617     }
618   }
619
620   #[tokio::test]
621   #[serial]
622   async fn test_crud() {
623     let pool = &build_db_pool_for_tests().await;
624     let pool = &mut pool.into();
625     let data = init_data(pool).await;
626
627     let expected_comment_view_no_person = expected_comment_view(&data, pool).await;
628
629     let mut expected_comment_view_with_person = expected_comment_view_no_person.clone();
630     expected_comment_view_with_person.my_vote = Some(1);
631
632     let read_comment_views_no_person = CommentQuery {
633       sort: (Some(CommentSortType::Old)),
634       post_id: (Some(data.inserted_post.id)),
635       ..Default::default()
636     }
637     .list(pool)
638     .await
639     .unwrap();
640
641     assert_eq!(
642       expected_comment_view_no_person,
643       read_comment_views_no_person[0]
644     );
645
646     let read_comment_views_with_person = CommentQuery {
647       sort: (Some(CommentSortType::Old)),
648       post_id: (Some(data.inserted_post.id)),
649       local_user: (Some(&data.local_user_view)),
650       ..Default::default()
651     }
652     .list(pool)
653     .await
654     .unwrap();
655
656     assert_eq!(
657       expected_comment_view_with_person,
658       read_comment_views_with_person[0]
659     );
660
661     // Make sure its 1, not showing the blocked comment
662     assert_eq!(5, read_comment_views_with_person.len());
663
664     let read_comment_from_blocked_person = CommentView::read(
665       pool,
666       data.inserted_comment_1.id,
667       Some(data.local_user_view.person.id),
668     )
669     .await
670     .unwrap();
671
672     // Make sure block set the creator blocked
673     assert!(read_comment_from_blocked_person.creator_blocked);
674
675     cleanup(data, pool).await;
676   }
677
678   #[tokio::test]
679   #[serial]
680   async fn test_comment_tree() {
681     let pool = &build_db_pool_for_tests().await;
682     let pool = &mut pool.into();
683     let data = init_data(pool).await;
684
685     let top_path = data.inserted_comment_0.path.clone();
686     let read_comment_views_top_path = CommentQuery {
687       post_id: (Some(data.inserted_post.id)),
688       parent_path: (Some(top_path)),
689       ..Default::default()
690     }
691     .list(pool)
692     .await
693     .unwrap();
694
695     let child_path = data.inserted_comment_1.path.clone();
696     let read_comment_views_child_path = CommentQuery {
697       post_id: (Some(data.inserted_post.id)),
698       parent_path: (Some(child_path)),
699       ..Default::default()
700     }
701     .list(pool)
702     .await
703     .unwrap();
704
705     // Make sure the comment parent-limited fetch is correct
706     assert_eq!(6, read_comment_views_top_path.len());
707     assert_eq!(4, read_comment_views_child_path.len());
708
709     // Make sure it contains the parent, but not the comment from the other tree
710     let child_comments = read_comment_views_child_path
711       .into_iter()
712       .map(|c| c.comment)
713       .collect::<Vec<Comment>>();
714     assert!(child_comments.contains(&data.inserted_comment_1));
715     assert!(!child_comments.contains(&data.inserted_comment_2));
716
717     let read_comment_views_top_max_depth = CommentQuery {
718       post_id: (Some(data.inserted_post.id)),
719       max_depth: (Some(1)),
720       ..Default::default()
721     }
722     .list(pool)
723     .await
724     .unwrap();
725
726     // Make sure a depth limited one only has the top comment
727     assert_eq!(
728       expected_comment_view(&data, pool).await,
729       read_comment_views_top_max_depth[0]
730     );
731     assert_eq!(1, read_comment_views_top_max_depth.len());
732
733     let child_path = data.inserted_comment_1.path.clone();
734     let read_comment_views_parent_max_depth = CommentQuery {
735       post_id: (Some(data.inserted_post.id)),
736       parent_path: (Some(child_path)),
737       max_depth: (Some(1)),
738       sort: (Some(CommentSortType::New)),
739       ..Default::default()
740     }
741     .list(pool)
742     .await
743     .unwrap();
744
745     // Make sure a depth limited one, and given child comment 1, has 3
746     assert!(read_comment_views_parent_max_depth[2]
747       .comment
748       .content
749       .eq("Comment 3"));
750     assert_eq!(3, read_comment_views_parent_max_depth.len());
751
752     cleanup(data, pool).await;
753   }
754
755   #[tokio::test]
756   #[serial]
757   async fn test_languages() {
758     let pool = &build_db_pool_for_tests().await;
759     let pool = &mut pool.into();
760     let data = init_data(pool).await;
761
762     // by default, user has all languages enabled and should see all comments
763     // (except from blocked user)
764     let all_languages = CommentQuery {
765       local_user: (Some(&data.local_user_view)),
766       ..Default::default()
767     }
768     .list(pool)
769     .await
770     .unwrap();
771     assert_eq!(5, all_languages.len());
772
773     // change user lang to finnish, should only show one post in finnish and one undetermined
774     let finnish_id = Language::read_id_from_code(pool, Some("fi"))
775       .await
776       .unwrap()
777       .unwrap();
778     LocalUserLanguage::update(pool, vec![finnish_id], data.local_user_view.local_user.id)
779       .await
780       .unwrap();
781     let finnish_comments = CommentQuery {
782       local_user: (Some(&data.local_user_view)),
783       ..Default::default()
784     }
785     .list(pool)
786     .await
787     .unwrap();
788     assert_eq!(2, finnish_comments.len());
789     let finnish_comment = finnish_comments
790       .iter()
791       .find(|c| c.comment.language_id == finnish_id);
792     assert!(finnish_comment.is_some());
793     assert_eq!(
794       data.inserted_comment_2.content,
795       finnish_comment.unwrap().comment.content
796     );
797
798     // now show all comments with undetermined language (which is the default value)
799     LocalUserLanguage::update(
800       pool,
801       vec![UNDETERMINED_ID],
802       data.local_user_view.local_user.id,
803     )
804     .await
805     .unwrap();
806     let undetermined_comment = CommentQuery {
807       local_user: (Some(&data.local_user_view)),
808       ..Default::default()
809     }
810     .list(pool)
811     .await
812     .unwrap();
813     assert_eq!(1, undetermined_comment.len());
814
815     cleanup(data, pool).await;
816   }
817
818   async fn cleanup(data: Data, pool: &mut DbPool<'_>) {
819     CommentLike::remove(
820       pool,
821       data.local_user_view.person.id,
822       data.inserted_comment_0.id,
823     )
824     .await
825     .unwrap();
826     Comment::delete(pool, data.inserted_comment_0.id)
827       .await
828       .unwrap();
829     Comment::delete(pool, data.inserted_comment_1.id)
830       .await
831       .unwrap();
832     Post::delete(pool, data.inserted_post.id).await.unwrap();
833     Community::delete(pool, data.inserted_community.id)
834       .await
835       .unwrap();
836     Person::delete(pool, data.local_user_view.person.id)
837       .await
838       .unwrap();
839     Person::delete(pool, data.inserted_person_2.id)
840       .await
841       .unwrap();
842     Instance::delete(pool, data.inserted_instance.id)
843       .await
844       .unwrap();
845   }
846
847   async fn expected_comment_view(data: &Data, pool: &mut DbPool<'_>) -> CommentView {
848     let agg = CommentAggregates::read(pool, data.inserted_comment_0.id)
849       .await
850       .unwrap();
851     CommentView {
852       creator_banned_from_community: false,
853       my_vote: None,
854       subscribed: SubscribedType::NotSubscribed,
855       saved: false,
856       creator_blocked: false,
857       comment: Comment {
858         id: data.inserted_comment_0.id,
859         content: "Comment 0".into(),
860         creator_id: data.local_user_view.person.id,
861         post_id: data.inserted_post.id,
862         removed: false,
863         deleted: false,
864         published: data.inserted_comment_0.published,
865         ap_id: data.inserted_comment_0.ap_id.clone(),
866         updated: None,
867         local: true,
868         distinguished: false,
869         path: data.inserted_comment_0.clone().path,
870         language_id: LanguageId(37),
871       },
872       creator: Person {
873         id: data.local_user_view.person.id,
874         name: "timmy".into(),
875         display_name: None,
876         published: data.local_user_view.person.published,
877         avatar: None,
878         actor_id: data.local_user_view.person.actor_id.clone(),
879         local: true,
880         banned: false,
881         deleted: false,
882         admin: false,
883         bot_account: false,
884         bio: None,
885         banner: None,
886         updated: None,
887         inbox_url: data.local_user_view.person.inbox_url.clone(),
888         shared_inbox_url: None,
889         matrix_user_id: None,
890         ban_expires: None,
891         instance_id: data.inserted_instance.id,
892         private_key: data.local_user_view.person.private_key.clone(),
893         public_key: data.local_user_view.person.public_key.clone(),
894         last_refreshed_at: data.local_user_view.person.last_refreshed_at,
895       },
896       post: Post {
897         id: data.inserted_post.id,
898         name: data.inserted_post.name.clone(),
899         creator_id: data.local_user_view.person.id,
900         url: None,
901         body: None,
902         published: data.inserted_post.published,
903         updated: None,
904         community_id: data.inserted_community.id,
905         removed: false,
906         deleted: false,
907         locked: false,
908         nsfw: false,
909         embed_title: None,
910         embed_description: None,
911         embed_video_url: None,
912         thumbnail_url: None,
913         ap_id: data.inserted_post.ap_id.clone(),
914         local: true,
915         language_id: Default::default(),
916         featured_community: false,
917         featured_local: false,
918       },
919       community: Community {
920         id: data.inserted_community.id,
921         name: "test community 5".to_string(),
922         icon: None,
923         removed: false,
924         deleted: false,
925         nsfw: false,
926         actor_id: data.inserted_community.actor_id.clone(),
927         local: true,
928         title: "nada".to_owned(),
929         description: None,
930         updated: None,
931         banner: None,
932         hidden: false,
933         posting_restricted_to_mods: false,
934         published: data.inserted_community.published,
935         instance_id: data.inserted_instance.id,
936         private_key: data.inserted_community.private_key.clone(),
937         public_key: data.inserted_community.public_key.clone(),
938         last_refreshed_at: data.inserted_community.last_refreshed_at,
939         followers_url: data.inserted_community.followers_url.clone(),
940         inbox_url: data.inserted_community.inbox_url.clone(),
941         shared_inbox_url: data.inserted_community.shared_inbox_url.clone(),
942         moderators_url: data.inserted_community.moderators_url.clone(),
943         featured_url: data.inserted_community.featured_url.clone(),
944       },
945       counts: CommentAggregates {
946         id: agg.id,
947         comment_id: data.inserted_comment_0.id,
948         score: 1,
949         upvotes: 1,
950         downvotes: 0,
951         published: agg.published,
952         child_count: 5,
953         hot_rank: 1728,
954         controversy_rank: 0.0,
955       },
956     }
957   }
958 }