88// https://github.com/matrix-org/mjolnir
99// </text>
1010
11- import { Draupnir } from "../Draupnir" ;
1211import {
1312 ActionResult ,
1413 PolicyRoomEditor ,
1514 PolicyRuleType ,
1615 isError ,
1716 Ok ,
17+ PolicyRoomManager ,
18+ RoomResolver ,
19+ PolicyListConfig ,
1820} from "matrix-protection-suite" ;
19- import { resolveRoomReferenceSafe } from "matrix-protection-suite-for-matrix-bot-sdk" ;
2021import { findPolicyRoomIDFromShortcode } from "./CreateBanListCommand" ;
2122import {
2223 MatrixRoomReference ,
2324 MatrixUserID ,
25+ StringUserID ,
2426} from "@the-draupnir-project/matrix-basic-types" ;
2527import {
2628 BasicInvocationInformation ,
@@ -32,22 +34,31 @@ import {
3234 tuple ,
3335 union ,
3436} from "@the-draupnir-project/interface-manager" ;
35- import { DraupnirInterfaceAdaptor } from "./DraupnirCommandPrerequisites" ;
37+ import {
38+ DraupnirContextToCommandContextTranslator ,
39+ DraupnirInterfaceAdaptor ,
40+ } from "./DraupnirCommandPrerequisites" ;
3641
3742export async function findPolicyRoomEditorFromRoomReference (
38- draupnir : Draupnir ,
43+ roomResolver : RoomResolver ,
44+ policyRoomManager : PolicyRoomManager ,
3945 policyRoomReference : MatrixRoomReference
4046) : Promise < ActionResult < PolicyRoomEditor > > {
41- const policyRoomID = await resolveRoomReferenceSafe (
42- draupnir . client ,
43- policyRoomReference
44- ) ;
47+ const policyRoomID = await roomResolver . resolveRoom ( policyRoomReference ) ;
4548 if ( isError ( policyRoomID ) ) {
4649 return policyRoomID ;
4750 }
48- return await draupnir . policyRoomManager . getPolicyRoomEditor ( policyRoomID . ok ) ;
51+ return await policyRoomManager . getPolicyRoomEditor ( policyRoomID . ok ) ;
4952}
5053
54+ export type DraupnirBanCommandContext = {
55+ policyRoomManager : PolicyRoomManager ;
56+ issuerManager : PolicyListConfig ;
57+ defaultReasons : string [ ] ;
58+ roomResolver : RoomResolver ;
59+ clientUserID : StringUserID ;
60+ } ;
61+
5162export const DraupnirBanCommand = describeCommand ( {
5263 summary : "Bans an entity from the policy list." ,
5364 parameters : tuple (
@@ -67,13 +78,13 @@ export const DraupnirBanCommand = describeCommand({
6778 MatrixRoomReferencePresentationSchema ,
6879 StringPresentationType
6980 ) ,
70- prompt : async function ( draupnir : Draupnir ) {
81+ prompt : async function ( {
82+ policyRoomManager,
83+ clientUserID,
84+ } : DraupnirBanCommandContext ) {
7185 return Ok ( {
72- suggestions : draupnir . policyRoomManager
73- . getEditablePolicyRoomIDs (
74- draupnir . clientUserID ,
75- PolicyRuleType . User
76- )
86+ suggestions : policyRoomManager
87+ . getEditablePolicyRoomIDs ( clientUserID , PolicyRuleType . User )
7788 . map ( ( room ) => MatrixRoomIDPresentationType . wrap ( room ) ) ,
7889 } ) ;
7990 } ,
@@ -83,16 +94,21 @@ export const DraupnirBanCommand = describeCommand({
8394 name : "reason" ,
8495 description : "The reason for the ban." ,
8596 acceptor : StringPresentationType ,
86- prompt : async function ( draupnir : Draupnir ) {
97+ prompt : async function ( { defaultReasons } : DraupnirBanCommandContext ) {
8798 return Ok ( {
88- suggestions : draupnir . config . commands . ban . defaultReasons . map (
89- ( reason ) => [ StringPresentationType . wrap ( reason ) ]
99+ suggestions : defaultReasons . map ( ( reason ) =>
100+ StringPresentationType . wrap ( reason )
90101 ) ,
91102 } ) ;
92103 } ,
93104 } ,
94105 async executor (
95- draupnir : Draupnir ,
106+ {
107+ issuerManager,
108+ policyRoomManager,
109+ roomResolver,
110+ clientUserID,
111+ } : DraupnirBanCommandContext ,
96112 _info : BasicInvocationInformation ,
97113 _keywords ,
98114 reasonParts ,
@@ -101,13 +117,19 @@ export const DraupnirBanCommand = describeCommand({
101117 ) : Promise < ActionResult < string > > {
102118 const policyRoomReference =
103119 typeof policyRoomDesignator === "string"
104- ? await findPolicyRoomIDFromShortcode ( draupnir , policyRoomDesignator )
120+ ? await findPolicyRoomIDFromShortcode (
121+ issuerManager ,
122+ policyRoomManager ,
123+ clientUserID ,
124+ policyRoomDesignator
125+ )
105126 : Ok ( policyRoomDesignator ) ;
106127 if ( isError ( policyRoomReference ) ) {
107128 return policyRoomReference ;
108129 }
109130 const policyListEditorResult = await findPolicyRoomEditorFromRoomReference (
110- draupnir ,
131+ roomResolver ,
132+ policyRoomManager ,
111133 policyRoomReference . ok
112134 ) ;
113135 if ( isError ( policyListEditorResult ) ) {
@@ -128,10 +150,7 @@ export const DraupnirBanCommand = describeCommand({
128150 reason
129151 ) ;
130152 } else {
131- const resolvedRoomReference = await resolveRoomReferenceSafe (
132- draupnir . client ,
133- entity
134- ) ;
153+ const resolvedRoomReference = await roomResolver . resolveRoom ( entity ) ;
135154 if ( isError ( resolvedRoomReference ) ) {
136155 return resolvedRoomReference ;
137156 }
@@ -144,6 +163,19 @@ export const DraupnirBanCommand = describeCommand({
144163 } ,
145164} ) ;
146165
166+ DraupnirContextToCommandContextTranslator . registerTranslation (
167+ DraupnirBanCommand ,
168+ function ( draupnir ) {
169+ return {
170+ policyRoomManager : draupnir . policyRoomManager ,
171+ issuerManager : draupnir . protectedRoomsSet . issuerManager ,
172+ defaultReasons : draupnir . config . commands . ban . defaultReasons ,
173+ roomResolver : draupnir . clientPlatform . toRoomResolver ( ) ,
174+ clientUserID : draupnir . clientUserID ,
175+ } ;
176+ }
177+ ) ;
178+
147179DraupnirInterfaceAdaptor . describeRenderer ( DraupnirBanCommand , {
148180 isAlwaysSupposedToUseDefaultRenderer : true ,
149181} ) ;
0 commit comments