Skip to content

Commit

Permalink
Update host permissions (#468)
Browse files Browse the repository at this point in the history
* Update host permissions

* Add admin & mod roles to host

* Optimize checking user role/feature

* use for ... of
  • Loading branch information
Mitchdev authored May 20, 2024
1 parent d89fee8 commit 8edde40
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
17 changes: 15 additions & 2 deletions assets/chat/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { ChatPoll, parseQuestionAndTime } from './poll';
import { isMuteActive, MutedTimer } from './mutedtimer';
import EmoteService from './emotes';
import UserFeatures from './features';
import UserRoles from './roles';
import makeSafeForRegex, {
regexslashcmd,
regextime,
Expand Down Expand Up @@ -2235,7 +2236,13 @@ class Chat {
let url = parts[0];
const displayName = parts.slice(1).join(' ') || undefined;

if (!this.user.hasAnyFeatures(UserFeatures.ADMIN, UserFeatures.MODERATOR)) {
if (
!this.user.hasAnyRoles(
UserRoles.ADMIN,
UserRoles.MODERATOR,
UserRoles.HOST,
)
) {
MessageBuilder.error(errorstrings.get('nopermission')).into(this);
return;
}
Expand Down Expand Up @@ -2277,7 +2284,13 @@ class Chat {
}

cmdUNHOST() {
if (!this.user.hasAnyFeatures(UserFeatures.ADMIN, UserFeatures.MODERATOR)) {
if (
!this.user.hasAnyRoles(
UserRoles.ADMIN,
UserRoles.MODERATOR,
UserRoles.HOST,
)
) {
MessageBuilder.error(errorstrings.get('nopermission')).into(this);
return;
}
Expand Down
7 changes: 7 additions & 0 deletions assets/chat/js/roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
USER: 'USER',
SUBSCRIBER: 'SUBSCRIBER',
ADMIN: 'ADMIN',
MODERATOR: 'MODERATOR',
HOST: 'HOST',
};
36 changes: 26 additions & 10 deletions assets/chat/js/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import UserFeature from './features';
* @property {string} nick
* @property {string} createdDate
* @property {string[]} features
* @property {string[]} roles
*/

class ChatUser {
Expand Down Expand Up @@ -39,6 +40,12 @@ class ChatUser {
*/
features = [];

/**
* User's roles.
* @type {[]string}
*/
roles = [];

/**
* User's watching embed.
* @type {?Object}
Expand All @@ -58,24 +65,33 @@ class ChatUser {
this.username = this.displayName.toLowerCase();
this.createdDate = user.createdDate || '';
this.features = user.features || [];
this.roles = user.roles || [];
this.watching = user.watching || null;
}
}

hasAnyFeatures(...features) {
let exists = false;
features.forEach((f) => {
if (
this.features.indexOf(typeof f !== 'string' ? f.toString() : f) !== -1
)
exists = true;
});

return exists;
for (const feature of features) {
if (this.features.includes(feature)) return true;
}

return false;
}

hasAnyRoles(...roles) {
for (const role of roles) {
if (this.roles.includes(role)) return true;
}

return false;
}

hasFeature(feature) {
return this.hasAnyFeatures(feature);
return this.features.includes(feature);
}

hasRole(role) {
return this.roles.includes(role);
}

hasModPowers() {
Expand Down

0 comments on commit 8edde40

Please sign in to comment.