From 4015f92b52ad2c08263c2dc94cc71df3e7b57a70 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 16 Nov 2021 13:25:26 -0500 Subject: [PATCH] Cleaning up canMod --- .../components/comment/comment-node.tsx | 30 ++++------ src/shared/components/post/post-listing.tsx | 56 +++++-------------- src/shared/utils.ts | 7 ++- 3 files changed, 31 insertions(+), 62 deletions(-) diff --git a/src/shared/components/comment/comment-node.tsx b/src/shared/components/comment/comment-node.tsx index 4161f09b..578b0632 100644 --- a/src/shared/components/comment/comment-node.tsx +++ b/src/shared/components/comment/comment-node.tsx @@ -900,29 +900,19 @@ export class CommentNode extends Component { } 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 ); } diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index 6f932307..321e64e5 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -737,7 +737,7 @@ export class PostListing extends Component { /> )} - {this.canModOnSelf && ( + {this.canMod(true) && ( <> ))} - {this.canMod && ( + {this.canMod() && ( <> {!this.isMod && (!post_view.creator_banned_from_community ? ( @@ -1177,47 +1177,21 @@ export class PostListing extends Component { 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 ); } diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 12ee1469..1c16f0b5 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -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(