From 46c7429429777de8b613611964753e6d473ed2d3 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 23 Nov 2021 10:54:30 -0500 Subject: [PATCH] Fixing duped report view for admins. Fixes #1933 (#1945) * Fixing duped report view for admins. Fixes #1933 * Fixing tests. --- crates/db_views/src/comment_report_view.rs | 59 +++++++++++-------- crates/db_views/src/post_report_view.rs | 59 +++++++++++-------- .../down.sql | 2 + .../up.sql | 2 + 4 files changed, 74 insertions(+), 48 deletions(-) create mode 100644 migrations/2021-11-23-031528_add_report_published_index/down.sql create mode 100644 migrations/2021-11-23-031528_add_report_published_index/up.sql diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 5b39a5eb5..343851751 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -147,22 +147,28 @@ impl CommentReportView { let mut query = comment_report::table .inner_join(comment::table) .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join( - community_moderator::table.on(community_moderator::community_id.eq(post::community_id)), - ) .filter(comment_report::resolved.eq(false)) .into_boxed(); - // If its not an admin, get only the ones you mod - if !admin { - query = query.filter(community_moderator::person_id.eq(my_person_id)); - } - if let Some(community_id) = community_id { query = query.filter(post::community_id.eq(community_id)) } - query.select(count(comment_report::id)).first::(conn) + // If its not an admin, get only the ones you mod + if !admin { + query + .inner_join( + community_moderator::table.on( + community_moderator::community_id + .eq(post::community_id) + .and(community_moderator::person_id.eq(my_person_id)), + ), + ) + .select(count(comment_report::id)) + .first::(conn) + } else { + query.select(count(comment_report::id)).first::(conn) + } } } @@ -216,10 +222,6 @@ impl<'a> CommentReportQueryBuilder<'a> { .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id))) - // Test this join - .inner_join( - community_moderator::table.on(community_moderator::community_id.eq(post::community_id)), - ) .inner_join( comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), ) @@ -254,11 +256,6 @@ impl<'a> CommentReportQueryBuilder<'a> { )) .into_boxed(); - // If its not an admin, get only the ones you mod - if !self.admin { - query = query.filter(community_moderator::person_id.eq(self.my_person_id)); - } - if let Some(community_id) = self.community_id { query = query.filter(post::community_id.eq(community_id)); } @@ -269,11 +266,25 @@ impl<'a> CommentReportQueryBuilder<'a> { let (limit, offset) = limit_and_offset(self.page, self.limit); - let res = query - .order_by(comment_report::published.asc()) + query = query + .order_by(comment_report::published.desc()) .limit(limit) - .offset(offset) - .load::(self.conn)?; + .offset(offset); + + // If its not an admin, get only the ones you mod + let res = if !self.admin { + query + .inner_join( + community_moderator::table.on( + community_moderator::community_id + .eq(post::community_id) + .and(community_moderator::person_id.eq(self.my_person_id)), + ), + ) + .load::(self.conn)? + } else { + query.load::(self.conn)? + }; Ok(CommentReportView::from_tuple_to_vec(res)) } @@ -498,8 +509,8 @@ mod tests { assert_eq!( reports, [ - expected_sara_report_view.to_owned(), - expected_jessica_report_view.to_owned() + expected_jessica_report_view.to_owned(), + expected_sara_report_view.to_owned() ] ); diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index f27ba50aa..e5594487c 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -131,23 +131,28 @@ impl PostReportView { use diesel::dsl::*; let mut query = post_report::table .inner_join(post::table) - // Test this join - .inner_join( - community_moderator::table.on(community_moderator::community_id.eq(post::community_id)), - ) .filter(post_report::resolved.eq(false)) .into_boxed(); - // If its not an admin, get only the ones you mod - if !admin { - query = query.filter(community_moderator::person_id.eq(my_person_id)); - } - if let Some(community_id) = community_id { query = query.filter(post::community_id.eq(community_id)) } - query.select(count(post_report::id)).first::(conn) + // If its not an admin, get only the ones you mod + if !admin { + query + .inner_join( + community_moderator::table.on( + community_moderator::community_id + .eq(post::community_id) + .and(community_moderator::person_id.eq(my_person_id)), + ), + ) + .select(count(post_report::id)) + .first::(conn) + } else { + query.select(count(post_report::id)).first::(conn) + } } } @@ -200,9 +205,6 @@ impl<'a> PostReportQueryBuilder<'a> { .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(post_report::creator_id.eq(person::id))) .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id))) - .inner_join( - community_moderator::table.on(community_moderator::community_id.eq(post::community_id)), - ) .left_join( community_person_ban::table.on( post::community_id @@ -234,11 +236,6 @@ impl<'a> PostReportQueryBuilder<'a> { )) .into_boxed(); - // If its not an admin, get only the ones you mod - if !self.admin { - query = query.filter(community_moderator::person_id.eq(self.my_person_id)); - } - if let Some(community_id) = self.community_id { query = query.filter(post::community_id.eq(community_id)); } @@ -249,11 +246,25 @@ impl<'a> PostReportQueryBuilder<'a> { let (limit, offset) = limit_and_offset(self.page, self.limit); - let res = query - .order_by(post_report::published.asc()) + query = query + .order_by(post_report::published.desc()) .limit(limit) - .offset(offset) - .load::(self.conn)?; + .offset(offset); + + // If its not an admin, get only the ones you mod + let res = if !self.admin { + query + .inner_join( + community_moderator::table.on( + community_moderator::community_id + .eq(post::community_id) + .and(community_moderator::person_id.eq(self.my_person_id)), + ), + ) + .load::(self.conn)? + } else { + query.load::(self.conn)? + }; Ok(PostReportView::from_tuple_to_vec(res)) } @@ -481,8 +492,8 @@ mod tests { assert_eq!( reports, [ - expected_sara_report_view.to_owned(), - expected_jessica_report_view.to_owned() + expected_jessica_report_view.to_owned(), + expected_sara_report_view.to_owned() ] ); diff --git a/migrations/2021-11-23-031528_add_report_published_index/down.sql b/migrations/2021-11-23-031528_add_report_published_index/down.sql new file mode 100644 index 000000000..b5e9b51ca --- /dev/null +++ b/migrations/2021-11-23-031528_add_report_published_index/down.sql @@ -0,0 +1,2 @@ +drop index idx_comment_report_published; +drop index idx_post_report_published; diff --git a/migrations/2021-11-23-031528_add_report_published_index/up.sql b/migrations/2021-11-23-031528_add_report_published_index/up.sql new file mode 100644 index 000000000..eb0c1220c --- /dev/null +++ b/migrations/2021-11-23-031528_add_report_published_index/up.sql @@ -0,0 +1,2 @@ +create index idx_comment_report_published on comment_report (published desc); +create index idx_post_report_published on post_report (published desc);