1 use crate::structs::CommentView;
2 use diesel::{dsl::*, result::Error, *};
3 use diesel_ltree::{nlevel, subpath, Ltree, LtreeExtensions};
5 aggregates::structs::CommentAggregates,
6 newtypes::{CommentId, CommunityId, DbUrl, LocalUserId, PersonId, PostId},
22 comment::{Comment, CommentSaved},
23 community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
24 local_user::LocalUser,
25 person::{Person, PersonSafe},
26 person_block::PersonBlock,
29 traits::{ToSafe, ViewToVec},
30 utils::{functions::hot_rank, fuzzy_search, limit_and_offset_unlimited},
34 use typed_builder::TypedBuilder;
36 type CommentViewTuple = (
42 Option<CommunityPersonBan>,
43 Option<CommunityFollower>,
51 conn: &mut PgConnection,
52 comment_id: CommentId,
53 my_person_id: Option<PersonId>,
54 ) -> Result<Self, Error> {
55 // The left join below will return None in this case
56 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
64 creator_banned_from_community,
71 .inner_join(person::table)
72 .inner_join(post::table)
73 .inner_join(community::table.on(post::community_id.eq(community::id)))
74 .inner_join(comment_aggregates::table)
76 community_person_ban::table.on(
78 .eq(community_person_ban::community_id)
79 .and(community_person_ban::person_id.eq(comment::creator_id))
81 community_person_ban::expires
83 .or(community_person_ban::expires.gt(now)),
88 community_follower::table.on(
90 .eq(community_follower::community_id)
91 .and(community_follower::person_id.eq(person_id_join)),
95 comment_saved::table.on(
97 .eq(comment_saved::comment_id)
98 .and(comment_saved::person_id.eq(person_id_join)),
102 person_block::table.on(
104 .eq(person_block::target_id)
105 .and(person_block::person_id.eq(person_id_join)),
109 comment_like::table.on(
111 .eq(comment_like::comment_id)
112 .and(comment_like::person_id.eq(person_id_join)),
116 comment::all_columns,
117 Person::safe_columns_tuple(),
119 Community::safe_columns_tuple(),
120 comment_aggregates::all_columns,
121 community_person_ban::all_columns.nullable(),
122 community_follower::all_columns.nullable(),
123 comment_saved::all_columns.nullable(),
124 person_block::all_columns.nullable(),
125 comment_like::score.nullable(),
127 .first::<CommentViewTuple>(conn)?;
129 // If a person is given, then my_vote, if None, should be 0, not null
130 // Necessary to differentiate between other person's votes
131 let my_vote = if my_person_id.is_some() && comment_like.is_none() {
143 creator_banned_from_community: creator_banned_from_community.is_some(),
144 subscribed: CommunityFollower::to_subscribed_type(&follower),
145 saved: saved.is_some(),
146 creator_blocked: creator_blocked.is_some(),
152 #[derive(TypedBuilder)]
153 #[builder(field_defaults(default))]
154 pub struct CommentQuery<'a> {
156 conn: &'a mut PgConnection,
157 listing_type: Option<ListingType>,
158 sort: Option<CommentSortType>,
159 community_id: Option<CommunityId>,
160 community_actor_id: Option<DbUrl>,
161 post_id: Option<PostId>,
162 parent_path: Option<Ltree>,
163 creator_id: Option<PersonId>,
164 local_user: Option<&'a LocalUser>,
165 search_term: Option<String>,
166 saved_only: Option<bool>,
167 show_deleted_and_removed: Option<bool>,
170 max_depth: Option<i32>,
173 impl<'a> CommentQuery<'a> {
174 pub fn list(self) -> Result<Vec<CommentView>, Error> {
177 // The left join below will return None in this case
178 let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
179 let local_user_id_join = self.local_user.map(|l| l.id).unwrap_or(LocalUserId(-1));
181 let mut query = comment::table
182 .inner_join(person::table)
183 .inner_join(post::table)
184 .inner_join(community::table.on(post::community_id.eq(community::id)))
185 .inner_join(comment_aggregates::table)
187 community_person_ban::table.on(
189 .eq(community_person_ban::community_id)
190 .and(community_person_ban::person_id.eq(comment::creator_id))
192 community_person_ban::expires
194 .or(community_person_ban::expires.gt(now)),
199 community_follower::table.on(
201 .eq(community_follower::community_id)
202 .and(community_follower::person_id.eq(person_id_join)),
206 comment_saved::table.on(
208 .eq(comment_saved::comment_id)
209 .and(comment_saved::person_id.eq(person_id_join)),
213 person_block::table.on(
215 .eq(person_block::target_id)
216 .and(person_block::person_id.eq(person_id_join)),
220 community_block::table.on(
222 .eq(community_block::community_id)
223 .and(community_block::person_id.eq(person_id_join)),
227 comment_like::table.on(
229 .eq(comment_like::comment_id)
230 .and(comment_like::person_id.eq(person_id_join)),
234 local_user_language::table.on(
236 .eq(local_user_language::language_id)
237 .and(local_user_language::local_user_id.eq(local_user_id_join)),
241 comment::all_columns,
242 Person::safe_columns_tuple(),
244 Community::safe_columns_tuple(),
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(),
254 if let Some(creator_id) = self.creator_id {
255 query = query.filter(comment::creator_id.eq(creator_id));
258 if let Some(post_id) = self.post_id {
259 query = query.filter(comment::post_id.eq(post_id));
262 if let Some(parent_path) = self.parent_path.as_ref() {
263 query = query.filter(comment::path.contained_by(parent_path));
266 if let Some(search_term) = self.search_term {
267 query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
270 if let Some(listing_type) = self.listing_type {
272 ListingType::Subscribed => {
273 query = query.filter(community_follower::person_id.is_not_null())
274 } // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
275 ListingType::Local => {
276 query = query.filter(community::local.eq(true)).filter(
279 .or(community_follower::person_id.eq(person_id_join)),
282 ListingType::All => {
283 query = query.filter(
286 .or(community_follower::person_id.eq(person_id_join)),
292 if let Some(community_id) = self.community_id {
293 query = query.filter(post::community_id.eq(community_id));
296 if let Some(community_actor_id) = self.community_actor_id {
297 query = query.filter(community::actor_id.eq(community_actor_id))
300 if self.saved_only.unwrap_or(false) {
301 query = query.filter(comment_saved::id.is_not_null());
304 if !self.show_deleted_and_removed.unwrap_or(true) {
305 query = query.filter(comment::deleted.eq(false));
306 query = query.filter(comment::removed.eq(false));
309 if !self.local_user.map(|l| l.show_bot_accounts).unwrap_or(true) {
310 query = query.filter(person::bot_account.eq(false));
313 if self.local_user.is_some() {
314 // Filter out the rows with missing languages
315 query = query.filter(local_user_language::id.is_not_null());
317 // Don't show blocked communities or persons
318 query = query.filter(community_block::person_id.is_null());
319 query = query.filter(person_block::person_id.is_null());
322 // A Max depth given means its a tree fetch
323 let (limit, offset) = if let Some(max_depth) = self.max_depth {
324 let depth_limit = if let Some(parent_path) = self.parent_path.as_ref() {
325 parent_path.0.split('.').count() as i32 + max_depth
326 // Add one because of root "0"
331 query = query.filter(nlevel(comment::path).le(depth_limit));
333 // Always order by the parent path first
334 query = query.order_by(subpath(comment::path, 0, -1));
336 // TODO limit question. Limiting does not work for comment threads ATM, only max_depth
337 // For now, don't do any limiting for tree fetches
338 // https://stackoverflow.com/questions/72983614/postgres-ltree-how-to-limit-the-max-number-of-children-at-any-given-level
340 // Don't use the regular error-checking one, many more comments must ofter be fetched.
341 // This does not work for comment trees, and the limit should be manually set to a high number
343 // If a max depth is given, then you know its a tree fetch, and limits should be ignored
346 limit_and_offset_unlimited(self.page, self.limit)
349 query = match self.sort.unwrap_or(CommentSortType::Hot) {
350 CommentSortType::Hot => query
351 .then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
352 .then_order_by(comment_aggregates::published.desc()),
353 CommentSortType::New => query.then_order_by(comment::published.desc()),
354 CommentSortType::Old => query.then_order_by(comment::published.asc()),
355 CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
358 // Note: deleted and removed comments are done on the front side
362 .load::<CommentViewTuple>(self.conn)?;
364 Ok(CommentView::from_tuple_to_vec(res))
368 impl ViewToVec for CommentView {
369 type DbTuple = CommentViewTuple;
370 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
379 creator_banned_from_community: a.5.is_some(),
380 subscribed: CommunityFollower::to_subscribed_type(&a.6),
381 saved: a.7.is_some(),
382 creator_blocked: a.8.is_some(),
385 .collect::<Vec<Self>>()
391 use crate::comment_view::*;
392 use lemmy_db_schema::{
393 aggregates::structs::CommentAggregates,
394 newtypes::LanguageId,
396 actor_language::LocalUserLanguage,
401 local_user::LocalUserInsertForm,
403 person_block::PersonBlockForm,
406 traits::{Blockable, Crud, Likeable},
407 utils::establish_unpooled_connection,
410 use serial_test::serial;
413 inserted_instance: Instance,
414 inserted_comment_0: Comment,
415 inserted_comment_1: Comment,
416 inserted_comment_2: Comment,
418 inserted_person: Person,
419 inserted_local_user: LocalUser,
420 inserted_person_2: Person,
421 inserted_community: Community,
424 fn init_data(conn: &mut PgConnection) -> Data {
425 let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
427 let new_person = PersonInsertForm::builder()
428 .name("timmy".into())
429 .public_key("pubkey".to_string())
430 .instance_id(inserted_instance.id)
432 let inserted_person = Person::create(conn, &new_person).unwrap();
433 let local_user_form = LocalUserInsertForm::builder()
434 .person_id(inserted_person.id)
435 .password_encrypted("".to_string())
437 let inserted_local_user = LocalUser::create(conn, &local_user_form).unwrap();
439 let new_person_2 = PersonInsertForm::builder()
441 .public_key("pubkey".to_string())
442 .instance_id(inserted_instance.id)
444 let inserted_person_2 = Person::create(conn, &new_person_2).unwrap();
446 let new_community = CommunityInsertForm::builder()
447 .name("test community 5".to_string())
448 .title("nada".to_owned())
449 .public_key("pubkey".to_string())
450 .instance_id(inserted_instance.id)
453 let inserted_community = Community::create(conn, &new_community).unwrap();
455 let new_post = PostInsertForm::builder()
456 .name("A test post 2".into())
457 .creator_id(inserted_person.id)
458 .community_id(inserted_community.id)
461 let inserted_post = Post::create(conn, &new_post).unwrap();
463 // Create a comment tree with this hierarchy
471 let comment_form_0 = CommentInsertForm::builder()
472 .content("Comment 0".into())
473 .creator_id(inserted_person.id)
474 .post_id(inserted_post.id)
477 let inserted_comment_0 = Comment::create(conn, &comment_form_0, None).unwrap();
479 let comment_form_1 = CommentInsertForm::builder()
480 .content("Comment 1, A test blocked comment".into())
481 .creator_id(inserted_person_2.id)
482 .post_id(inserted_post.id)
485 let inserted_comment_1 =
486 Comment::create(conn, &comment_form_1, Some(&inserted_comment_0.path)).unwrap();
488 let finnish_id = Language::read_id_from_code(conn, "fi").unwrap();
489 let comment_form_2 = CommentInsertForm::builder()
490 .content("Comment 2".into())
491 .creator_id(inserted_person.id)
492 .post_id(inserted_post.id)
493 .language_id(Some(finnish_id))
496 let inserted_comment_2 =
497 Comment::create(conn, &comment_form_2, Some(&inserted_comment_0.path)).unwrap();
499 let comment_form_3 = CommentInsertForm::builder()
500 .content("Comment 3".into())
501 .creator_id(inserted_person.id)
502 .post_id(inserted_post.id)
505 let _inserted_comment_3 =
506 Comment::create(conn, &comment_form_3, Some(&inserted_comment_1.path)).unwrap();
508 let polish_id = Language::read_id_from_code(conn, "pl").unwrap();
509 let comment_form_4 = CommentInsertForm::builder()
510 .content("Comment 4".into())
511 .creator_id(inserted_person.id)
512 .post_id(inserted_post.id)
513 .language_id(Some(polish_id))
516 let inserted_comment_4 =
517 Comment::create(conn, &comment_form_4, Some(&inserted_comment_1.path)).unwrap();
519 let comment_form_5 = CommentInsertForm::builder()
520 .content("Comment 5".into())
521 .creator_id(inserted_person.id)
522 .post_id(inserted_post.id)
525 let _inserted_comment_5 =
526 Comment::create(conn, &comment_form_5, Some(&inserted_comment_4.path)).unwrap();
528 let timmy_blocks_sara_form = PersonBlockForm {
529 person_id: inserted_person.id,
530 target_id: inserted_person_2.id,
533 let inserted_block = PersonBlock::block(conn, &timmy_blocks_sara_form).unwrap();
535 let expected_block = PersonBlock {
536 id: inserted_block.id,
537 person_id: inserted_person.id,
538 target_id: inserted_person_2.id,
539 published: inserted_block.published,
541 assert_eq!(expected_block, inserted_block);
543 let comment_like_form = CommentLikeForm {
544 comment_id: inserted_comment_0.id,
545 post_id: inserted_post.id,
546 person_id: inserted_person.id,
550 let _inserted_comment_like = CommentLike::like(conn, &comment_like_form).unwrap();
568 let conn = &mut establish_unpooled_connection();
569 let data = init_data(conn);
571 let expected_comment_view_no_person = expected_comment_view(&data, conn);
573 let mut expected_comment_view_with_person = expected_comment_view_no_person.to_owned();
574 expected_comment_view_with_person.my_vote = Some(1);
576 let read_comment_views_no_person = CommentQuery::builder()
578 .post_id(Some(data.inserted_post.id))
584 expected_comment_view_no_person,
585 read_comment_views_no_person[0]
588 let read_comment_views_with_person = CommentQuery::builder()
590 .post_id(Some(data.inserted_post.id))
591 .local_user(Some(&data.inserted_local_user))
597 expected_comment_view_with_person,
598 read_comment_views_with_person[0]
601 // Make sure its 1, not showing the blocked comment
602 assert_eq!(5, read_comment_views_with_person.len());
604 let read_comment_from_blocked_person = CommentView::read(
606 data.inserted_comment_1.id,
607 Some(data.inserted_person.id),
611 // Make sure block set the creator blocked
612 assert!(read_comment_from_blocked_person.creator_blocked);
619 fn test_comment_tree() {
620 let conn = &mut establish_unpooled_connection();
621 let data = init_data(conn);
623 let top_path = data.inserted_comment_0.path.clone();
624 let read_comment_views_top_path = CommentQuery::builder()
626 .post_id(Some(data.inserted_post.id))
627 .parent_path(Some(top_path))
632 let child_path = data.inserted_comment_1.path.clone();
633 let read_comment_views_child_path = CommentQuery::builder()
635 .post_id(Some(data.inserted_post.id))
636 .parent_path(Some(child_path))
641 // Make sure the comment parent-limited fetch is correct
642 assert_eq!(6, read_comment_views_top_path.len());
643 assert_eq!(4, read_comment_views_child_path.len());
645 // Make sure it contains the parent, but not the comment from the other tree
646 let child_comments = read_comment_views_child_path
649 .collect::<Vec<Comment>>();
650 assert!(child_comments.contains(&data.inserted_comment_1));
651 assert!(!child_comments.contains(&data.inserted_comment_2));
653 let read_comment_views_top_max_depth = CommentQuery::builder()
655 .post_id(Some(data.inserted_post.id))
661 // Make sure a depth limited one only has the top comment
663 expected_comment_view(&data, conn),
664 read_comment_views_top_max_depth[0]
666 assert_eq!(1, read_comment_views_top_max_depth.len());
668 let child_path = data.inserted_comment_1.path.clone();
669 let read_comment_views_parent_max_depth = CommentQuery::builder()
671 .post_id(Some(data.inserted_post.id))
672 .parent_path(Some(child_path))
674 .sort(Some(CommentSortType::New))
679 // Make sure a depth limited one, and given child comment 1, has 3
680 assert!(read_comment_views_parent_max_depth[2]
684 assert_eq!(3, read_comment_views_parent_max_depth.len());
691 fn test_languages() {
692 let conn = &mut establish_unpooled_connection();
693 let data = init_data(conn);
695 // by default, user has all languages enabled and should see all comments
696 // (except from blocked user)
697 let all_languages = CommentQuery::builder()
699 .local_user(Some(&data.inserted_local_user))
703 assert_eq!(5, all_languages.len());
705 // change user lang to finnish, should only show single finnish comment
706 let finnish_id = Language::read_id_from_code(conn, "fi").unwrap();
707 LocalUserLanguage::update(conn, vec![finnish_id], data.inserted_local_user.id).unwrap();
708 let finnish_comment = CommentQuery::builder()
710 .local_user(Some(&data.inserted_local_user))
714 assert_eq!(1, finnish_comment.len());
716 data.inserted_comment_2.content,
717 finnish_comment[0].comment.content
719 assert_eq!(finnish_id, finnish_comment[0].comment.language_id);
721 // now show all comments with undetermined language (which is the default value)
722 let undetermined_id = Language::read_id_from_code(conn, "und").unwrap();
723 LocalUserLanguage::update(conn, vec![undetermined_id], data.inserted_local_user.id).unwrap();
724 let undetermined_comment = CommentQuery::builder()
726 .local_user(Some(&data.inserted_local_user))
730 assert_eq!(3, undetermined_comment.len());
735 fn cleanup(data: Data, conn: &mut PgConnection) {
736 CommentLike::remove(conn, data.inserted_person.id, data.inserted_comment_0.id).unwrap();
737 Comment::delete(conn, data.inserted_comment_0.id).unwrap();
738 Comment::delete(conn, data.inserted_comment_1.id).unwrap();
739 Post::delete(conn, data.inserted_post.id).unwrap();
740 Community::delete(conn, data.inserted_community.id).unwrap();
741 Person::delete(conn, data.inserted_person.id).unwrap();
742 Person::delete(conn, data.inserted_person_2.id).unwrap();
743 Instance::delete(conn, data.inserted_instance.id).unwrap();
746 fn expected_comment_view(data: &Data, conn: &mut PgConnection) -> CommentView {
747 let agg = CommentAggregates::read(conn, data.inserted_comment_0.id).unwrap();
749 creator_banned_from_community: false,
751 subscribed: SubscribedType::NotSubscribed,
753 creator_blocked: false,
755 id: data.inserted_comment_0.id,
756 content: "Comment 0".into(),
757 creator_id: data.inserted_person.id,
758 post_id: data.inserted_post.id,
761 published: data.inserted_comment_0.published,
762 ap_id: data.inserted_comment_0.ap_id.clone(),
765 distinguished: false,
766 path: data.inserted_comment_0.to_owned().path,
767 language_id: LanguageId(0),
769 creator: PersonSafe {
770 id: data.inserted_person.id,
771 name: "timmy".into(),
773 published: data.inserted_person.published,
775 actor_id: data.inserted_person.actor_id.to_owned(),
784 inbox_url: data.inserted_person.inbox_url.to_owned(),
785 shared_inbox_url: None,
786 matrix_user_id: None,
788 instance_id: data.inserted_instance.id,
791 id: data.inserted_post.id,
792 name: data.inserted_post.name.to_owned(),
793 creator_id: data.inserted_person.id,
796 published: data.inserted_post.published,
798 community_id: data.inserted_community.id,
805 embed_description: None,
806 embed_video_url: None,
808 ap_id: data.inserted_post.ap_id.to_owned(),
810 language_id: Default::default(),
812 community: CommunitySafe {
813 id: data.inserted_community.id,
814 name: "test community 5".to_string(),
819 actor_id: data.inserted_community.actor_id.to_owned(),
821 title: "nada".to_owned(),
826 posting_restricted_to_mods: false,
827 published: data.inserted_community.published,
828 instance_id: data.inserted_instance.id,
830 counts: CommentAggregates {
832 comment_id: data.inserted_comment_0.id,
836 published: agg.published,