1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{
3 aggregates::comment_aggregates::CommentAggregates,
13 use lemmy_db_schema::{
30 comment::{Comment, CommentAlias1, CommentSaved},
31 community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
32 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
33 person_block::PersonBlock,
44 #[derive(Debug, PartialEq, Serialize, Clone)]
45 pub struct CommentView {
47 pub creator: PersonSafe,
48 pub recipient: Option<PersonSafeAlias1>, // Left joins to comment and person
50 pub community: CommunitySafe,
51 pub counts: CommentAggregates,
52 pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
53 pub subscribed: bool, // Left join to CommunityFollower
54 pub saved: bool, // Left join to CommentSaved
55 pub creator_blocked: bool, // Left join to PersonBlock
56 pub my_vote: Option<i16>, // Left join to CommentLike
59 type CommentViewTuple = (
62 Option<CommentAlias1>,
63 Option<PersonSafeAlias1>,
67 Option<CommunityPersonBan>,
68 Option<CommunityFollower>,
77 comment_id: CommentId,
78 my_person_id: Option<PersonId>,
79 ) -> Result<Self, Error> {
80 // The left join below will return None in this case
81 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
91 creator_banned_from_community,
98 .inner_join(person::table)
100 .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
101 .left_join(person_alias_1::table.on(person_alias_1::id.eq(comment_alias_1::creator_id)))
102 .inner_join(post::table)
103 .inner_join(community::table.on(post::community_id.eq(community::id)))
104 .inner_join(comment_aggregates::table)
106 community_person_ban::table.on(
108 .eq(community_person_ban::community_id)
109 .and(community_person_ban::person_id.eq(comment::creator_id)),
113 community_follower::table.on(
115 .eq(community_follower::community_id)
116 .and(community_follower::person_id.eq(person_id_join)),
120 comment_saved::table.on(
122 .eq(comment_saved::comment_id)
123 .and(comment_saved::person_id.eq(person_id_join)),
127 person_block::table.on(
129 .eq(person_block::target_id)
130 .and(person_block::person_id.eq(person_id_join)),
134 comment_like::table.on(
136 .eq(comment_like::comment_id)
137 .and(comment_like::person_id.eq(person_id_join)),
141 comment::all_columns,
142 Person::safe_columns_tuple(),
143 comment_alias_1::all_columns.nullable(),
144 PersonAlias1::safe_columns_tuple().nullable(),
146 Community::safe_columns_tuple(),
147 comment_aggregates::all_columns,
148 community_person_ban::all_columns.nullable(),
149 community_follower::all_columns.nullable(),
150 comment_saved::all_columns.nullable(),
151 person_block::all_columns.nullable(),
152 comment_like::score.nullable(),
154 .first::<CommentViewTuple>(conn)?;
156 // If a person is given, then my_vote, if None, should be 0, not null
157 // Necessary to differentiate between other person's votes
158 let my_vote = if my_person_id.is_some() && comment_like.is_none() {
171 creator_banned_from_community: creator_banned_from_community.is_some(),
172 subscribed: subscribed.is_some(),
173 saved: saved.is_some(),
174 creator_blocked: creator_blocked.is_some(),
179 /// Gets the recipient person id.
180 /// If there is no parent comment, its the post creator
181 pub fn get_recipient_id(&self) -> PersonId {
182 match &self.recipient {
183 Some(parent_commenter) => parent_commenter.id,
184 None => self.post.creator_id,
188 /// Gets the number of unread replies
189 pub fn get_unread_replies(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
194 .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
195 .left_join(person_alias_1::table.on(person_alias_1::id.eq(comment_alias_1::creator_id)))
196 .inner_join(post::table)
197 .inner_join(community::table.on(post::community_id.eq(community::id)))
199 person_block::table.on(
201 .eq(person_block::target_id)
202 .and(person_block::person_id.eq(my_person_id)),
206 community_block::table.on(
208 .eq(community_block::community_id)
209 .and(community_block::person_id.eq(my_person_id)),
212 .filter(person_alias_1::id.eq(my_person_id)) // Gets the comment replies
216 .and(post::creator_id.eq(my_person_id)),
217 ) // Gets the top level replies
218 .filter(comment::read.eq(false))
219 .filter(comment::deleted.eq(false))
220 .filter(comment::removed.eq(false))
221 // Don't show blocked communities or persons
222 .filter(community_block::person_id.is_null())
223 .filter(person_block::person_id.is_null())
224 .select(count(comment::id))
229 pub struct CommentQueryBuilder<'a> {
230 conn: &'a PgConnection,
231 listing_type: Option<ListingType>,
232 sort: Option<SortType>,
233 community_id: Option<CommunityId>,
234 community_actor_id: Option<DbUrl>,
235 post_id: Option<PostId>,
236 creator_id: Option<PersonId>,
237 recipient_id: Option<PersonId>,
238 my_person_id: Option<PersonId>,
239 search_term: Option<String>,
240 saved_only: Option<bool>,
241 unread_only: Option<bool>,
242 show_bot_accounts: Option<bool>,
247 impl<'a> CommentQueryBuilder<'a> {
248 pub fn create(conn: &'a PgConnection) -> Self {
249 CommentQueryBuilder {
254 community_actor_id: None,
262 show_bot_accounts: None,
268 pub fn listing_type<T: MaybeOptional<ListingType>>(mut self, listing_type: T) -> Self {
269 self.listing_type = listing_type.get_optional();
273 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
274 self.sort = sort.get_optional();
278 pub fn post_id<T: MaybeOptional<PostId>>(mut self, post_id: T) -> Self {
279 self.post_id = post_id.get_optional();
283 pub fn creator_id<T: MaybeOptional<PersonId>>(mut self, creator_id: T) -> Self {
284 self.creator_id = creator_id.get_optional();
288 pub fn recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
289 self.recipient_id = recipient_id.get_optional();
293 pub fn community_id<T: MaybeOptional<CommunityId>>(mut self, community_id: T) -> Self {
294 self.community_id = community_id.get_optional();
298 pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
299 self.my_person_id = my_person_id.get_optional();
303 pub fn community_actor_id<T: MaybeOptional<DbUrl>>(mut self, community_actor_id: T) -> Self {
304 self.community_actor_id = community_actor_id.get_optional();
308 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
309 self.search_term = search_term.get_optional();
313 pub fn saved_only<T: MaybeOptional<bool>>(mut self, saved_only: T) -> Self {
314 self.saved_only = saved_only.get_optional();
318 pub fn unread_only<T: MaybeOptional<bool>>(mut self, unread_only: T) -> Self {
319 self.unread_only = unread_only.get_optional();
323 pub fn show_bot_accounts<T: MaybeOptional<bool>>(mut self, show_bot_accounts: T) -> Self {
324 self.show_bot_accounts = show_bot_accounts.get_optional();
328 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
329 self.page = page.get_optional();
333 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
334 self.limit = limit.get_optional();
338 pub fn list(self) -> Result<Vec<CommentView>, Error> {
341 // The left join below will return None in this case
342 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
344 let mut query = comment::table
345 .inner_join(person::table)
347 .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
348 .left_join(person_alias_1::table.on(person_alias_1::id.eq(comment_alias_1::creator_id)))
349 .inner_join(post::table)
350 .inner_join(community::table.on(post::community_id.eq(community::id)))
351 .inner_join(comment_aggregates::table)
353 community_person_ban::table.on(
355 .eq(community_person_ban::community_id)
356 .and(community_person_ban::person_id.eq(comment::creator_id)),
360 community_follower::table.on(
362 .eq(community_follower::community_id)
363 .and(community_follower::person_id.eq(person_id_join)),
367 comment_saved::table.on(
369 .eq(comment_saved::comment_id)
370 .and(comment_saved::person_id.eq(person_id_join)),
374 person_block::table.on(
376 .eq(person_block::target_id)
377 .and(person_block::person_id.eq(person_id_join)),
381 community_block::table.on(
383 .eq(community_block::community_id)
384 .and(community_block::person_id.eq(person_id_join)),
388 comment_like::table.on(
390 .eq(comment_like::comment_id)
391 .and(comment_like::person_id.eq(person_id_join)),
395 comment::all_columns,
396 Person::safe_columns_tuple(),
397 comment_alias_1::all_columns.nullable(),
398 PersonAlias1::safe_columns_tuple().nullable(),
400 Community::safe_columns_tuple(),
401 comment_aggregates::all_columns,
402 community_person_ban::all_columns.nullable(),
403 community_follower::all_columns.nullable(),
404 comment_saved::all_columns.nullable(),
405 person_block::all_columns.nullable(),
406 comment_like::score.nullable(),
411 if let Some(recipient_id) = self.recipient_id {
413 // TODO needs lots of testing
414 .filter(person_alias_1::id.eq(recipient_id)) // Gets the comment replies
418 .and(post::creator_id.eq(recipient_id)),
419 ) // Gets the top level replies
420 .filter(comment::deleted.eq(false))
421 .filter(comment::removed.eq(false));
424 if self.unread_only.unwrap_or(false) {
425 query = query.filter(comment::read.eq(false));
428 if let Some(creator_id) = self.creator_id {
429 query = query.filter(comment::creator_id.eq(creator_id));
432 if let Some(community_id) = self.community_id {
433 query = query.filter(post::community_id.eq(community_id));
436 if let Some(community_actor_id) = self.community_actor_id {
437 query = query.filter(community::actor_id.eq(community_actor_id))
440 if let Some(post_id) = self.post_id {
441 query = query.filter(comment::post_id.eq(post_id));
444 if let Some(search_term) = self.search_term {
445 query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
448 if let Some(listing_type) = self.listing_type {
449 query = match listing_type {
450 ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
451 ListingType::Local => query.filter(community::local.eq(true)),
456 if self.saved_only.unwrap_or(false) {
457 query = query.filter(comment_saved::id.is_not_null());
460 if !self.show_bot_accounts.unwrap_or(true) {
461 query = query.filter(person::bot_account.eq(false));
464 query = match self.sort.unwrap_or(SortType::New) {
465 SortType::Hot | SortType::Active => query
466 .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
467 .then_order_by(comment_aggregates::published.desc()),
468 SortType::New | SortType::MostComments | SortType::NewComments => {
469 query.order_by(comment::published.desc())
471 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
472 SortType::TopYear => query
473 .filter(comment::published.gt(now - 1.years()))
474 .order_by(comment_aggregates::score.desc()),
475 SortType::TopMonth => query
476 .filter(comment::published.gt(now - 1.months()))
477 .order_by(comment_aggregates::score.desc()),
478 SortType::TopWeek => query
479 .filter(comment::published.gt(now - 1.weeks()))
480 .order_by(comment_aggregates::score.desc()),
481 SortType::TopDay => query
482 .filter(comment::published.gt(now - 1.days()))
483 .order_by(comment_aggregates::score.desc()),
486 // Don't show blocked communities or persons
487 if self.my_person_id.is_some() {
488 query = query.filter(community_block::person_id.is_null());
489 query = query.filter(person_block::person_id.is_null());
492 let (limit, offset) = limit_and_offset(self.page, self.limit);
494 // Note: deleted and removed comments are done on the front side
498 .load::<CommentViewTuple>(self.conn)?;
500 Ok(CommentView::from_tuple_to_vec(res))
504 impl ViewToVec for CommentView {
505 type DbTuple = CommentViewTuple;
506 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
510 comment: a.0.to_owned(),
511 creator: a.1.to_owned(),
512 recipient: a.3.to_owned(),
513 post: a.4.to_owned(),
514 community: a.5.to_owned(),
515 counts: a.6.to_owned(),
516 creator_banned_from_community: a.7.is_some(),
517 subscribed: a.8.is_some(),
518 saved: a.9.is_some(),
519 creator_blocked: a.10.is_some(),
522 .collect::<Vec<Self>>()
528 use crate::comment_view::*;
529 use lemmy_db_queries::{
530 aggregates::comment_aggregates::CommentAggregates,
531 establish_unpooled_connection,
536 use lemmy_db_schema::source::{
540 person_block::PersonBlockForm,
543 use serial_test::serial;
548 let conn = establish_unpooled_connection();
550 let new_person = PersonForm {
551 name: "timmy".into(),
552 ..PersonForm::default()
555 let inserted_person = Person::create(&conn, &new_person).unwrap();
557 let new_person_2 = PersonForm {
559 ..PersonForm::default()
562 let inserted_person_2 = Person::create(&conn, &new_person_2).unwrap();
564 let new_community = CommunityForm {
565 name: "test community 5".to_string(),
566 title: "nada".to_owned(),
567 ..CommunityForm::default()
570 let inserted_community = Community::create(&conn, &new_community).unwrap();
572 let new_post = PostForm {
573 name: "A test post 2".into(),
574 creator_id: inserted_person.id,
575 community_id: inserted_community.id,
576 ..PostForm::default()
579 let inserted_post = Post::create(&conn, &new_post).unwrap();
581 let comment_form = CommentForm {
582 content: "A test comment 32".into(),
583 creator_id: inserted_person.id,
584 post_id: inserted_post.id,
585 ..CommentForm::default()
588 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
590 let comment_form_2 = CommentForm {
591 content: "A test blocked comment".into(),
592 creator_id: inserted_person_2.id,
593 post_id: inserted_post.id,
594 parent_id: Some(inserted_comment.id),
595 ..CommentForm::default()
598 let inserted_comment_2 = Comment::create(&conn, &comment_form_2).unwrap();
600 let timmy_blocks_sara_form = PersonBlockForm {
601 person_id: inserted_person.id,
602 target_id: inserted_person_2.id,
605 let inserted_block = PersonBlock::block(&conn, &timmy_blocks_sara_form).unwrap();
607 let expected_block = PersonBlock {
608 id: inserted_block.id,
609 person_id: inserted_person.id,
610 target_id: inserted_person_2.id,
611 published: inserted_block.published,
614 assert_eq!(expected_block, inserted_block);
616 let comment_like_form = CommentLikeForm {
617 comment_id: inserted_comment.id,
618 post_id: inserted_post.id,
619 person_id: inserted_person.id,
623 let _inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
625 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
627 let expected_comment_view_no_person = CommentView {
628 creator_banned_from_community: false,
632 creator_blocked: false,
634 id: inserted_comment.id,
635 content: "A test comment 32".into(),
636 creator_id: inserted_person.id,
637 post_id: inserted_post.id,
642 published: inserted_comment.published,
643 ap_id: inserted_comment.ap_id,
647 creator: PersonSafe {
648 id: inserted_person.id,
649 name: "timmy".into(),
651 published: inserted_person.published,
653 actor_id: inserted_person.actor_id.to_owned(),
662 inbox_url: inserted_person.inbox_url.to_owned(),
663 shared_inbox_url: None,
664 matrix_user_id: None,
668 id: inserted_post.id,
669 name: inserted_post.name.to_owned(),
670 creator_id: inserted_person.id,
673 published: inserted_post.published,
675 community_id: inserted_community.id,
682 embed_description: None,
685 ap_id: inserted_post.ap_id.to_owned(),
688 community: CommunitySafe {
689 id: inserted_community.id,
690 name: "test community 5".to_string(),
695 actor_id: inserted_community.actor_id.to_owned(),
697 title: "nada".to_owned(),
701 published: inserted_community.published,
703 counts: CommentAggregates {
705 comment_id: inserted_comment.id,
709 published: agg.published,
713 let mut expected_comment_view_with_person = expected_comment_view_no_person.to_owned();
714 expected_comment_view_with_person.my_vote = Some(1);
716 let read_comment_views_no_person = CommentQueryBuilder::create(&conn)
717 .post_id(inserted_post.id)
721 let read_comment_views_with_person = CommentQueryBuilder::create(&conn)
722 .post_id(inserted_post.id)
723 .my_person_id(inserted_person.id)
727 let read_comment_from_blocked_person =
728 CommentView::read(&conn, inserted_comment_2.id, Some(inserted_person.id)).unwrap();
730 let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
731 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
732 Comment::delete(&conn, inserted_comment_2.id).unwrap();
733 Post::delete(&conn, inserted_post.id).unwrap();
734 Community::delete(&conn, inserted_community.id).unwrap();
735 Person::delete(&conn, inserted_person.id).unwrap();
736 Person::delete(&conn, inserted_person_2.id).unwrap();
738 // Make sure its 1, not showing the blocked comment
739 assert_eq!(1, read_comment_views_with_person.len());
742 expected_comment_view_no_person,
743 read_comment_views_no_person[1]
746 expected_comment_view_with_person,
747 read_comment_views_with_person[0]
750 // Make sure block set the creator blocked
751 assert!(read_comment_from_blocked_person.creator_blocked);
753 assert_eq!(1, num_deleted);
754 assert_eq!(1, like_removed);