mirror of
https://github.com/the-draupnir-project/Draupnir.git
synced 2026-05-25 00:35:17 +00:00
Post MPS Allow use of shortcodes to refer to policy rooms (#331)
* Update to MPS 0.15.0. This adds support for mjolnir shortcodes. * Add shortcode support back post MPS. Fixes https://github.com/the-draupnir-project/Draupnir/issues/316.
This commit is contained in:
+2
-2
@@ -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",
|
||||
|
||||
+12
-4
@@ -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<ActionResult<PolicyRoomEditor>> {
|
||||
@@ -49,12 +50,18 @@ async function ban(
|
||||
this: DraupnirContext,
|
||||
_keywords: ParsedKeywords,
|
||||
entity: UserID|MatrixRoomReference|string,
|
||||
policyRoomReference: MatrixRoomReference,
|
||||
policyRoomDesignator: MatrixRoomReference|string,
|
||||
...reasonParts: string[]
|
||||
): Promise<ActionResult<string>> {
|
||||
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<PromptOptions> {
|
||||
return {
|
||||
|
||||
@@ -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<ActionResult<MatrixRoomID>> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +100,8 @@ defineMatrixInterfaceAdaptor({
|
||||
const renderPolicyLists = (header: string, lists: ListInfo[]) => {
|
||||
const renderedLists = lists.map(list => {
|
||||
return <li>
|
||||
<a href={list.revision.room.toPermalink()}>{list.revision.room.toRoomIDOrAlias()}</a> propagation: {list.watchedListProfile.propagation}
|
||||
<a href={list.revision.room.toPermalink()}>{list.revision.room.toRoomIDOrAlias()}</a>  
|
||||
({list.revision.shortcode ?? '<no 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)
|
||||
</li>
|
||||
});
|
||||
|
||||
+10
-2
@@ -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<ActionResult<void>> {
|
||||
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 {
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user