add API constraints

This commit is contained in:
Matthieu Jolimaitre 2024-02-02 12:57:42 +01:00
parent 431c184690
commit fe34c8a91c
5 changed files with 48 additions and 34 deletions

View file

@ -1,12 +1,14 @@
type SerializedRule = { group_id: string; target_role: TargetRole };
export type TargetRole = { guild_id: string; role_id: string };
type SerializedRule = { group_id: string; target_role: TargetRole };
export class RuleSet {
rules;
constructor() {
private rules;
public constructor() {
this.rules = new Map<string, TargetRole[]>();
}
static async from_file(path: string) {
public static async from_file(path: string) {
const result = new RuleSet();
const file_content = await Deno.readTextFile(path);
const parsed = JSON.parse(file_content) as SerializedRule[];
@ -14,6 +16,16 @@ export class RuleSet {
return result;
}
public roles_for_group(group_id: string) {
return this.rules.get(group_id) ?? [];
}
public size() {
let result = 0;
for (const roles of this.rules.values()) result += roles.length;
return result;
}
private append_rule(group_id: string, target_role: TargetRole) {
let roles = this.rules.get(group_id);
if (roles === undefined) {
@ -22,8 +34,4 @@ export class RuleSet {
}
roles.push(target_role);
}
roles_for_group(group_id: string) {
return this.rules.get(group_id) ?? [];
}
}