From 94ad4e9b99c15341ce8c6aa3fa1e30b846b893ce Mon Sep 17 00:00:00 2001 From: phroggster Date: Sat, 7 Sep 2024 16:40:18 -0500 Subject: [PATCH 1/2] fix: $hasRole and $hasRoles Examples - Role name comparisons are case-sensitive. - Moderator and VIP being the names sent out. - So reflect those accurately in the examples. - ... and clear up a few lint warnings (white space, ignored param) --- .../variables/builtin/user/roles/has-role.ts | 18 ++++++++++++++---- .../variables/builtin/user/roles/has-roles.ts | 14 +++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/backend/variables/builtin/user/roles/has-role.ts b/src/backend/variables/builtin/user/roles/has-role.ts index c5246f420..c99322bb7 100644 --- a/src/backend/variables/builtin/user/roles/has-role.ts +++ b/src/backend/variables/builtin/user/roles/has-role.ts @@ -17,12 +17,22 @@ const model : ReplaceVariable = { definition: { handle: "hasRole", usage: "hasRole[user, role]", - description: "Returns true if the user has the specified role. Only valid within $if", + description: "Returns true if the user has the specified role. Only valid within `$if`", + examples: [ + { + usage: "hasRole[user, Moderator]", + description: "Returns true if user is a mod" + }, + { + usage: "hasRole[user, VIP]", + description: "Returns true if user is a VIP" + } + ], triggers: triggers, categories: [VariableCategory.COMMON, VariableCategory.USER], possibleDataOutput: [OutputDataType.ALL] }, - evaluator: async (trigger, username: string, role: string) => { + evaluator: async (_trigger, username: string, role: string) => { if (username == null || username === "") { return false; } @@ -41,8 +51,8 @@ const model : ReplaceVariable = { } catch { // Silently fail } - + return false; } }; -export default model; \ No newline at end of file +export default model; diff --git a/src/backend/variables/builtin/user/roles/has-roles.ts b/src/backend/variables/builtin/user/roles/has-roles.ts index 0862a75cc..2ed23aecc 100644 --- a/src/backend/variables/builtin/user/roles/has-roles.ts +++ b/src/backend/variables/builtin/user/roles/has-roles.ts @@ -20,11 +20,11 @@ const model : ReplaceVariable = { description: "Returns true if the user has the specified roles. Only valid within $if", examples: [ { - usage: "hasRoles[$user, any, mod, vip]", + usage: "hasRoles[$user, any, Moderator, VIP]", description: "returns true if $user is a mod OR VIP" }, { - usage: "hasRoles[$user, all, mod, vip]", + usage: "hasRoles[$user, all, Moderator, VIP]", description: "Returns true if $user is a mod AND a VIP" } ], @@ -32,7 +32,7 @@ const model : ReplaceVariable = { categories: [VariableCategory.COMMON, VariableCategory.USER], possibleDataOutput: [OutputDataType.ALL] }, - evaluator: async (trigger, username: string, respective, ...roles) => { + evaluator: async (_trigger, username: string, respective, ...roles) => { if (username == null || username === "") { return false; } @@ -55,14 +55,14 @@ const model : ReplaceVariable = { if (user == null) { return false; } - + const userRoles = await roleHelpers.getAllRolesForViewer(user.id); - + // any if (respective === "any") { return userRoles.some(r => roles.includes(r.name)); } - + // all return roles.length === userRoles.filter(r => roles.includes(r.name)).length; } catch { @@ -73,4 +73,4 @@ const model : ReplaceVariable = { } }; -export default model; \ No newline at end of file +export default model; From d109d97ace91cd18f2361b90c3a54974b74b82a3 Mon Sep 17 00:00:00 2001 From: phroggster Date: Sat, 7 Sep 2024 17:01:52 -0500 Subject: [PATCH 2/2] Backtick the $if in the `$hasRoles` example - ... since I did it for `$hasRole` --- src/backend/variables/builtin/user/roles/has-roles.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/variables/builtin/user/roles/has-roles.ts b/src/backend/variables/builtin/user/roles/has-roles.ts index 2ed23aecc..6e002c01d 100644 --- a/src/backend/variables/builtin/user/roles/has-roles.ts +++ b/src/backend/variables/builtin/user/roles/has-roles.ts @@ -17,7 +17,7 @@ const model : ReplaceVariable = { definition: { handle: "hasRoles", usage: "hasRoles[user, any|all, role, role2, ...]", - description: "Returns true if the user has the specified roles. Only valid within $if", + description: "Returns true if the user has the specified roles. Only valid within `$if`", examples: [ { usage: "hasRoles[$user, any, Moderator, VIP]",