diff --git a/src/Mjolnir.ts b/src/Mjolnir.ts index ce6c6b0..7126479 100644 --- a/src/Mjolnir.ts +++ b/src/Mjolnir.ts @@ -27,7 +27,7 @@ import { TextualMessageEventContent } from "matrix-bot-sdk"; -import { ALL_RULE_TYPES as ALL_BAN_LIST_RULE_TYPES, RULE_ROOM, RULE_SERVER, RULE_USER } from "./models/PolicyRule"; +import { ALL_RULE_TYPES as ALL_BAN_LIST_RULE_TYPES, EntityType, RULE_ROOM, RULE_SERVER, RULE_USER } from "./models/PolicyRule"; import { applyServerAcls } from "./actions/ApplyAcl"; import { RoomUpdateError } from "./models/RoomUpdateError"; import { COMMAND_PREFIX, handleCommand } from "./commands/CommandHandler"; @@ -1194,4 +1194,25 @@ export class Mjolnir { await protection.handleReport(this, roomId, reporterId, event, reason); } } + + /** + * Return the composite opinion for an entity. + * + * In the current implementation, we return the first opinion found in the list + * of policies. Future versions will support additional mechanisms for composing + * opinions. + * + * @param entity + * @param type + * @returns The opinion or null if no list defines an opinion on this entity. + */ + public opinionForEntity(entity: string, type: EntityType): number | null { + for (let policyList of this.policyLists) { + let opinion = policyList.opinionForEntity(entity, type); + if (opinion !== null) { + return opinion; + } + } + return null; + } } diff --git a/src/models/PolicyList.ts b/src/models/PolicyList.ts index e32b835..833f668 100644 --- a/src/models/PolicyList.ts +++ b/src/models/PolicyList.ts @@ -193,9 +193,9 @@ class PolicyList extends EventEmitter { /** * Return the opinion for a given entity. - * @returns The opinion specified by this list or `0` if no opinion was defined. + * @returns The opinion specified by this list or `null` if no opinion was defined. */ - public opinionForEntity(entity: string, ruleKind?: EntityType): number { + public opinionForEntity(entity: string, ruleKind?: EntityType): number | null { ruleKind = ruleKind || extractEntityType(entity); // Find all rules for this entity. @@ -235,8 +235,8 @@ class PolicyList extends EventEmitter { } } - // By default, the opinion is 0. - return 0; + // By default, the opinion is null. + return null; } /**