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(),
150 pub struct CommentQueryBuilder<'a> {
151 conn: &'a PgConnection,
152 listing_type: ListingType,
154 community_id: Option<i32>,
155 community_name: Option<String>,
156 post_id: Option<i32>,
157 creator_id: Option<i32>,
158 recipient_id: Option<i32>,
159 my_user_id: Option<i32>,
160 search_term: Option<String>,
167 impl<'a> CommentQueryBuilder<'a> {
168 pub fn create(conn: &'a PgConnection) -> Self {
169 CommentQueryBuilder {
171 listing_type: ListingType::All,
172 sort: &SortType::New,
174 community_name: None,
187 pub fn listing_type(mut self, listing_type: ListingType) -> Self {
188 self.listing_type = listing_type;
192 pub fn sort(mut self, sort: &'a SortType) -> Self {
197 pub fn post_id<T: MaybeOptional<i32>>(mut self, post_id: T) -> Self {
198 self.post_id = post_id.get_optional();
202 pub fn creator_id<T: MaybeOptional<i32>>(mut self, creator_id: T) -> Self {
203 self.creator_id = creator_id.get_optional();
207 pub fn recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
208 self.recipient_id = recipient_id.get_optional();
212 pub fn community_id<T: MaybeOptional<i32>>(mut self, community_id: T) -> Self {
213 self.community_id = community_id.get_optional();
217 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
218 self.my_user_id = my_user_id.get_optional();
222 pub fn community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
223 self.community_name = community_name.get_optional();
227 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
228 self.search_term = search_term.get_optional();
232 pub fn saved_only(mut self, saved_only: bool) -> Self {
233 self.saved_only = saved_only;
237 pub fn unread_only(mut self, unread_only: bool) -> Self {
238 self.unread_only = unread_only;
242 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
243 self.page = page.get_optional();
247 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
248 self.limit = limit.get_optional();
252 pub fn list(self) -> Result<Vec<CommentView>, Error> {
255 // The left join below will return None in this case
256 let user_id_join = self.my_user_id.unwrap_or(-1);
258 let mut query = comment::table
259 .inner_join(user_::table)
261 .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
262 .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
263 .inner_join(post::table)
264 .inner_join(community::table.on(post::community_id.eq(community::id)))
265 .inner_join(comment_aggregates::table)
267 community_user_ban::table.on(
269 .eq(community_user_ban::community_id)
270 .and(community_user_ban::user_id.eq(comment::creator_id)),
274 community_follower::table.on(
276 .eq(community_follower::community_id)
277 .and(community_follower::user_id.eq(user_id_join)),
281 comment_saved::table.on(
283 .eq(comment_saved::comment_id)
284 .and(comment_saved::user_id.eq(user_id_join)),
288 comment_like::table.on(
290 .eq(comment_like::comment_id)
291 .and(comment_like::user_id.eq(user_id_join)),
295 comment::all_columns,
296 User_::safe_columns_tuple(),
297 comment_alias_1::all_columns.nullable(),
298 UserAlias1::safe_columns_tuple().nullable(),
300 Community::safe_columns_tuple(),
301 comment_aggregates::all_columns,
302 community_user_ban::all_columns.nullable(),
303 community_follower::all_columns.nullable(),
304 comment_saved::all_columns.nullable(),
305 comment_like::score.nullable(),
310 if let Some(recipient_id) = self.recipient_id {
312 // TODO needs lots of testing
313 .filter(user_alias_1::id.eq(recipient_id))
314 .filter(comment::deleted.eq(false))
315 .filter(comment::removed.eq(false));
318 if self.unread_only {
319 query = query.filter(comment::read.eq(false));
322 if let Some(creator_id) = self.creator_id {
323 query = query.filter(comment::creator_id.eq(creator_id));
326 if let Some(community_id) = self.community_id {
327 query = query.filter(post::community_id.eq(community_id));
330 if let Some(community_name) = self.community_name {
332 .filter(community::name.eq(community_name))
333 .filter(comment::local.eq(true));
336 if let Some(post_id) = self.post_id {
337 query = query.filter(comment::post_id.eq(post_id));
340 if let Some(search_term) = self.search_term {
341 query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
344 query = match self.listing_type {
345 // ListingType::Subscribed => query.filter(community_follower::subscribed.eq(true)),
346 ListingType::Subscribed => query.filter(community_follower::user_id.is_not_null()), // TODO could be this: and(community_follower::user_id.eq(user_id_join)),
347 ListingType::Local => query.filter(community::local.eq(true)),
352 query = query.filter(comment_saved::id.is_not_null());
355 query = match self.sort {
356 SortType::Hot | SortType::Active => query
357 .order_by(hot_rank(comment_aggregates::score, comment::published).desc())
358 .then_order_by(comment::published.desc()),
359 SortType::New => query.order_by(comment::published.desc()),
360 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
361 SortType::TopYear => query
362 .filter(comment::published.gt(now - 1.years()))
363 .order_by(comment_aggregates::score.desc()),
364 SortType::TopMonth => query
365 .filter(comment::published.gt(now - 1.months()))
366 .order_by(comment_aggregates::score.desc()),
367 SortType::TopWeek => query
368 .filter(comment::published.gt(now - 1.weeks()))
369 .order_by(comment_aggregates::score.desc()),
370 SortType::TopDay => query
371 .filter(comment::published.gt(now - 1.days()))
372 .order_by(comment_aggregates::score.desc()),
375 let (limit, offset) = limit_and_offset(self.page, self.limit);
377 // Note: deleted and removed comments are done on the front side
381 .load::<CommentViewTuple>(self.conn)?;
383 Ok(CommentView::to_vec(res))
387 impl ViewToVec for CommentView {
388 type DbTuple = CommentViewTuple;
389 fn to_vec(posts: Vec<Self::DbTuple>) -> Vec<Self> {
393 comment: a.0.to_owned(),
394 creator: a.1.to_owned(),
395 recipient: a.3.to_owned(),
396 post: a.4.to_owned(),
397 community: a.5.to_owned(),
398 counts: a.6.to_owned(),
399 creator_banned_from_community: a.7.is_some(),
400 subscribed: a.8.is_some(),
401 saved: a.9.is_some(),
404 .collect::<Vec<Self>>()
411 source::{comment::*, community::*, post::*, user::*},
412 tests::establish_unpooled_connection,
413 views::comment_view::*,
421 let conn = establish_unpooled_connection();
423 let new_user = UserForm {
424 name: "timmy".into(),
425 preferred_username: None,
426 password_encrypted: "nope".into(),
428 matrix_user_id: None,
436 theme: "browser".into(),
437 default_sort_type: SortType::Hot as i16,
438 default_listing_type: ListingType::Subscribed as i16,
439 lang: "browser".into(),
441 send_notifications_to_email: false,
447 last_refreshed_at: None,
450 let inserted_user = User_::create(&conn, &new_user).unwrap();
452 let new_community = CommunityForm {
453 name: "test community 5".to_string(),
454 title: "nada".to_owned(),
457 creator_id: inserted_user.id,
466 last_refreshed_at: None,
472 let inserted_community = Community::create(&conn, &new_community).unwrap();
474 let new_post = PostForm {
475 name: "A test post 2".into(),
476 creator_id: inserted_user.id,
479 community_id: inserted_community.id,
487 embed_description: None,
495 let inserted_post = Post::create(&conn, &new_post).unwrap();
497 let comment_form = CommentForm {
498 content: "A test comment 32".into(),
499 creator_id: inserted_user.id,
500 post_id: inserted_post.id,
511 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
513 let comment_like_form = CommentLikeForm {
514 comment_id: inserted_comment.id,
515 post_id: inserted_post.id,
516 user_id: inserted_user.id,
520 let _inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
522 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
524 let expected_comment_view_no_user = CommentView {
525 creator_banned_from_community: false,
530 id: inserted_comment.id,
531 content: "A test comment 32".into(),
532 creator_id: inserted_user.id,
533 post_id: inserted_post.id,
538 published: inserted_comment.published,
539 ap_id: inserted_comment.ap_id,
544 id: inserted_user.id,
545 name: "timmy".into(),
546 preferred_username: None,
547 published: inserted_user.published,
549 actor_id: inserted_user.actor_id.to_owned(),
557 matrix_user_id: None,
561 id: inserted_post.id,
562 name: inserted_post.name.to_owned(),
563 creator_id: inserted_user.id,
566 published: inserted_post.published,
568 community_id: inserted_community.id,
575 embed_description: None,
578 ap_id: inserted_post.ap_id.to_owned(),
581 community: CommunitySafe {
582 id: inserted_community.id,
583 name: "test community 5".to_string(),
588 actor_id: inserted_community.actor_id.to_owned(),
590 title: "nada".to_owned(),
592 creator_id: inserted_user.id,
596 published: inserted_community.published,
598 counts: CommentAggregates {
600 comment_id: inserted_comment.id,
607 let mut expected_comment_view_with_user = expected_comment_view_no_user.to_owned();
608 expected_comment_view_with_user.my_vote = Some(1);
610 let read_comment_views_no_user = CommentQueryBuilder::create(&conn)
611 .post_id(inserted_post.id)
615 let read_comment_views_with_user = CommentQueryBuilder::create(&conn)
616 .post_id(inserted_post.id)
617 .my_user_id(inserted_user.id)
621 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
622 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
623 Post::delete(&conn, inserted_post.id).unwrap();
624 Community::delete(&conn, inserted_community.id).unwrap();
625 User_::delete(&conn, inserted_user.id).unwrap();
627 assert_eq!(expected_comment_view_no_user, read_comment_views_no_user[0]);
629 expected_comment_view_with_user,
630 read_comment_views_with_user[0]
632 assert_eq!(1, num_deleted);
633 assert_eq!(1, like_removed);