2 aggregates::comment_aggregates::CommentAggregates,
12 use diesel::{result::Error, *};
13 use lemmy_db_schema::{
28 comment::{Comment, CommentAlias1, CommentSaved},
29 community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan},
31 user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
36 #[derive(Debug, PartialEq, Serialize, Clone)]
37 pub struct CommentView {
39 pub creator: UserSafe,
40 pub recipient: Option<UserSafeAlias1>, // Left joins to comment and user
42 pub community: CommunitySafe,
43 pub counts: CommentAggregates,
44 pub creator_banned_from_community: bool, // Left Join to CommunityUserBan
45 pub subscribed: bool, // Left join to CommunityFollower
46 pub saved: bool, // Left join to CommentSaved
47 pub my_vote: Option<i16>, // Left join to CommentLike
50 type CommentViewTuple = (
53 Option<CommentAlias1>,
54 Option<UserSafeAlias1>,
58 Option<CommunityUserBan>,
59 Option<CommunityFollower>,
68 my_user_id: Option<i32>,
69 ) -> Result<Self, Error> {
70 // The left join below will return None in this case
71 let user_id_join = my_user_id.unwrap_or(-1);
81 creator_banned_from_community,
87 .inner_join(user_::table)
89 .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
90 .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
91 .inner_join(post::table)
92 .inner_join(community::table.on(post::community_id.eq(community::id)))
93 .inner_join(comment_aggregates::table)
95 community_user_ban::table.on(
97 .eq(community_user_ban::community_id)
98 .and(community_user_ban::user_id.eq(comment::creator_id)),
102 community_follower::table.on(
104 .eq(community_follower::community_id)
105 .and(community_follower::user_id.eq(user_id_join)),
109 comment_saved::table.on(
111 .eq(comment_saved::comment_id)
112 .and(comment_saved::user_id.eq(user_id_join)),
116 comment_like::table.on(
118 .eq(comment_like::comment_id)
119 .and(comment_like::user_id.eq(user_id_join)),
123 comment::all_columns,
124 User_::safe_columns_tuple(),
125 comment_alias_1::all_columns.nullable(),
126 UserAlias1::safe_columns_tuple().nullable(),
128 Community::safe_columns_tuple(),
129 comment_aggregates::all_columns,
130 community_user_ban::all_columns.nullable(),
131 community_follower::all_columns.nullable(),
132 comment_saved::all_columns.nullable(),
133 comment_like::score.nullable(),
135 .first::<CommentViewTuple>(conn)?;
144 creator_banned_from_community: creator_banned_from_community.is_some(),
145 subscribed: subscribed.is_some(),
146 saved: saved.is_some(),
151 /// Gets the recipient user id.
152 /// If there is no parent comment, its the post creator
153 pub fn get_recipient_id(&self) -> i32 {
154 match &self.recipient {
155 Some(parent_commenter) => parent_commenter.id,
156 None => self.post.creator_id,
161 pub struct CommentQueryBuilder<'a> {
162 conn: &'a PgConnection,
163 listing_type: ListingType,
165 community_id: Option<i32>,
166 community_name: Option<String>,
167 post_id: Option<i32>,
168 creator_id: Option<i32>,
169 recipient_id: Option<i32>,
170 my_user_id: Option<i32>,
171 search_term: Option<String>,
178 impl<'a> CommentQueryBuilder<'a> {
179 pub fn create(conn: &'a PgConnection) -> Self {
180 CommentQueryBuilder {
182 listing_type: ListingType::All,
183 sort: &SortType::New,
185 community_name: None,
198 pub fn listing_type(mut self, listing_type: ListingType) -> Self {
199 self.listing_type = listing_type;
203 pub fn sort(mut self, sort: &'a SortType) -> Self {
208 pub fn post_id<T: MaybeOptional<i32>>(mut self, post_id: T) -> Self {
209 self.post_id = post_id.get_optional();
213 pub fn creator_id<T: MaybeOptional<i32>>(mut self, creator_id: T) -> Self {
214 self.creator_id = creator_id.get_optional();
218 pub fn recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
219 self.recipient_id = recipient_id.get_optional();
223 pub fn community_id<T: MaybeOptional<i32>>(mut self, community_id: T) -> Self {
224 self.community_id = community_id.get_optional();
228 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
229 self.my_user_id = my_user_id.get_optional();
233 pub fn community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
234 self.community_name = community_name.get_optional();
238 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
239 self.search_term = search_term.get_optional();
243 pub fn saved_only(mut self, saved_only: bool) -> Self {
244 self.saved_only = saved_only;
248 pub fn unread_only(mut self, unread_only: bool) -> Self {
249 self.unread_only = unread_only;
253 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
254 self.page = page.get_optional();
258 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
259 self.limit = limit.get_optional();
263 pub fn list(self) -> Result<Vec<CommentView>, Error> {
266 // The left join below will return None in this case
267 let user_id_join = self.my_user_id.unwrap_or(-1);
269 let mut query = comment::table
270 .inner_join(user_::table)
272 .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
273 .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
274 .inner_join(post::table)
275 .inner_join(community::table.on(post::community_id.eq(community::id)))
276 .inner_join(comment_aggregates::table)
278 community_user_ban::table.on(
280 .eq(community_user_ban::community_id)
281 .and(community_user_ban::user_id.eq(comment::creator_id)),
285 community_follower::table.on(
287 .eq(community_follower::community_id)
288 .and(community_follower::user_id.eq(user_id_join)),
292 comment_saved::table.on(
294 .eq(comment_saved::comment_id)
295 .and(comment_saved::user_id.eq(user_id_join)),
299 comment_like::table.on(
301 .eq(comment_like::comment_id)
302 .and(comment_like::user_id.eq(user_id_join)),
306 comment::all_columns,
307 User_::safe_columns_tuple(),
308 comment_alias_1::all_columns.nullable(),
309 UserAlias1::safe_columns_tuple().nullable(),
311 Community::safe_columns_tuple(),
312 comment_aggregates::all_columns,
313 community_user_ban::all_columns.nullable(),
314 community_follower::all_columns.nullable(),
315 comment_saved::all_columns.nullable(),
316 comment_like::score.nullable(),
321 if let Some(recipient_id) = self.recipient_id {
323 // TODO needs lots of testing
324 .filter(user_alias_1::id.eq(recipient_id))
325 .filter(comment::deleted.eq(false))
326 .filter(comment::removed.eq(false));
329 if self.unread_only {
330 query = query.filter(comment::read.eq(false));
333 if let Some(creator_id) = self.creator_id {
334 query = query.filter(comment::creator_id.eq(creator_id));
337 if let Some(community_id) = self.community_id {
338 query = query.filter(post::community_id.eq(community_id));
341 if let Some(community_name) = self.community_name {
343 .filter(community::name.eq(community_name))
344 .filter(comment::local.eq(true));
347 if let Some(post_id) = self.post_id {
348 query = query.filter(comment::post_id.eq(post_id));
351 if let Some(search_term) = self.search_term {
352 query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
355 query = match self.listing_type {
356 // ListingType::Subscribed => query.filter(community_follower::subscribed.eq(true)),
357 ListingType::Subscribed => query.filter(community_follower::user_id.is_not_null()), // TODO could be this: and(community_follower::user_id.eq(user_id_join)),
358 ListingType::Local => query.filter(community::local.eq(true)),
363 query = query.filter(comment_saved::id.is_not_null());
366 query = match self.sort {
367 SortType::Hot | SortType::Active => query
368 .order_by(hot_rank(comment_aggregates::score, comment::published).desc())
369 .then_order_by(comment::published.desc()),
370 SortType::New => query.order_by(comment::published.desc()),
371 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
372 SortType::TopYear => query
373 .filter(comment::published.gt(now - 1.years()))
374 .order_by(comment_aggregates::score.desc()),
375 SortType::TopMonth => query
376 .filter(comment::published.gt(now - 1.months()))
377 .order_by(comment_aggregates::score.desc()),
378 SortType::TopWeek => query
379 .filter(comment::published.gt(now - 1.weeks()))
380 .order_by(comment_aggregates::score.desc()),
381 SortType::TopDay => query
382 .filter(comment::published.gt(now - 1.days()))
383 .order_by(comment_aggregates::score.desc()),
386 let (limit, offset) = limit_and_offset(self.page, self.limit);
388 // Note: deleted and removed comments are done on the front side
392 .load::<CommentViewTuple>(self.conn)?;
394 Ok(CommentView::to_vec(res))
398 impl ViewToVec for CommentView {
399 type DbTuple = CommentViewTuple;
400 fn to_vec(posts: Vec<Self::DbTuple>) -> Vec<Self> {
404 comment: a.0.to_owned(),
405 creator: a.1.to_owned(),
406 recipient: a.3.to_owned(),
407 post: a.4.to_owned(),
408 community: a.5.to_owned(),
409 counts: a.6.to_owned(),
410 creator_banned_from_community: a.7.is_some(),
411 subscribed: a.8.is_some(),
412 saved: a.9.is_some(),
415 .collect::<Vec<Self>>()
421 use crate::{tests::establish_unpooled_connection, views::comment_view::*, Crud, Likeable, *};
422 use lemmy_db_schema::source::{comment::*, community::*, post::*, user::*};
426 let conn = establish_unpooled_connection();
428 let new_user = UserForm {
429 name: "timmy".into(),
430 preferred_username: None,
431 password_encrypted: "nope".into(),
433 matrix_user_id: None,
441 theme: "browser".into(),
442 default_sort_type: SortType::Hot as i16,
443 default_listing_type: ListingType::Subscribed as i16,
444 lang: "browser".into(),
446 send_notifications_to_email: false,
452 last_refreshed_at: None,
455 let inserted_user = User_::create(&conn, &new_user).unwrap();
457 let new_community = CommunityForm {
458 name: "test community 5".to_string(),
459 title: "nada".to_owned(),
462 creator_id: inserted_user.id,
471 last_refreshed_at: None,
477 let inserted_community = Community::create(&conn, &new_community).unwrap();
479 let new_post = PostForm {
480 name: "A test post 2".into(),
481 creator_id: inserted_user.id,
484 community_id: inserted_community.id,
492 embed_description: None,
500 let inserted_post = Post::create(&conn, &new_post).unwrap();
502 let comment_form = CommentForm {
503 content: "A test comment 32".into(),
504 creator_id: inserted_user.id,
505 post_id: inserted_post.id,
516 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
518 let comment_like_form = CommentLikeForm {
519 comment_id: inserted_comment.id,
520 post_id: inserted_post.id,
521 user_id: inserted_user.id,
525 let _inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
527 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
529 let expected_comment_view_no_user = CommentView {
530 creator_banned_from_community: false,
535 id: inserted_comment.id,
536 content: "A test comment 32".into(),
537 creator_id: inserted_user.id,
538 post_id: inserted_post.id,
543 published: inserted_comment.published,
544 ap_id: inserted_comment.ap_id,
549 id: inserted_user.id,
550 name: "timmy".into(),
551 preferred_username: None,
552 published: inserted_user.published,
554 actor_id: inserted_user.actor_id.to_owned(),
562 matrix_user_id: None,
566 id: inserted_post.id,
567 name: inserted_post.name.to_owned(),
568 creator_id: inserted_user.id,
571 published: inserted_post.published,
573 community_id: inserted_community.id,
580 embed_description: None,
583 ap_id: inserted_post.ap_id.to_owned(),
586 community: CommunitySafe {
587 id: inserted_community.id,
588 name: "test community 5".to_string(),
593 actor_id: inserted_community.actor_id.to_owned(),
595 title: "nada".to_owned(),
597 creator_id: inserted_user.id,
601 published: inserted_community.published,
603 counts: CommentAggregates {
605 comment_id: inserted_comment.id,
612 let mut expected_comment_view_with_user = expected_comment_view_no_user.to_owned();
613 expected_comment_view_with_user.my_vote = Some(1);
615 let read_comment_views_no_user = CommentQueryBuilder::create(&conn)
616 .post_id(inserted_post.id)
620 let read_comment_views_with_user = CommentQueryBuilder::create(&conn)
621 .post_id(inserted_post.id)
622 .my_user_id(inserted_user.id)
626 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
627 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
628 Post::delete(&conn, inserted_post.id).unwrap();
629 Community::delete(&conn, inserted_community.id).unwrap();
630 User_::delete(&conn, inserted_user.id).unwrap();
632 assert_eq!(expected_comment_view_no_user, read_comment_views_no_user[0]);
634 expected_comment_view_with_user,
635 read_comment_views_with_user[0]
637 assert_eq!(1, num_deleted);
638 assert_eq!(1, like_removed);