Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add creator_is_moderator to Comment and PostViews. Fixes #3347 #4087

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/db_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub mod newtypes;
pub mod schema;
#[cfg(feature = "full")]
pub mod aliases {
use crate::schema::person;
diesel::alias!(person as person1: Person1, person as person2: Person2);
use crate::schema::{community_moderator, person};
diesel::alias!(person as person1: Person1, person as person2: Person2, community_moderator as community_moderator1: CommunityModerator1);
}
pub mod source;
#[cfg(feature = "full")]
Expand Down
50 changes: 48 additions & 2 deletions crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use diesel::{
use diesel_async::RunQueryDsl;
use diesel_ltree::{nlevel, subpath, Ltree, LtreeExtensions};
use lemmy_db_schema::{
aliases,
newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId},
schema::{
comment,
Expand Down Expand Up @@ -90,6 +91,17 @@ fn queries<'a>() -> Queries<
.and(community_moderator::person_id.eq(person_id_join)),
),
)
.left_join(
dessalines marked this conversation as resolved.
Show resolved Hide resolved
aliases::community_moderator1.on(
community::id
.eq(aliases::community_moderator1.field(community_moderator::community_id))
.and(
aliases::community_moderator1
.field(community_moderator::person_id)
.eq(comment::creator_id),
),
),
)
};

let selection = (
Expand All @@ -99,6 +111,10 @@ fn queries<'a>() -> Queries<
community::all_columns,
comment_aggregates::all_columns,
community_person_ban::id.nullable().is_not_null(),
aliases::community_moderator1
.field(community_moderator::id)
.nullable()
.is_not_null(),
CommunityFollower::select_subscribed_type(),
comment_saved::id.nullable().is_not_null(),
person_block::id.nullable().is_not_null(),
Expand Down Expand Up @@ -338,15 +354,15 @@ mod tests {
source::{
actor_language::LocalUserLanguage,
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
community::{Community, CommunityInsertForm},
community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
instance::Instance,
language::Language,
local_user::{LocalUser, LocalUserInsertForm},
person::{Person, PersonInsertForm},
person_block::{PersonBlock, PersonBlockForm},
post::{Post, PostInsertForm},
},
traits::{Blockable, Crud, Likeable},
traits::{Blockable, Crud, Joinable, Likeable},
utils::build_db_pool_for_tests,
SubscribedType,
};
Expand Down Expand Up @@ -779,6 +795,35 @@ mod tests {
cleanup(data, pool).await;
}

#[tokio::test]
#[serial]
async fn test_creator_is_moderator() {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;

// Make one of the inserted persons a moderator
let person_id = data.inserted_person_2.id;
let community_id = data.inserted_community.id;
let form = CommunityModeratorForm {
community_id,
person_id,
};
CommunityModerator::join(pool, &form).await.unwrap();

// Make sure that they come back as a mod in the list
let comments = CommentQuery {
..Default::default()
}
dessalines marked this conversation as resolved.
Show resolved Hide resolved
.list(pool)
.await
.unwrap();

assert!(comments[1].creator_is_moderator);
dessalines marked this conversation as resolved.
Show resolved Hide resolved

cleanup(data, pool).await;
}

async fn cleanup(data: Data, pool: &mut DbPool<'_>) {
CommentLike::remove(
pool,
Expand Down Expand Up @@ -814,6 +859,7 @@ mod tests {
.unwrap();
CommentView {
creator_banned_from_community: false,
creator_is_moderator: false,
my_vote: None,
subscribed: SubscribedType::NotSubscribed,
saved: false,
Expand Down
43 changes: 41 additions & 2 deletions crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ fn queries<'a>() -> Queries<
.and(community_person_ban::person_id.eq(post_aggregates::creator_id)),
),
);
let creator_is_moderator = exists(
community_moderator::table.filter(
post_aggregates::community_id
.eq(community_moderator::community_id)
.and(community_moderator::person_id.eq(post_aggregates::creator_id)),
),
);

let is_saved = |person_id| {
exists(
Expand Down Expand Up @@ -226,6 +233,7 @@ fn queries<'a>() -> Queries<
person::all_columns,
community::all_columns,
is_creator_banned_from_community,
creator_is_moderator,
post_aggregates::all_columns,
subscribed_type_selection,
is_saved_selection,
Expand Down Expand Up @@ -705,7 +713,7 @@ mod tests {
newtypes::LanguageId,
source::{
actor_language::LocalUserLanguage,
community::{Community, CommunityInsertForm},
community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
community_block::{CommunityBlock, CommunityBlockForm},
instance::Instance,
instance_block::{InstanceBlock, InstanceBlockForm},
Expand All @@ -715,7 +723,7 @@ mod tests {
person_block::{PersonBlock, PersonBlockForm},
post::{Post, PostInsertForm, PostLike, PostLikeForm, PostUpdateForm},
},
traits::{Blockable, Crud, Likeable},
traits::{Blockable, Crud, Joinable, Likeable},
utils::{build_db_pool_for_tests, DbPool},
SortType,
SubscribedType,
Expand Down Expand Up @@ -1063,6 +1071,36 @@ mod tests {
cleanup(data, pool).await;
}

#[tokio::test]
#[serial]
async fn creator_is_moderator() {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;

// Make one of the inserted persons a moderator
let person_id = data.local_user_view.person.id;
let community_id = data.inserted_community.id;
let form = CommunityModeratorForm {
community_id,
person_id,
};
CommunityModerator::join(pool, &form).await.unwrap();

let post_listing = PostQuery {
sort: (Some(SortType::New)),
community_id: (Some(data.inserted_community.id)),
local_user: (Some(&data.local_user_view)),
..Default::default()
}
.list(pool)
.await
.unwrap();

assert!(post_listing[1].creator_is_moderator);
cleanup(data, pool).await;
}

#[tokio::test]
#[serial]
async fn post_listing_person_language() {
Expand Down Expand Up @@ -1396,6 +1434,7 @@ mod tests {
last_refreshed_at: inserted_person.last_refreshed_at,
},
creator_banned_from_community: false,
creator_is_moderator: false,
community: Community {
id: inserted_community.id,
name: inserted_community.name.clone(),
Expand Down
2 changes: 2 additions & 0 deletions crates/db_views/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct CommentView {
pub community: Community,
pub counts: CommentAggregates,
pub creator_banned_from_community: bool,
pub creator_is_moderator: bool,
pub subscribed: SubscribedType,
pub saved: bool,
pub creator_blocked: bool,
Expand Down Expand Up @@ -107,6 +108,7 @@ pub struct PostView {
pub creator: Person,
pub community: Community,
pub creator_banned_from_community: bool,
pub creator_is_moderator: bool,
pub counts: PostAggregates,
pub subscribed: SubscribedType,
pub saved: bool,
Expand Down