Cleaning up canMod

This commit is contained in:
Dessalines 2021-11-16 13:25:26 -05:00
parent 6d10274c8f
commit 4015f92b52
3 changed files with 31 additions and 62 deletions

View file

@ -900,29 +900,19 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
}
get canMod(): boolean {
if (this.props.admins && this.props.moderators) {
let adminsThenMods = this.props.admins
.map(a => a.person.id)
.concat(this.props.moderators.map(m => m.moderator.id));
return canMod(
UserService.Instance.myUserInfo,
adminsThenMods,
this.props.node.comment_view.creator.id
);
} else {
return false;
}
return canMod(
UserService.Instance.myUserInfo,
this.props.node.comment_view.creator.id,
this.props.admins,
this.props.moderators
);
}
get canAdmin(): boolean {
return (
this.props.admins &&
canMod(
UserService.Instance.myUserInfo,
this.props.admins.map(a => a.person.id),
this.props.node.comment_view.creator.id
)
return canMod(
UserService.Instance.myUserInfo,
this.props.node.comment_view.creator.id,
this.props.admins
);
}

View file

@ -737,7 +737,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
/>
</button>
)}
{this.canModOnSelf && (
{this.canMod(true) && (
<>
<button
class="btn btn-link btn-animate text-muted py-0"
@ -780,7 +780,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
</>
)}
{/* Mods can ban from community, and appoint as mods to community */}
{(this.canMod || this.canAdmin) &&
{(this.canMod() || this.canAdmin) &&
(!post_view.post.removed ? (
<button
class="btn btn-link btn-animate text-muted py-0"
@ -798,7 +798,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
{i18n.t("restore")}
</button>
))}
{this.canMod && (
{this.canMod() && (
<>
{!this.isMod &&
(!post_view.creator_banned_from_community ? (
@ -1177,47 +1177,21 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
return isAdmin(this.props.admins, this.props.post_view.creator.id);
}
get canMod(): boolean {
if (this.props.admins && this.props.moderators) {
let adminsThenMods = this.props.admins
.map(a => a.person.id)
.concat(this.props.moderators.map(m => m.moderator.id));
return canMod(
UserService.Instance.myUserInfo,
adminsThenMods,
this.props.post_view.creator.id
);
} else {
return false;
}
}
get canModOnSelf(): boolean {
if (this.props.admins && this.props.moderators) {
let adminsThenMods = this.props.admins
.map(a => a.person.id)
.concat(this.props.moderators.map(m => m.moderator.id));
return canMod(
UserService.Instance.myUserInfo,
adminsThenMods,
this.props.post_view.creator.id,
true
);
} else {
return false;
}
canMod(onSelf = false): boolean {
return canMod(
UserService.Instance.myUserInfo,
this.props.post_view.creator.id,
this.props.admins,
this.props.moderators,
onSelf
);
}
get canAdmin(): boolean {
return (
this.props.admins &&
canMod(
UserService.Instance.myUserInfo,
this.props.admins.map(a => a.person.id),
this.props.post_view.creator.id
)
return canMod(
UserService.Instance.myUserInfo,
this.props.post_view.creator.id,
this.props.admins
);
}

View file

@ -285,10 +285,15 @@ export function getUnixTime(text: string): number {
export function canMod(
myUserInfo: MyUserInfo,
modIds: number[],
creator_id: number,
admins: PersonViewSafe[] = [],
moderators: CommunityModeratorView[] = [],
onSelf = false
): boolean {
let modIds = admins
.map(a => a.person.id)
.concat(moderators.map(m => m.moderator.id));
// You can do moderator actions only on the mods added after you.
if (myUserInfo) {
let yourIndex = modIds.findIndex(