Add creator_banned_from_community to vote_view. (#4568)

* Add creator_banned_from_community to vote_view.

- Fixes #4561

* Adding tests.
This commit is contained in:
Dessalines 2024-03-26 11:22:04 -04:00 committed by GitHub
parent 7929e77602
commit 945064726f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 6 deletions

View file

@ -211,5 +211,6 @@ pub struct CustomEmojiView {
/// A vote view for checking a post or comments votes. /// A vote view for checking a post or comments votes.
pub struct VoteView { pub struct VoteView {
pub creator: Person, pub creator: Person,
pub creator_banned_from_community: bool,
pub score: i16, pub score: i16,
} }

View file

@ -1,9 +1,16 @@
use crate::structs::VoteView; use crate::structs::VoteView;
use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel::{
result::Error,
BoolExpressionMethods,
ExpressionMethods,
JoinOnDsl,
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommentId, PostId}, 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}, utils::{get_conn, limit_and_offset, DbPool},
}; };
@ -19,8 +26,21 @@ impl VoteView {
post_like::table post_like::table
.inner_join(person::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)) .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) .order_by(post_like::score)
.limit(limit) .limit(limit)
.offset(offset) .offset(offset)
@ -39,8 +59,21 @@ impl VoteView {
comment_like::table comment_like::table
.inner_join(person::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)) .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) .order_by(comment_like::score)
.limit(limit) .limit(limit)
.offset(offset) .offset(offset)
@ -58,12 +91,12 @@ mod tests {
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm}, comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
community::{Community, CommunityInsertForm}, community::{Community, CommunityInsertForm, CommunityPersonBan, CommunityPersonBanForm},
instance::Instance, instance::Instance,
person::{Person, PersonInsertForm}, person::{Person, PersonInsertForm},
post::{Post, PostInsertForm, PostLike, PostLikeForm}, post::{Post, PostInsertForm, PostLike, PostLikeForm},
}, },
traits::{Crud, Likeable}, traits::{Bannable, Crud, Likeable},
utils::build_db_pool_for_tests, utils::build_db_pool_for_tests,
}; };
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
@ -139,10 +172,12 @@ mod tests {
let expected_post_vote_views = [ let expected_post_vote_views = [
VoteView { VoteView {
creator: inserted_sara.clone(), creator: inserted_sara.clone(),
creator_banned_from_community: false,
score: -1, score: -1,
}, },
VoteView { VoteView {
creator: inserted_timmy.clone(), creator: inserted_timmy.clone(),
creator_banned_from_community: false,
score: 1, score: 1,
}, },
]; ];
@ -177,10 +212,12 @@ mod tests {
let expected_comment_vote_views = [ let expected_comment_vote_views = [
VoteView { VoteView {
creator: inserted_timmy.clone(), creator: inserted_timmy.clone(),
creator_banned_from_community: false,
score: -1, score: -1,
}, },
VoteView { VoteView {
creator: inserted_sara.clone(), creator: inserted_sara.clone(),
creator_banned_from_community: false,
score: 1, score: 1,
}, },
]; ];
@ -190,6 +227,41 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!(read_comment_vote_views, expected_comment_vote_views); 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 // Cleanup
Instance::delete(pool, inserted_instance.id).await.unwrap(); Instance::delete(pool, inserted_instance.id).await.unwrap();
} }