Skip to content

Commit

Permalink
fix: $hasRole and $hasRoles Usage Examples (#2790)
Browse files Browse the repository at this point in the history
* 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)

* Backtick the $if in the `$hasRoles` example

- ... since I did it for `$hasRole`

---------

Co-authored-by: Erik Bigler <[email protected]>
  • Loading branch information
phroggster and ebiggz authored Sep 13, 2024
1 parent 295d613 commit d4a276e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src/backend/variables/builtin/user/roles/has-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -41,8 +51,8 @@ const model : ReplaceVariable = {
} catch {
// Silently fail
}

return false;
}
};
export default model;
export default model;
16 changes: 8 additions & 8 deletions src/backend/variables/builtin/user/roles/has-roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ 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, 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"
}
],
triggers: triggers,
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;
}
Expand All @@ -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 {
Expand All @@ -73,4 +73,4 @@ const model : ReplaceVariable = {
}
};

export default model;
export default model;

0 comments on commit d4a276e

Please sign in to comment.