@@ -14,12 +14,13 @@ import {
1414 sendMatrixEventsFromDeadDocument ,
1515} from "./MPSMatrixInterfaceAdaptor" ;
1616import {
17+ DocumentNode ,
1718 MatrixInterfaceAdaptorCallbacks ,
1819 MatrixInterfaceCommandDispatcher ,
1920 TextPresentationRenderer ,
2021} from "@the-draupnir-project/interface-manager" ;
2122import { resultifyBotSDKRequestError } from "matrix-protection-suite-for-matrix-bot-sdk" ;
22- import { Logger , Task , Value } from "matrix-protection-suite" ;
23+ import { Logger , RoomEvent , Task , Value } from "matrix-protection-suite" ;
2324import {
2425 MatrixReactionHandler ,
2526 ReactionListener ,
@@ -69,30 +70,41 @@ export function makeConfirmationPromptListener(
6970 } ;
7071}
7172
72- export const matrixEventsFromConfirmationPrompt = async function (
73- { client, clientPlatform, reactionHandler } ,
74- { event } ,
75- command ,
76- document
77- ) {
73+ /**
74+ * This utility allows protections to send confirmation prompts that appear like confirmation prompts
75+ * for commands that have been sent without the `--no-confirm` option, but require confirmation.
76+ */
77+ export async function sendConfirmationPrompt (
78+ { client, clientPlatform, reactionHandler } : MatrixAdaptorContext ,
79+ {
80+ commandDesignator,
81+ readItems,
82+ } : { commandDesignator : string [ ] ; readItems : string [ ] } ,
83+ document : DocumentNode ,
84+ {
85+ roomID,
86+ event,
87+ } : { roomID ?: StringRoomID | undefined ; event ?: RoomEvent | undefined }
88+ ) : Promise < Result < void > > {
89+ const roomIDToUse = roomID ?? event ?. room_id ;
90+ if ( roomIDToUse === undefined ) {
91+ throw new TypeError ( `You must provide either a room ID or an event` ) ;
92+ }
7893 const reactionMap = new Map < string , string > (
7994 Object . entries ( { OK : "OK" , Cancel : "Cancel" } )
8095 ) ;
8196 const sendResult = await sendMatrixEventsFromDeadDocument (
8297 clientPlatform . toRoomMessageSender ( ) ,
83- event . room_id ,
98+ roomIDToUse ,
8499 document ,
85100 {
86101 replyToEvent : event ,
87102 additionalContent : reactionHandler . createAnnotation (
88103 COMMAND_CONFIRMATION_LISTENER ,
89104 reactionMap ,
90105 {
91- command_designator : command . designator ,
92- read_items : command
93- . toPartialCommand ( )
94- . stream . source . slice ( command . designator . length )
95- . map ( ( p ) => TextPresentationRenderer . render ( p ) ) ,
106+ command_designator : commandDesignator ,
107+ read_items : readItems ,
96108 }
97109 ) ,
98110 }
@@ -104,8 +116,28 @@ export const matrixEventsFromConfirmationPrompt = async function (
104116 throw new TypeError ( `We exepct to have sent at least one event` ) ;
105117 }
106118 return await reactionHandler
107- . addReactionsToEvent ( client , event . room_id , sendResult . ok [ 0 ] , reactionMap )
119+ . addReactionsToEvent ( client , roomIDToUse , sendResult . ok [ 0 ] , reactionMap )
108120 . then ( ( _ ) => Ok ( undefined ) , resultifyBotSDKRequestError ) ;
121+ }
122+
123+ export const matrixEventsFromConfirmationPrompt = async function (
124+ adaptorContext ,
125+ { event } ,
126+ command ,
127+ document
128+ ) {
129+ return await sendConfirmationPrompt (
130+ adaptorContext ,
131+ {
132+ commandDesignator : command . designator ,
133+ readItems : command
134+ . toPartialCommand ( )
135+ . stream . source . slice ( command . designator . length )
136+ . map ( ( p ) => TextPresentationRenderer . render ( p ) ) ,
137+ } ,
138+ document ,
139+ { event }
140+ ) ;
109141} satisfies MatrixInterfaceAdaptorCallbacks <
110142 MatrixAdaptorContext ,
111143 MatrixEventContext
0 commit comments