Conversation
13a42ab to
117dc2d
Compare
jaller94
left a comment
There was a problem hiding this comment.
Converting the regex strings at startup may prevent run time errors, which I think is worth the small effort.
| * Room ID or alias | ||
| * Room ID or alias. Optional. | ||
| * | ||
| * Either `room` or `roomRegex` must be provided. |
There was a problem hiding this comment.
Have you considered splitting this into three types?
type IConfigRoomRule = IConfigRoomRuleWithRoom | IConfigRoomRuleWithRoomRegex;
There would need to be one validation when loading the config, but after that it reduces the complexity of conditions.
| public getRoomRule(roomIdOrAlias?: string) { | ||
| const aliasRule = this.roomRules.find((r) => r.room === roomIdOrAlias); | ||
| if (aliasRule && aliasRule.action === "deny") { | ||
| public getRoomRule(roomIdOrAlias: string) { |
There was a problem hiding this comment.
This is a "first match wins" algorithm. I don't think this is expected by users when supporting regexps.
Without an annotation in the config sample (and the docs) I would expect exceptions like these to work:
/discord/ deny
"discord-jaller" allow
I think that would be .findLast() (<-- not a JS function). But we can also have the annotation saying that the first match wins.
| if (r.room === roomIdOrAlias) { | ||
| return true; | ||
| } | ||
| return (r.roomRegex && roomIdOrAlias.match(r.roomRegex)); |
There was a problem hiding this comment.
We have no interest in the return value. .test() may be faster and is more intentional.
| return (r.roomRegex && roomIdOrAlias.match(r.roomRegex)); | |
| return (r.roomRegex && r.roomRegex.test(new RegExp(roomIdOrAlias))); |
| * Either `room` or `roomRegex` must be provided. | ||
| */ | ||
| room: string; | ||
| roomRegex?: string; |
There was a problem hiding this comment.
I recommend converting the strings to RegExp here.
A bit for performance reasons, but mainly to catch config errors (alias invalid RegExp strings) at startup.
new RegExp(string)
Because there are rooms with 10s of thousands of users which are breaking things.