diff --git a/package.json b/package.json index 9a3fffad..d3167842 100644 --- a/package.json +++ b/package.json @@ -63,8 +63,8 @@ "js-yaml": "^4.1.0", "jsdom": "^24.0.0", "matrix-appservice-bridge": "^9.0.1", - "matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@0.14.0", - "matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.14.0", + "matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@0.15.0", + "matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.15.0", "parse-duration": "^1.0.2", "pg": "^8.8.0", "shell-quote": "^1.7.3", diff --git a/src/commands/Ban.tsx b/src/commands/Ban.tsx index 673206b2..cc9280b1 100644 --- a/src/commands/Ban.tsx +++ b/src/commands/Ban.tsx @@ -33,8 +33,9 @@ import { defineMatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfac import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer"; import { PromptOptions } from "./interface-manager/PromptForAccept"; import { Draupnir } from "../Draupnir"; -import { ActionResult, MatrixRoomReference, PolicyRoomEditor, PolicyRuleType, isError, UserID } from "matrix-protection-suite"; +import { ActionResult, MatrixRoomReference, PolicyRoomEditor, PolicyRuleType, isError, UserID, Ok } from "matrix-protection-suite"; import { resolveRoomReferenceSafe } from "matrix-protection-suite-for-matrix-bot-sdk"; +import { findPolicyRoomIDFromShortcode } from "./CreateBanListCommand"; export async function findPolicyRoomEditorFromRoomReference(draupnir: Draupnir, policyRoomReference: MatrixRoomReference): Promise> { @@ -49,12 +50,18 @@ async function ban( this: DraupnirContext, _keywords: ParsedKeywords, entity: UserID|MatrixRoomReference|string, - policyRoomReference: MatrixRoomReference, + policyRoomDesignator: MatrixRoomReference|string, ...reasonParts: string[] ): Promise> { + const policyRoomReference = typeof policyRoomDesignator === 'string' + ? await findPolicyRoomIDFromShortcode(this.draupnir, policyRoomDesignator) + : Ok(policyRoomDesignator); + if (isError(policyRoomReference)) { + return policyRoomReference; + } const policyListEditorResult = await findPolicyRoomEditorFromRoomReference( this.draupnir, - policyRoomReference + policyRoomReference.ok ); if (isError(policyListEditorResult)) { return policyListEditorResult; @@ -92,7 +99,8 @@ defineInterfaceCommand({ { name: "list", acceptor: union( - findPresentationType("MatrixRoomReference") + findPresentationType("MatrixRoomReference"), + findPresentationType("string") ), prompt: async function (this: DraupnirContext, _parameter: ParameterDescription): Promise { return { diff --git a/src/commands/CreateBanListCommand.ts b/src/commands/CreateBanListCommand.ts index 48dd68f4..c414c30a 100644 --- a/src/commands/CreateBanListCommand.ts +++ b/src/commands/CreateBanListCommand.ts @@ -25,12 +25,14 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ -import { ActionResult, MatrixRoomID, PropagationType, isError } from "matrix-protection-suite"; +import { ActionError, ActionResult, MatrixRoomID, Ok, PolicyRuleType, PropagationType, isError } from "matrix-protection-suite"; import { DraupnirContext } from "./CommandHandler"; import { defineInterfaceCommand, findTableCommand } from "./interface-manager/InterfaceCommand"; import { ParsedKeywords, findPresentationType, parameters } from "./interface-manager/ParameterParsing"; import { defineMatrixInterfaceAdaptor } from "./interface-manager/MatrixInterfaceAdaptor"; import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer"; +import { listInfo } from "./StatusCommand"; +import { Draupnir } from "../Draupnir"; export async function createList( this: DraupnirContext, @@ -76,3 +78,20 @@ defineMatrixInterfaceAdaptor({ interfaceCommand: findTableCommand("mjolnir", "list", "create"), renderer: tickCrossRenderer }) + +export async function findPolicyRoomIDFromShortcode(draupnir: Draupnir, shortcode: string): Promise> { + const info = await listInfo(draupnir); + const matchingRevisions = info.filter(list => list.revision.shortcode === shortcode); + if (matchingRevisions.length === 0) { + return ActionError.Result(`Could not find a policy room from the shortcode: ${shortcode}`); + } else if (matchingRevisions.length === 1) { + return Ok(matchingRevisions[0].revision.room); + } else { + const remainingRevisions = matchingRevisions.filter(revision => revision.revision.isAbleToEdit(draupnir.clientUserID, PolicyRuleType.User)); + if (remainingRevisions.length !== 1) { + return ActionError.Result(`The shortcode ${shortcode} is ambiguous and is currently used by ${remainingRevisions.length} lists.`) + } else { + return Ok(remainingRevisions[0].revision.room) + } + } +} diff --git a/src/commands/StatusCommand.tsx b/src/commands/StatusCommand.tsx index e3d8d398..fd8910e5 100644 --- a/src/commands/StatusCommand.tsx +++ b/src/commands/StatusCommand.tsx @@ -100,7 +100,8 @@ defineMatrixInterfaceAdaptor({ const renderPolicyLists = (header: string, lists: ListInfo[]) => { const renderedLists = lists.map(list => { return
  • - {list.revision.room.toRoomIDOrAlias()} propagation: {list.watchedListProfile.propagation} + {list.revision.room.toRoomIDOrAlias()} + ({list.revision.shortcode ?? ''}) propagation: {list.watchedListProfile.propagation} (rules: {list.revision.allRulesOfType(PolicyRuleType.Server).length} servers, {list.revision.allRulesOfType(PolicyRuleType.User).length} users, {list.revision.allRulesOfType(PolicyRuleType.Room).length} rooms)
  • }); diff --git a/src/commands/Unban.ts b/src/commands/Unban.ts index 13f7b187..03c0d5d6 100644 --- a/src/commands/Unban.ts +++ b/src/commands/Unban.ts @@ -34,6 +34,7 @@ import { tickCrossRenderer } from "./interface-manager/MatrixHelpRenderer"; import { Draupnir } from "../Draupnir"; import { ActionResult, isError, isStringUserID, MatrixRoomReference, Ok, PolicyRuleType } from "matrix-protection-suite"; import { resolveRoomReferenceSafe } from "matrix-protection-suite-for-matrix-bot-sdk"; +import { findPolicyRoomIDFromShortcode } from "./CreateBanListCommand"; async function unbanUserFromRooms(draupnir: Draupnir, rule: MatrixGlob) { await draupnir.managementRoomOutput.logMessage(LogLevel.INFO, "Unban", "Unbanning users that match glob: " + rule.regex); @@ -58,9 +59,15 @@ async function unban( this: DraupnirContext, keywords: ParsedKeywords, entity: UserID|MatrixRoomReference|string, - policyRoomReference: MatrixRoomReference, + policyRoomDesignator: MatrixRoomReference|string, ): Promise> { - const policyRoom = await resolveRoomReferenceSafe(this.client, policyRoomReference); + const policyRoomReference = typeof policyRoomDesignator === 'string' + ? await findPolicyRoomIDFromShortcode(this.draupnir, policyRoomDesignator) + : Ok(policyRoomDesignator); + if (isError(policyRoomReference)) { + return policyRoomReference; + } + const policyRoom = await resolveRoomReferenceSafe(this.client, policyRoomReference.ok); if (isError(policyRoom)) { return policyRoom; } @@ -117,6 +124,7 @@ defineInterfaceCommand({ name: "list", acceptor: union( findPresentationType("MatrixRoomReference"), + findPresentationType("string") ), prompt: async function (this: DraupnirContext) { return { diff --git a/yarn.lock b/yarn.lock index 084dae7a..b7ab0b25 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2505,15 +2505,15 @@ matrix-appservice@^2.0.0: request-promise "^4.2.6" sanitize-html "^2.8.0" -"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.14.0": - version "0.14.0" - resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-0.14.0.tgz#291efc7e5ba86e92220ddb07e85829683de2a67c" - integrity sha512-LlG3OTgE6v7NcyavuMMvxqPOHQIGtnEy/IJipgkM8SLjo8aR9zpmAr95eh1PBylCYj4sRcptXdg/thhaQc5j2Q== +"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.15.0": + version "0.15.0" + resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-0.15.0.tgz#d59c51af79b3d198791a065fed16bd65b52dac33" + integrity sha512-U4CqeTBBwuI/kQFjT5muFwIoNkCopb5yG4TXvEEJqWy+Vt2NrAHV3UGwr5t/fcBYzzC+GLDYlRQkG5B9x0n3YA== -"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@0.14.0": - version "0.14.0" - resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-0.14.0.tgz#9fdbb63ecdbc16716b8bd5d2051e5069ddfda7c5" - integrity sha512-CjKIXhnV6yWGEBLdyVrYX3ArcJDTd5j6SS2dwobtoc0i95iu1y95LdhbMhoGtwcP4zLsw3ObM5jzhC+1KHTNOw== +"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@0.15.0": + version "0.15.0" + resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-0.15.0.tgz#7a876e4f670ac8d513e23eb4d4919d1f67f19d1c" + integrity sha512-2E75q9KEj1nz9V/5I7dOLRDTh1czDEftppbcr4DvPnJeXiJsLJ5eiSqd/hbr0AN0/PS+rICPgOqGOEbXXsk3uQ== dependencies: await-lock "^2.2.2" crypto-js "^4.1.1"