From 945064726f9e3b4f8451ae319ee7154a27b65cc3 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 26 Mar 2024 11:22:04 -0400 Subject: [PATCH] Add creator_banned_from_community to vote_view. (#4568) * Add creator_banned_from_community to vote_view. - Fixes #4561 * Adding tests. --- crates/db_views/src/structs.rs | 1 + crates/db_views/src/vote_view.rs | 84 +++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/crates/db_views/src/structs.rs b/crates/db_views/src/structs.rs index cfaee591d..a290ca4a1 100644 --- a/crates/db_views/src/structs.rs +++ b/crates/db_views/src/structs.rs @@ -211,5 +211,6 @@ pub struct CustomEmojiView { /// A vote view for checking a post or comments votes. pub struct VoteView { pub creator: Person, + pub creator_banned_from_community: bool, pub score: i16, } diff --git a/crates/db_views/src/vote_view.rs b/crates/db_views/src/vote_view.rs index 26df3bdbb..a0441ff4e 100644 --- a/crates/db_views/src/vote_view.rs +++ b/crates/db_views/src/vote_view.rs @@ -1,9 +1,16 @@ use crate::structs::VoteView; -use diesel::{result::Error, ExpressionMethods, QueryDsl}; +use diesel::{ + result::Error, + BoolExpressionMethods, + ExpressionMethods, + JoinOnDsl, + NullableExpressionMethods, + QueryDsl, +}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::{CommentId, PostId}, - schema::{comment_like, person, post_like}, + schema::{comment_like, community_person_ban, person, post, post_like}, utils::{get_conn, limit_and_offset, DbPool}, }; @@ -19,8 +26,21 @@ impl VoteView { post_like::table .inner_join(person::table) + .inner_join(post::table) + // Join to community_person_ban to get creator_banned_from_community + .left_join( + community_person_ban::table.on( + post::community_id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(post_like::person_id)), + ), + ) .filter(post_like::post_id.eq(post_id)) - .select((person::all_columns, post_like::score)) + .select(( + person::all_columns, + community_person_ban::community_id.nullable().is_not_null(), + post_like::score, + )) .order_by(post_like::score) .limit(limit) .offset(offset) @@ -39,8 +59,21 @@ impl VoteView { comment_like::table .inner_join(person::table) + .inner_join(post::table) + // Join to community_person_ban to get creator_banned_from_community + .left_join( + community_person_ban::table.on( + post::community_id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment_like::person_id)), + ), + ) .filter(comment_like::comment_id.eq(comment_id)) - .select((person::all_columns, comment_like::score)) + .select(( + person::all_columns, + community_person_ban::community_id.nullable().is_not_null(), + comment_like::score, + )) .order_by(comment_like::score) .limit(limit) .offset(offset) @@ -58,12 +91,12 @@ mod tests { use lemmy_db_schema::{ source::{ comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm}, - community::{Community, CommunityInsertForm}, + community::{Community, CommunityInsertForm, CommunityPersonBan, CommunityPersonBanForm}, instance::Instance, person::{Person, PersonInsertForm}, post::{Post, PostInsertForm, PostLike, PostLikeForm}, }, - traits::{Crud, Likeable}, + traits::{Bannable, Crud, Likeable}, utils::build_db_pool_for_tests, }; use pretty_assertions::assert_eq; @@ -139,10 +172,12 @@ mod tests { let expected_post_vote_views = [ VoteView { creator: inserted_sara.clone(), + creator_banned_from_community: false, score: -1, }, VoteView { creator: inserted_timmy.clone(), + creator_banned_from_community: false, score: 1, }, ]; @@ -177,10 +212,12 @@ mod tests { let expected_comment_vote_views = [ VoteView { creator: inserted_timmy.clone(), + creator_banned_from_community: false, score: -1, }, VoteView { creator: inserted_sara.clone(), + creator_banned_from_community: false, score: 1, }, ]; @@ -190,6 +227,41 @@ mod tests { .unwrap(); assert_eq!(read_comment_vote_views, expected_comment_vote_views); + // Ban timmy from that community + let ban_timmy_form = CommunityPersonBanForm { + community_id: inserted_community.id, + person_id: inserted_timmy.id, + expires: None, + }; + CommunityPersonBan::ban(pool, &ban_timmy_form) + .await + .unwrap(); + + // Make sure creator_banned_from_community is true + let read_comment_vote_views_after_ban = + VoteView::list_for_comment(pool, inserted_comment.id, None, None) + .await + .unwrap(); + + assert!( + read_comment_vote_views_after_ban + .first() + .unwrap() + .creator_banned_from_community + ); + + let read_post_vote_views_after_ban = + VoteView::list_for_post(pool, inserted_post.id, None, None) + .await + .unwrap(); + + assert!( + read_post_vote_views_after_ban + .get(1) + .unwrap() + .creator_banned_from_community + ); + // Cleanup Instance::delete(pool, inserted_instance.id).await.unwrap(); }