From d660689dc286ceb8e762e332c681c12eefa6418f Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Sat, 18 May 2024 11:41:00 +1200 Subject: [PATCH 1/4] Update host permissions --- assets/chat/js/chat.js | 5 +++-- assets/chat/js/roles.js | 7 +++++++ assets/chat/js/user.js | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 assets/chat/js/roles.js diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index b2c51b37..119fe8b0 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -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, @@ -2235,7 +2236,7 @@ class Chat { const displayName = parts[1]; let url = parts[0]; - if (!this.user.hasAnyFeatures(UserFeatures.ADMIN, UserFeatures.MODERATOR)) { + if (!this.user.hasRole(UserRoles.HOST)) { MessageBuilder.error(errorstrings.get('nopermission')).into(this); return; } @@ -2277,7 +2278,7 @@ class Chat { } cmdUNHOST() { - if (!this.user.hasAnyFeatures(UserFeatures.ADMIN, UserFeatures.MODERATOR)) { + if (!this.user.hasRole(UserRoles.HOST)) { MessageBuilder.error(errorstrings.get('nopermission')).into(this); return; } diff --git a/assets/chat/js/roles.js b/assets/chat/js/roles.js new file mode 100644 index 00000000..d3b1442d --- /dev/null +++ b/assets/chat/js/roles.js @@ -0,0 +1,7 @@ +export default { + USER: 'USER', + SUBSCRIBER: 'SUBSCRIBER', + ADMIN: 'ADMIN', + MODERATOR: 'MODERATOR', + HOST: 'HOST', +}; diff --git a/assets/chat/js/user.js b/assets/chat/js/user.js index 35039836..e590f067 100644 --- a/assets/chat/js/user.js +++ b/assets/chat/js/user.js @@ -6,6 +6,7 @@ import UserFeature from './features'; * @property {string} nick * @property {string} createdDate * @property {string[]} features + * @property {string[]} roles */ class ChatUser { @@ -39,6 +40,12 @@ class ChatUser { */ features = []; + /** + * User's roles. + * @type {[]string} + */ + roles = []; + /** * User's watching embed. * @type {?Object} @@ -58,6 +65,7 @@ class ChatUser { this.username = this.displayName.toLowerCase(); this.createdDate = user.createdDate || ''; this.features = user.features || []; + this.roles = user.roles || []; this.watching = user.watching || null; } } @@ -74,10 +82,24 @@ class ChatUser { return exists; } + hasAnyRoles(...roles) { + let exists = false; + roles.forEach((f) => { + if (this.roles.indexOf(typeof f !== 'string' ? f.toString() : f) !== -1) + exists = true; + }); + + return exists; + } + hasFeature(feature) { return this.hasAnyFeatures(feature); } + hasRole(role) { + return this.hasAnyRoles(role); + } + hasModPowers() { return this.hasAnyFeatures(UserFeature.ADMIN, UserFeature.MODERATOR); } From f9a53261bbff51548c2dd3723857e085c9e81322 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Mon, 20 May 2024 10:30:40 +1200 Subject: [PATCH 2/4] Add admin & mod roles to host --- assets/chat/js/chat.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index 119fe8b0..c6a31403 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -2236,7 +2236,13 @@ class Chat { const displayName = parts[1]; let url = parts[0]; - if (!this.user.hasRole(UserRoles.HOST)) { + if ( + !this.user.hasAnyRoles( + UserRoles.ADMIN, + UserRoles.MODERATOR, + UserRoles.HOST, + ) + ) { MessageBuilder.error(errorstrings.get('nopermission')).into(this); return; } @@ -2278,7 +2284,13 @@ class Chat { } cmdUNHOST() { - if (!this.user.hasRole(UserRoles.HOST)) { + if ( + !this.user.hasAnyRoles( + UserRoles.ADMIN, + UserRoles.MODERATOR, + UserRoles.HOST, + ) + ) { MessageBuilder.error(errorstrings.get('nopermission')).into(this); return; } From 937bb1bfd8accda8d765ed3a0049716fcc3a613a Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Tue, 21 May 2024 08:58:37 +1200 Subject: [PATCH 3/4] Optimize checking user role/feature --- assets/chat/js/user.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/assets/chat/js/user.js b/assets/chat/js/user.js index e590f067..91c4a133 100644 --- a/assets/chat/js/user.js +++ b/assets/chat/js/user.js @@ -71,33 +71,27 @@ class ChatUser { } hasAnyFeatures(...features) { - let exists = false; - features.forEach((f) => { - if ( - this.features.indexOf(typeof f !== 'string' ? f.toString() : f) !== -1 - ) - exists = true; - }); - - return exists; + for (let i = 0; i < features.length; i++) { + if (this.features.includes(features[i])) return true; + } + + return false; } hasAnyRoles(...roles) { - let exists = false; - roles.forEach((f) => { - if (this.roles.indexOf(typeof f !== 'string' ? f.toString() : f) !== -1) - exists = true; - }); + for (let i = 0; i < roles.length; i++) { + if (this.roles.includes(roles[i])) return true; + } - return exists; + return false; } hasFeature(feature) { - return this.hasAnyFeatures(feature); + return this.features.includes(feature); } hasRole(role) { - return this.hasAnyRoles(role); + return this.roles.includes(role); } hasModPowers() { From f709e19ab7ac1af3585093ed7a82775c2d56c2c1 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Tue, 21 May 2024 10:18:30 +1200 Subject: [PATCH 4/4] use for ... of --- assets/chat/js/user.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/assets/chat/js/user.js b/assets/chat/js/user.js index 91c4a133..3f747f6d 100644 --- a/assets/chat/js/user.js +++ b/assets/chat/js/user.js @@ -71,16 +71,16 @@ class ChatUser { } hasAnyFeatures(...features) { - for (let i = 0; i < features.length; i++) { - if (this.features.includes(features[i])) return true; + for (const feature of features) { + if (this.features.includes(feature)) return true; } return false; } hasAnyRoles(...roles) { - for (let i = 0; i < roles.length; i++) { - if (this.roles.includes(roles[i])) return true; + for (const role of roles) { + if (this.roles.includes(role)) return true; } return false;