From 51c9e47f1632d9591e1ec9012f2344bf49c89c45 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 27 Sep 2021 17:24:03 -0400 Subject: [PATCH] Adding my_vote to report views --- crates/api/src/comment_report.rs | 7 ++-- crates/api/src/post_report.rs | 7 ++-- crates/db_views/src/comment_report_view.rs | 41 +++++++++++++++++-- crates/db_views/src/post_report_view.rs | 46 +++++++++++++++++++--- 4 files changed, 86 insertions(+), 15 deletions(-) diff --git a/crates/api/src/comment_report.rs b/crates/api/src/comment_report.rs index 7bbbd6890..32a4a5d9b 100644 --- a/crates/api/src/comment_report.rs +++ b/crates/api/src/comment_report.rs @@ -62,7 +62,7 @@ impl Perform for CreateCommentReport { .map_err(|_| ApiError::err("couldnt_create_report"))?; let comment_report_view = blocking(context.pool(), move |conn| { - CommentReportView::read(conn, report.id) + CommentReportView::read(conn, report.id, person_id) }) .await??; @@ -96,8 +96,9 @@ impl Perform for ResolveCommentReport { get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; let report_id = data.report_id; + let person_id = local_user_view.person.id; let report = blocking(context.pool(), move |conn| { - CommentReportView::read(conn, report_id) + CommentReportView::read(conn, report_id, person_id) }) .await??; @@ -119,7 +120,7 @@ impl Perform for ResolveCommentReport { let report_id = data.report_id; let comment_report_view = blocking(context.pool(), move |conn| { - CommentReportView::read(conn, report_id) + CommentReportView::read(conn, report_id, person_id) }) .await??; diff --git a/crates/api/src/post_report.rs b/crates/api/src/post_report.rs index aad21bdbf..25af96ba6 100644 --- a/crates/api/src/post_report.rs +++ b/crates/api/src/post_report.rs @@ -70,7 +70,7 @@ impl Perform for CreatePostReport { .map_err(|_| ApiError::err("couldnt_create_report"))?; let post_report_view = blocking(context.pool(), move |conn| { - PostReportView::read(conn, report.id) + PostReportView::read(conn, report.id, person_id) }) .await??; @@ -102,8 +102,9 @@ impl Perform for ResolvePostReport { get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; let report_id = data.report_id; + let person_id = local_user_view.person.id; let report = blocking(context.pool(), move |conn| { - PostReportView::read(conn, report_id) + PostReportView::read(conn, report_id, person_id) }) .await??; @@ -124,7 +125,7 @@ impl Perform for ResolvePostReport { }; let post_report_view = blocking(context.pool(), move |conn| { - PostReportView::read(conn, report_id) + PostReportView::read(conn, report_id, person_id) }) .await??; diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index bdfc2c5ca..753b82fdd 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -10,6 +10,7 @@ use lemmy_db_schema::{ schema::{ comment, comment_aggregates, + comment_like, comment_report, community, community_moderator, @@ -42,6 +43,7 @@ pub struct CommentReportView { pub comment_creator: PersonSafeAlias1, pub counts: CommentAggregates, pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan + pub my_vote: Option, // Left join to CommentLike pub resolver: Option, } @@ -54,6 +56,7 @@ type CommentReportViewTuple = ( PersonSafeAlias1, CommentAggregates, Option, + Option, Option, ); @@ -61,7 +64,11 @@ impl CommentReportView { /// returns the CommentReportView for the provided report_id /// /// * `report_id` - the report id to obtain - pub fn read(conn: &PgConnection, report_id: CommentReportId) -> Result { + pub fn read( + conn: &PgConnection, + report_id: CommentReportId, + my_person_id: PersonId, + ) -> Result { let ( comment_report, comment, @@ -71,6 +78,7 @@ impl CommentReportView { comment_creator, counts, creator_banned_from_community, + comment_like, resolver, ) = comment_report::table .find(report_id) @@ -89,6 +97,13 @@ impl CommentReportView { .and(community_person_ban::person_id.eq(comment::creator_id)), ), ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person_id)), + ), + ) .left_join( person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())), ) @@ -101,10 +116,17 @@ impl CommentReportView { PersonAlias1::safe_columns_tuple(), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), PersonAlias2::safe_columns_tuple().nullable(), )) .first::(conn)?; + let my_vote = if comment_like.is_none() { + Some(0) + } else { + comment_like + }; + Ok(Self { comment_report, comment, @@ -114,6 +136,7 @@ impl CommentReportView { comment_creator, counts, creator_banned_from_community: creator_banned_from_community.is_some(), + my_vote, resolver, }) } @@ -219,6 +242,13 @@ impl<'a> CommentReportQueryBuilder<'a> { .and(community_person_ban::person_id.eq(comment::creator_id)), ), ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(self.my_person_id)), + ), + ) .left_join( person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())), ) @@ -231,6 +261,7 @@ impl<'a> CommentReportQueryBuilder<'a> { PersonAlias1::safe_columns_tuple(), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), PersonAlias2::safe_columns_tuple().nullable(), )) .into_boxed(); @@ -269,7 +300,8 @@ impl ViewToVec for CommentReportView { comment_creator: a.5.to_owned(), counts: a.6.to_owned(), creator_banned_from_community: a.7.is_some(), - resolver: a.8.to_owned(), + my_vote: a.8, + resolver: a.9.to_owned(), }) .collect::>() } @@ -372,7 +404,7 @@ mod tests { let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap(); let read_jessica_report_view = - CommentReportView::read(&conn, inserted_jessica_report.id).unwrap(); + CommentReportView::read(&conn, inserted_jessica_report.id, inserted_jessica.id).unwrap(); let expected_jessica_report_view = CommentReportView { comment_report: inserted_jessica_report.to_owned(), comment: inserted_comment.to_owned(), @@ -439,6 +471,7 @@ mod tests { downvotes: 0, published: agg.published, }, + my_vote: None, resolver: None, }; @@ -486,7 +519,7 @@ mod tests { // Try to resolve the report CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap(); let read_jessica_report_view_after_resolve = - CommentReportView::read(&conn, inserted_jessica_report.id).unwrap(); + CommentReportView::read(&conn, inserted_jessica_report.id, inserted_jessica.id).unwrap(); let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view; expected_jessica_report_view_after_resolve diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 40cfb45fc..79a10ac72 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -16,6 +16,7 @@ use lemmy_db_schema::{ person_alias_2, post, post_aggregates, + post_like, post_report, }, source::{ @@ -38,6 +39,7 @@ pub struct PostReportView { pub creator: PersonSafe, pub post_creator: PersonSafeAlias1, pub creator_banned_from_community: bool, + pub my_vote: Option, pub counts: PostAggregates, pub resolver: Option, } @@ -49,6 +51,7 @@ type PostReportViewTuple = ( PersonSafe, PersonSafeAlias1, Option, + Option, PostAggregates, Option, ); @@ -57,7 +60,11 @@ impl PostReportView { /// returns the PostReportView for the provided report_id /// /// * `report_id` - the report id to obtain - pub fn read(conn: &PgConnection, report_id: PostReportId) -> Result { + pub fn read( + conn: &PgConnection, + report_id: PostReportId, + my_person_id: PersonId, + ) -> Result { let ( post_report, post, @@ -65,6 +72,7 @@ impl PostReportView { creator, post_creator, creator_banned_from_community, + post_like, counts, resolver, ) = post_report::table @@ -80,6 +88,13 @@ impl PostReportView { .and(community_person_ban::person_id.eq(post::creator_id)), ), ) + .left_join( + post_like::table.on( + post::id + .eq(post_like::post_id) + .and(post_like::person_id.eq(my_person_id)), + ), + ) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) .left_join( person_alias_2::table.on(post_report::resolver_id.eq(person_alias_2::id.nullable())), @@ -91,11 +106,20 @@ impl PostReportView { Person::safe_columns_tuple(), PersonAlias1::safe_columns_tuple(), community_person_ban::all_columns.nullable(), + post_like::score.nullable(), post_aggregates::all_columns, PersonAlias2::safe_columns_tuple().nullable(), )) .first::(conn)?; + // If a person is given, then my_vote, if None, should be 0, not null + // Necessary to differentiate between other person's votes + let my_vote = if post_like.is_none() { + Some(0) + } else { + post_like + }; + Ok(Self { post_report, post, @@ -103,6 +127,7 @@ impl PostReportView { creator, post_creator, creator_banned_from_community: creator_banned_from_community.is_some(), + my_vote, counts, resolver, }) @@ -202,6 +227,13 @@ impl<'a> PostReportQueryBuilder<'a> { .and(community_person_ban::person_id.eq(post::creator_id)), ), ) + .left_join( + post_like::table.on( + post::id + .eq(post_like::post_id) + .and(post_like::person_id.eq(self.my_person_id)), + ), + ) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) .left_join( person_alias_2::table.on(post_report::resolver_id.eq(person_alias_2::id.nullable())), @@ -213,6 +245,7 @@ impl<'a> PostReportQueryBuilder<'a> { Person::safe_columns_tuple(), PersonAlias1::safe_columns_tuple(), community_person_ban::all_columns.nullable(), + post_like::score.nullable(), post_aggregates::all_columns, PersonAlias2::safe_columns_tuple().nullable(), )) @@ -250,8 +283,9 @@ impl ViewToVec for PostReportView { creator: a.3.to_owned(), post_creator: a.4.to_owned(), creator_banned_from_community: a.5.is_some(), - counts: a.6.to_owned(), - resolver: a.7.to_owned(), + my_vote: a.6, + counts: a.7.to_owned(), + resolver: a.8.to_owned(), }) .collect::>() } @@ -353,7 +387,8 @@ mod tests { let agg = PostAggregates::read(&conn, inserted_post.id).unwrap(); - let read_jessica_report_view = PostReportView::read(&conn, inserted_jessica_report.id).unwrap(); + let read_jessica_report_view = + PostReportView::read(&conn, inserted_jessica_report.id, inserted_jessica.id).unwrap(); let expected_jessica_report_view = PostReportView { post_report: inserted_jessica_report.to_owned(), post: inserted_post.to_owned(), @@ -411,6 +446,7 @@ mod tests { matrix_user_id: None, }, creator_banned_from_community: false, + my_vote: None, counts: PostAggregates { id: agg.id, post_id: inserted_post.id, @@ -470,7 +506,7 @@ mod tests { // Try to resolve the report PostReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap(); let read_jessica_report_view_after_resolve = - PostReportView::read(&conn, inserted_jessica_report.id).unwrap(); + PostReportView::read(&conn, inserted_jessica_report.id, inserted_jessica.id).unwrap(); let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view; expected_jessica_report_view_after_resolve