Adding my_vote to report views

This commit is contained in:
Dessalines 2021-09-27 17:24:03 -04:00
parent 7b55aef0c6
commit 51c9e47f16
4 changed files with 86 additions and 15 deletions

View file

@ -62,7 +62,7 @@ impl Perform for CreateCommentReport {
.map_err(|_| ApiError::err("couldnt_create_report"))?; .map_err(|_| ApiError::err("couldnt_create_report"))?;
let comment_report_view = blocking(context.pool(), move |conn| { let comment_report_view = blocking(context.pool(), move |conn| {
CommentReportView::read(conn, report.id) CommentReportView::read(conn, report.id, person_id)
}) })
.await??; .await??;
@ -96,8 +96,9 @@ impl Perform for ResolveCommentReport {
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let report_id = data.report_id; let report_id = data.report_id;
let person_id = local_user_view.person.id;
let report = blocking(context.pool(), move |conn| { let report = blocking(context.pool(), move |conn| {
CommentReportView::read(conn, report_id) CommentReportView::read(conn, report_id, person_id)
}) })
.await??; .await??;
@ -119,7 +120,7 @@ impl Perform for ResolveCommentReport {
let report_id = data.report_id; let report_id = data.report_id;
let comment_report_view = blocking(context.pool(), move |conn| { let comment_report_view = blocking(context.pool(), move |conn| {
CommentReportView::read(conn, report_id) CommentReportView::read(conn, report_id, person_id)
}) })
.await??; .await??;

View file

@ -70,7 +70,7 @@ impl Perform for CreatePostReport {
.map_err(|_| ApiError::err("couldnt_create_report"))?; .map_err(|_| ApiError::err("couldnt_create_report"))?;
let post_report_view = blocking(context.pool(), move |conn| { let post_report_view = blocking(context.pool(), move |conn| {
PostReportView::read(conn, report.id) PostReportView::read(conn, report.id, person_id)
}) })
.await??; .await??;
@ -102,8 +102,9 @@ impl Perform for ResolvePostReport {
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let report_id = data.report_id; let report_id = data.report_id;
let person_id = local_user_view.person.id;
let report = blocking(context.pool(), move |conn| { let report = blocking(context.pool(), move |conn| {
PostReportView::read(conn, report_id) PostReportView::read(conn, report_id, person_id)
}) })
.await??; .await??;
@ -124,7 +125,7 @@ impl Perform for ResolvePostReport {
}; };
let post_report_view = blocking(context.pool(), move |conn| { let post_report_view = blocking(context.pool(), move |conn| {
PostReportView::read(conn, report_id) PostReportView::read(conn, report_id, person_id)
}) })
.await??; .await??;

View file

@ -10,6 +10,7 @@ use lemmy_db_schema::{
schema::{ schema::{
comment, comment,
comment_aggregates, comment_aggregates,
comment_like,
comment_report, comment_report,
community, community,
community_moderator, community_moderator,
@ -42,6 +43,7 @@ pub struct CommentReportView {
pub comment_creator: PersonSafeAlias1, pub comment_creator: PersonSafeAlias1,
pub counts: CommentAggregates, pub counts: CommentAggregates,
pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
pub my_vote: Option<i16>, // Left join to CommentLike
pub resolver: Option<PersonSafeAlias2>, pub resolver: Option<PersonSafeAlias2>,
} }
@ -54,6 +56,7 @@ type CommentReportViewTuple = (
PersonSafeAlias1, PersonSafeAlias1,
CommentAggregates, CommentAggregates,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<i16>,
Option<PersonSafeAlias2>, Option<PersonSafeAlias2>,
); );
@ -61,7 +64,11 @@ impl CommentReportView {
/// returns the CommentReportView for the provided report_id /// returns the CommentReportView for the provided report_id
/// ///
/// * `report_id` - the report id to obtain /// * `report_id` - the report id to obtain
pub fn read(conn: &PgConnection, report_id: CommentReportId) -> Result<Self, Error> { pub fn read(
conn: &PgConnection,
report_id: CommentReportId,
my_person_id: PersonId,
) -> Result<Self, Error> {
let ( let (
comment_report, comment_report,
comment, comment,
@ -71,6 +78,7 @@ impl CommentReportView {
comment_creator, comment_creator,
counts, counts,
creator_banned_from_community, creator_banned_from_community,
comment_like,
resolver, resolver,
) = comment_report::table ) = comment_report::table
.find(report_id) .find(report_id)
@ -89,6 +97,13 @@ impl CommentReportView {
.and(community_person_ban::person_id.eq(comment::creator_id)), .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( .left_join(
person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())), 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(), PersonAlias1::safe_columns_tuple(),
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
comment_like::score.nullable(),
PersonAlias2::safe_columns_tuple().nullable(), PersonAlias2::safe_columns_tuple().nullable(),
)) ))
.first::<CommentReportViewTuple>(conn)?; .first::<CommentReportViewTuple>(conn)?;
let my_vote = if comment_like.is_none() {
Some(0)
} else {
comment_like
};
Ok(Self { Ok(Self {
comment_report, comment_report,
comment, comment,
@ -114,6 +136,7 @@ impl CommentReportView {
comment_creator, comment_creator,
counts, counts,
creator_banned_from_community: creator_banned_from_community.is_some(), creator_banned_from_community: creator_banned_from_community.is_some(),
my_vote,
resolver, resolver,
}) })
} }
@ -219,6 +242,13 @@ impl<'a> CommentReportQueryBuilder<'a> {
.and(community_person_ban::person_id.eq(comment::creator_id)), .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( .left_join(
person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())), 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(), PersonAlias1::safe_columns_tuple(),
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
comment_like::score.nullable(),
PersonAlias2::safe_columns_tuple().nullable(), PersonAlias2::safe_columns_tuple().nullable(),
)) ))
.into_boxed(); .into_boxed();
@ -269,7 +300,8 @@ impl ViewToVec for CommentReportView {
comment_creator: a.5.to_owned(), comment_creator: a.5.to_owned(),
counts: a.6.to_owned(), counts: a.6.to_owned(),
creator_banned_from_community: a.7.is_some(), creator_banned_from_community: a.7.is_some(),
resolver: a.8.to_owned(), my_vote: a.8,
resolver: a.9.to_owned(),
}) })
.collect::<Vec<Self>>() .collect::<Vec<Self>>()
} }
@ -372,7 +404,7 @@ mod tests {
let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap(); let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
let read_jessica_report_view = 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 { let expected_jessica_report_view = CommentReportView {
comment_report: inserted_jessica_report.to_owned(), comment_report: inserted_jessica_report.to_owned(),
comment: inserted_comment.to_owned(), comment: inserted_comment.to_owned(),
@ -439,6 +471,7 @@ mod tests {
downvotes: 0, downvotes: 0,
published: agg.published, published: agg.published,
}, },
my_vote: None,
resolver: None, resolver: None,
}; };
@ -486,7 +519,7 @@ mod tests {
// Try to resolve the report // Try to resolve the report
CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap(); CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
let read_jessica_report_view_after_resolve = 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; let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
expected_jessica_report_view_after_resolve expected_jessica_report_view_after_resolve

View file

@ -16,6 +16,7 @@ use lemmy_db_schema::{
person_alias_2, person_alias_2,
post, post,
post_aggregates, post_aggregates,
post_like,
post_report, post_report,
}, },
source::{ source::{
@ -38,6 +39,7 @@ pub struct PostReportView {
pub creator: PersonSafe, pub creator: PersonSafe,
pub post_creator: PersonSafeAlias1, pub post_creator: PersonSafeAlias1,
pub creator_banned_from_community: bool, pub creator_banned_from_community: bool,
pub my_vote: Option<i16>,
pub counts: PostAggregates, pub counts: PostAggregates,
pub resolver: Option<PersonSafeAlias2>, pub resolver: Option<PersonSafeAlias2>,
} }
@ -49,6 +51,7 @@ type PostReportViewTuple = (
PersonSafe, PersonSafe,
PersonSafeAlias1, PersonSafeAlias1,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<i16>,
PostAggregates, PostAggregates,
Option<PersonSafeAlias2>, Option<PersonSafeAlias2>,
); );
@ -57,7 +60,11 @@ impl PostReportView {
/// returns the PostReportView for the provided report_id /// returns the PostReportView for the provided report_id
/// ///
/// * `report_id` - the report id to obtain /// * `report_id` - the report id to obtain
pub fn read(conn: &PgConnection, report_id: PostReportId) -> Result<Self, Error> { pub fn read(
conn: &PgConnection,
report_id: PostReportId,
my_person_id: PersonId,
) -> Result<Self, Error> {
let ( let (
post_report, post_report,
post, post,
@ -65,6 +72,7 @@ impl PostReportView {
creator, creator,
post_creator, post_creator,
creator_banned_from_community, creator_banned_from_community,
post_like,
counts, counts,
resolver, resolver,
) = post_report::table ) = post_report::table
@ -80,6 +88,13 @@ impl PostReportView {
.and(community_person_ban::person_id.eq(post::creator_id)), .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))) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
.left_join( .left_join(
person_alias_2::table.on(post_report::resolver_id.eq(person_alias_2::id.nullable())), 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(), Person::safe_columns_tuple(),
PersonAlias1::safe_columns_tuple(), PersonAlias1::safe_columns_tuple(),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
post_like::score.nullable(),
post_aggregates::all_columns, post_aggregates::all_columns,
PersonAlias2::safe_columns_tuple().nullable(), PersonAlias2::safe_columns_tuple().nullable(),
)) ))
.first::<PostReportViewTuple>(conn)?; .first::<PostReportViewTuple>(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 { Ok(Self {
post_report, post_report,
post, post,
@ -103,6 +127,7 @@ impl PostReportView {
creator, creator,
post_creator, post_creator,
creator_banned_from_community: creator_banned_from_community.is_some(), creator_banned_from_community: creator_banned_from_community.is_some(),
my_vote,
counts, counts,
resolver, resolver,
}) })
@ -202,6 +227,13 @@ impl<'a> PostReportQueryBuilder<'a> {
.and(community_person_ban::person_id.eq(post::creator_id)), .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))) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
.left_join( .left_join(
person_alias_2::table.on(post_report::resolver_id.eq(person_alias_2::id.nullable())), 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(), Person::safe_columns_tuple(),
PersonAlias1::safe_columns_tuple(), PersonAlias1::safe_columns_tuple(),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
post_like::score.nullable(),
post_aggregates::all_columns, post_aggregates::all_columns,
PersonAlias2::safe_columns_tuple().nullable(), PersonAlias2::safe_columns_tuple().nullable(),
)) ))
@ -250,8 +283,9 @@ impl ViewToVec for PostReportView {
creator: a.3.to_owned(), creator: a.3.to_owned(),
post_creator: a.4.to_owned(), post_creator: a.4.to_owned(),
creator_banned_from_community: a.5.is_some(), creator_banned_from_community: a.5.is_some(),
counts: a.6.to_owned(), my_vote: a.6,
resolver: a.7.to_owned(), counts: a.7.to_owned(),
resolver: a.8.to_owned(),
}) })
.collect::<Vec<Self>>() .collect::<Vec<Self>>()
} }
@ -353,7 +387,8 @@ mod tests {
let agg = PostAggregates::read(&conn, inserted_post.id).unwrap(); 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 { let expected_jessica_report_view = PostReportView {
post_report: inserted_jessica_report.to_owned(), post_report: inserted_jessica_report.to_owned(),
post: inserted_post.to_owned(), post: inserted_post.to_owned(),
@ -411,6 +446,7 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
}, },
creator_banned_from_community: false, creator_banned_from_community: false,
my_vote: None,
counts: PostAggregates { counts: PostAggregates {
id: agg.id, id: agg.id,
post_id: inserted_post.id, post_id: inserted_post.id,
@ -470,7 +506,7 @@ mod tests {
// Try to resolve the report // Try to resolve the report
PostReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap(); PostReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
let read_jessica_report_view_after_resolve = 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; let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
expected_jessica_report_view_after_resolve expected_jessica_report_view_after_resolve