From 072ee49e31f2f1191f1fe7aa98681cdaf2ec3ea7 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Sat, 9 Dec 2023 17:52:54 +0000 Subject: [PATCH] Update Rules command for MPS. --- src/commands/Rules.tsx | 66 +++++++++++++++++----------------- src/commands/StatusCommand.tsx | 2 +- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/commands/Rules.tsx b/src/commands/Rules.tsx index 8f2916b1..64822a8d 100644 --- a/src/commands/Rules.tsx +++ b/src/commands/Rules.tsx @@ -25,31 +25,29 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ +import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk"; import { DraupnirContext } from "./CommandHandler"; import { renderMatrixAndSend } from "./interface-manager/DeadDocumentMatrix"; import { defineInterfaceCommand, findTableCommand } from "./interface-manager/InterfaceCommand"; import { JSXFactory } from "./interface-manager/JSXFactory"; import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer"; import { defineMatrixInterfaceAdaptor, MatrixContext, MatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfaceAdaptor"; -import { MatrixRoomReference } from "./interface-manager/MatrixRoomReference"; import { findPresentationType, parameters, union } from "./interface-manager/ParameterParsing"; -import { CommandResult } from "./interface-manager/Validation"; import { UserID } from "matrix-bot-sdk"; -import { MatrixSendClient } from "../MatrixEmitter"; -import { ListRule } from "../models/ListRule"; -import { PolicyRule } from "matrix-protection-suite"; +import { ActionResult, MatrixRoomID, MatrixRoomReference, Ok, PolicyRoomWatchProfile, PolicyRule, StringRoomID, isError } from "matrix-protection-suite"; +import { listInfo } from "./StatusCommand"; async function renderListMatches( - this: MatrixInterfaceAdaptor, client: MatrixSendClient, commandRoomId: string, event: any, result: CommandResult + this: MatrixInterfaceAdaptor, client: MatrixSendClient, commandRoomID: StringRoomID, event: any, result: ActionResult ) { - if (result.isErr()) { + if (isError(result)) { return await tickCrossRenderer.call(this, ...arguments); } const lists = result.ok; if (lists.length === 0) { return await renderMatrixAndSend( No policy lists configured, - commandRoomId, event, client + commandRoomID, event, client ) } return await renderMatrixAndSend( @@ -57,29 +55,30 @@ async function renderListMatches( Rules currently in use:
{lists.map(list => renderListRules(list))} , - commandRoomId, event, client + commandRoomID, event, client ) } export function renderListRules(list: ListMatches) { - const renderRuleSummary = (rule: ListRule, entityDescription: string) => { + const renderRuleSummary = (rule: PolicyRule) => { return
  • - {entityDescription} ({rule.recommendation}): {rule.entity} ({rule.reason}) + {rule.kind} ({rule.recommendation}): {rule.entity} ({rule.reason})
  • }; return - {list.roomId}
    + {list.roomID} propagation: {list.profile.propagation}
      {list.matches.length === 0 ?
    • No rules
    • - : list.matches.map(rule => renderRuleSummary(rule, rule.kind))} + : list.matches.map(rule => renderRuleSummary(rule))}
    } interface ListMatches { - roomRef: string, - roomId: string, + room: MatrixRoomID, + roomID: StringRoomID, + profile: PolicyRoomWatchProfile, matches: PolicyRule[] } @@ -87,17 +86,17 @@ defineInterfaceCommand({ designator: ["rules"], table: "mjolnir", parameters: parameters([]), - command: async function (this: DraupnirContext) { - return CommandResult.Ok( - this.mjolnir.policyListManager.lists - .map(list => { - return { - shortcode: list.listShortcode, - roomRef: list.roomRef, - roomId: list.roomId, - matches: list.allRules - } + command: async function (this: DraupnirContext): Promise> { + const infoResult = await listInfo(this.draupnir); + return Ok( + infoResult.map( + policyRoom => ({ + room: policyRoom.revision.room, + roomID: policyRoom.revision.room.toRoomIDOrAlias(), + profile: policyRoom.watchedListProfile, + matches: policyRoom.revision.allRules() }) + ) ); }, summary: "Lists the rules currently in use by Mjolnir." @@ -123,15 +122,16 @@ defineInterfaceCommand({ ]), command: async function ( this: DraupnirContext, _keywords, entity: string|UserID|MatrixRoomReference - ): Promise> { - return CommandResult.Ok( - this.mjolnir.policyListManager.lists - .map(list => { + ): Promise> { + const policyRooms = await listInfo(this.draupnir); + return Ok( + policyRooms + .map(policyRoom => { return { - shortcode: list.listShortcode, - roomRef: list.roomRef, - roomId: list.roomId, - matches: list.rulesMatchingEntity(entity.toString()) + room: policyRoom.revision.room, + roomID: policyRoom.revision.room.toRoomIDOrAlias(), + matches: policyRoom.revision.allRulesMatchingEntity(entity.toString()), + profile: policyRoom.watchedListProfile } }) ); diff --git a/src/commands/StatusCommand.tsx b/src/commands/StatusCommand.tsx index 74fc9eb7..fcb7002e 100644 --- a/src/commands/StatusCommand.tsx +++ b/src/commands/StatusCommand.tsx @@ -60,7 +60,7 @@ export interface StatusInfo { repository: string } -async function listInfo(draupnir: Draupnir): Promise { +export async function listInfo(draupnir: Draupnir): Promise { const watchedListProfiles = draupnir.protectedRoomsSet.issuerManager.allWatchedLists; const issuerResults = await Promise.all(watchedListProfiles.map((profile) => draupnir.managerManager.policyRoomManager.getPolicyRoomRevisionIssuer(profile.room)