2 aggregates::comment_aggregates::CommentAggregates,
20 comment::{Comment, CommentAlias1, CommentSaved},
21 community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan},
23 user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
31 use diesel::{result::Error, *};
34 #[derive(Debug, PartialEq, Serialize, Clone)]
35 pub struct CommentView {
37 pub creator: UserSafe,
38 pub recipient: Option<UserSafeAlias1>, // Left joins to comment and user
40 pub community: CommunitySafe,
41 pub counts: CommentAggregates,
42 pub creator_banned_from_community: bool, // Left Join to CommunityUserBan
43 pub subscribed: bool, // Left join to CommunityFollower
44 pub saved: bool, // Left join to CommentSaved
45 pub my_vote: Option<i16>, // Left join to CommentLike
48 type CommentViewTuple = (
51 Option<CommentAlias1>,
52 Option<UserSafeAlias1>,
56 Option<CommunityUserBan>,
57 Option<CommunityFollower>,
66 my_user_id: Option<i32>,
67 ) -> Result<Self, Error> {
68 // The left join below will return None in this case
69 let user_id_join = my_user_id.unwrap_or(-1);
79 creator_banned_from_community,
85 .inner_join(user_::table)
87 .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
88 .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
89 .inner_join(post::table)
90 .inner_join(community::table.on(post::community_id.eq(community::id)))
91 .inner_join(comment_aggregates::table)
93 community_user_ban::table.on(
95 .eq(community_user_ban::community_id)
96 .and(community_user_ban::user_id.eq(comment::creator_id)),
100 community_follower::table.on(
102 .eq(community_follower::community_id)
103 .and(community_follower::user_id.eq(user_id_join)),
107 comment_saved::table.on(
109 .eq(comment_saved::comment_id)
110 .and(comment_saved::user_id.eq(user_id_join)),
114 comment_like::table.on(
116 .eq(comment_like::comment_id)
117 .and(comment_like::user_id.eq(user_id_join)),
121 comment::all_columns,
122 User_::safe_columns_tuple(),
123 comment_alias_1::all_columns.nullable(),
124 UserAlias1::safe_columns_tuple().nullable(),
126 Community::safe_columns_tuple(),
127 comment_aggregates::all_columns,
128 community_user_ban::all_columns.nullable(),
129 community_follower::all_columns.nullable(),
130 comment_saved::all_columns.nullable(),
131 comment_like::score.nullable(),
133 .first::<CommentViewTuple>(conn)?;
142 creator_banned_from_community: creator_banned_from_community.is_some(),
143 subscribed: subscribed.is_some(),
144 saved: saved.is_some(),
149 /// Gets the recipient user id.
150 /// If there is no parent comment, its the post creator
151 pub fn get_recipient_id(&self) -> i32 {
152 match &self.recipient {
153 Some(parent_commenter) => parent_commenter.id,
154 None => self.post.creator_id,
159 pub struct CommentQueryBuilder<'a> {
160 conn: &'a PgConnection,
161 listing_type: ListingType,
163 community_id: Option<i32>,
164 community_name: Option<String>,
165 post_id: Option<i32>,
166 creator_id: Option<i32>,
167 recipient_id: Option<i32>,
168 my_user_id: Option<i32>,
169 search_term: Option<String>,
176 impl<'a> CommentQueryBuilder<'a> {
177 pub fn create(conn: &'a PgConnection) -> Self {
178 CommentQueryBuilder {
180 listing_type: ListingType::All,
181 sort: &SortType::New,
183 community_name: None,
196 pub fn listing_type(mut self, listing_type: ListingType) -> Self {
197 self.listing_type = listing_type;
201 pub fn sort(mut self, sort: &'a SortType) -> Self {
206 pub fn post_id<T: MaybeOptional<i32>>(mut self, post_id: T) -> Self {
207 self.post_id = post_id.get_optional();
211 pub fn creator_id<T: MaybeOptional<i32>>(mut self, creator_id: T) -> Self {
212 self.creator_id = creator_id.get_optional();
216 pub fn recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
217 self.recipient_id = recipient_id.get_optional();
221 pub fn community_id<T: MaybeOptional<i32>>(mut self, community_id: T) -> Self {
222 self.community_id = community_id.get_optional();
226 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
227 self.my_user_id = my_user_id.get_optional();
231 pub fn community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
232 self.community_name = community_name.get_optional();
236 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
237 self.search_term = search_term.get_optional();
241 pub fn saved_only(mut self, saved_only: bool) -> Self {
242 self.saved_only = saved_only;
246 pub fn unread_only(mut self, unread_only: bool) -> Self {
247 self.unread_only = unread_only;
251 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
252 self.page = page.get_optional();
256 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
257 self.limit = limit.get_optional();
261 pub fn list(self) -> Result<Vec<CommentView>, Error> {
264 // The left join below will return None in this case
265 let user_id_join = self.my_user_id.unwrap_or(-1);
267 let mut query = comment::table
268 .inner_join(user_::table)
270 .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
271 .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
272 .inner_join(post::table)
273 .inner_join(community::table.on(post::community_id.eq(community::id)))
274 .inner_join(comment_aggregates::table)
276 community_user_ban::table.on(
278 .eq(community_user_ban::community_id)
279 .and(community_user_ban::user_id.eq(comment::creator_id)),
283 community_follower::table.on(
285 .eq(community_follower::community_id)
286 .and(community_follower::user_id.eq(user_id_join)),
290 comment_saved::table.on(
292 .eq(comment_saved::comment_id)
293 .and(comment_saved::user_id.eq(user_id_join)),
297 comment_like::table.on(
299 .eq(comment_like::comment_id)
300 .and(comment_like::user_id.eq(user_id_join)),
304 comment::all_columns,
305 User_::safe_columns_tuple(),
306 comment_alias_1::all_columns.nullable(),
307 UserAlias1::safe_columns_tuple().nullable(),
309 Community::safe_columns_tuple(),
310 comment_aggregates::all_columns,
311 community_user_ban::all_columns.nullable(),
312 community_follower::all_columns.nullable(),
313 comment_saved::all_columns.nullable(),
314 comment_like::score.nullable(),
319 if let Some(recipient_id) = self.recipient_id {
321 // TODO needs lots of testing
322 .filter(user_alias_1::id.eq(recipient_id))
323 .filter(comment::deleted.eq(false))
324 .filter(comment::removed.eq(false));
327 if self.unread_only {
328 query = query.filter(comment::read.eq(false));
331 if let Some(creator_id) = self.creator_id {
332 query = query.filter(comment::creator_id.eq(creator_id));
335 if let Some(community_id) = self.community_id {
336 query = query.filter(post::community_id.eq(community_id));
339 if let Some(community_name) = self.community_name {
341 .filter(community::name.eq(community_name))
342 .filter(comment::local.eq(true));
345 if let Some(post_id) = self.post_id {
346 query = query.filter(comment::post_id.eq(post_id));
349 if let Some(search_term) = self.search_term {
350 query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
353 query = match self.listing_type {
354 // ListingType::Subscribed => query.filter(community_follower::subscribed.eq(true)),
355 ListingType::Subscribed => query.filter(community_follower::user_id.is_not_null()), // TODO could be this: and(community_follower::user_id.eq(user_id_join)),
356 ListingType::Local => query.filter(community::local.eq(true)),
361 query = query.filter(comment_saved::id.is_not_null());
364 query = match self.sort {
365 SortType::Hot | SortType::Active => query
366 .order_by(hot_rank(comment_aggregates::score, comment::published).desc())
367 .then_order_by(comment::published.desc()),
368 SortType::New => query.order_by(comment::published.desc()),
369 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
370 SortType::TopYear => query
371 .filter(comment::published.gt(now - 1.years()))
372 .order_by(comment_aggregates::score.desc()),
373 SortType::TopMonth => query
374 .filter(comment::published.gt(now - 1.months()))
375 .order_by(comment_aggregates::score.desc()),
376 SortType::TopWeek => query
377 .filter(comment::published.gt(now - 1.weeks()))
378 .order_by(comment_aggregates::score.desc()),
379 SortType::TopDay => query
380 .filter(comment::published.gt(now - 1.days()))
381 .order_by(comment_aggregates::score.desc()),
384 let (limit, offset) = limit_and_offset(self.page, self.limit);
386 // Note: deleted and removed comments are done on the front side
390 .load::<CommentViewTuple>(self.conn)?;
392 Ok(CommentView::to_vec(res))
396 impl ViewToVec for CommentView {
397 type DbTuple = CommentViewTuple;
398 fn to_vec(posts: Vec<Self::DbTuple>) -> Vec<Self> {
402 comment: a.0.to_owned(),
403 creator: a.1.to_owned(),
404 recipient: a.3.to_owned(),
405 post: a.4.to_owned(),
406 community: a.5.to_owned(),
407 counts: a.6.to_owned(),
408 creator_banned_from_community: a.7.is_some(),
409 subscribed: a.8.is_some(),
410 saved: a.9.is_some(),
413 .collect::<Vec<Self>>()
420 source::{comment::*, community::*, post::*, user::*},
421 tests::establish_unpooled_connection,
422 views::comment_view::*,
430 let conn = establish_unpooled_connection();
432 let new_user = UserForm {
433 name: "timmy".into(),
434 preferred_username: None,
435 password_encrypted: "nope".into(),
437 matrix_user_id: None,
445 theme: "browser".into(),
446 default_sort_type: SortType::Hot as i16,
447 default_listing_type: ListingType::Subscribed as i16,
448 lang: "browser".into(),
450 send_notifications_to_email: false,
456 last_refreshed_at: None,
459 let inserted_user = User_::create(&conn, &new_user).unwrap();
461 let new_community = CommunityForm {
462 name: "test community 5".to_string(),
463 title: "nada".to_owned(),
466 creator_id: inserted_user.id,
475 last_refreshed_at: None,
481 let inserted_community = Community::create(&conn, &new_community).unwrap();
483 let new_post = PostForm {
484 name: "A test post 2".into(),
485 creator_id: inserted_user.id,
488 community_id: inserted_community.id,
496 embed_description: None,
504 let inserted_post = Post::create(&conn, &new_post).unwrap();
506 let comment_form = CommentForm {
507 content: "A test comment 32".into(),
508 creator_id: inserted_user.id,
509 post_id: inserted_post.id,
520 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
522 let comment_like_form = CommentLikeForm {
523 comment_id: inserted_comment.id,
524 post_id: inserted_post.id,
525 user_id: inserted_user.id,
529 let _inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
531 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
533 let expected_comment_view_no_user = CommentView {
534 creator_banned_from_community: false,
539 id: inserted_comment.id,
540 content: "A test comment 32".into(),
541 creator_id: inserted_user.id,
542 post_id: inserted_post.id,
547 published: inserted_comment.published,
548 ap_id: inserted_comment.ap_id,
553 id: inserted_user.id,
554 name: "timmy".into(),
555 preferred_username: None,
556 published: inserted_user.published,
558 actor_id: inserted_user.actor_id.to_owned(),
566 matrix_user_id: None,
570 id: inserted_post.id,
571 name: inserted_post.name.to_owned(),
572 creator_id: inserted_user.id,
575 published: inserted_post.published,
577 community_id: inserted_community.id,
584 embed_description: None,
587 ap_id: inserted_post.ap_id.to_owned(),
590 community: CommunitySafe {
591 id: inserted_community.id,
592 name: "test community 5".to_string(),
597 actor_id: inserted_community.actor_id.to_owned(),
599 title: "nada".to_owned(),
601 creator_id: inserted_user.id,
605 published: inserted_community.published,
607 counts: CommentAggregates {
609 comment_id: inserted_comment.id,
616 let mut expected_comment_view_with_user = expected_comment_view_no_user.to_owned();
617 expected_comment_view_with_user.my_vote = Some(1);
619 let read_comment_views_no_user = CommentQueryBuilder::create(&conn)
620 .post_id(inserted_post.id)
624 let read_comment_views_with_user = CommentQueryBuilder::create(&conn)
625 .post_id(inserted_post.id)
626 .my_user_id(inserted_user.id)
630 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
631 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
632 Post::delete(&conn, inserted_post.id).unwrap();
633 Community::delete(&conn, inserted_community.id).unwrap();
634 User_::delete(&conn, inserted_user.id).unwrap();
636 assert_eq!(expected_comment_view_no_user, read_comment_views_no_user[0]);
638 expected_comment_view_with_user,
639 read_comment_views_with_user[0]
641 assert_eq!(1, num_deleted);
642 assert_eq!(1, like_removed);