1- // Copyright 2022 Gnuxie <[email protected] > 1+ // Copyright 2022, 2025 Gnuxie <[email protected] > 22// Copyright 2019 The Matrix.org Foundation C.I.C.
33//
44// SPDX-License-Identifier: AFL-3.0 AND Apache-2.0
99// </text>
1010
1111import {
12- RoomResolver ,
12+ PolicyRoomManager ,
13+ ProtectedRoomsSet ,
14+ RoomJoiner ,
1315 WatchedPolicyRooms ,
1416 isError ,
1517} from "matrix-protection-suite" ;
@@ -18,16 +20,23 @@ import {
1820 describeCommand ,
1921 tuple ,
2022} from "@the-draupnir-project/interface-manager" ;
21- import { Result , ResultError } from "@gnuxie/typescript-result" ;
23+ import { Ok , Result , ResultError } from "@gnuxie/typescript-result" ;
2224import { Draupnir } from "../Draupnir" ;
2325import {
2426 DraupnirContextToCommandContextTranslator ,
2527 DraupnirInterfaceAdaptor ,
2628} from "./DraupnirCommandPrerequisites" ;
29+ import {
30+ generateWatchPreview ,
31+ renderWatchCommandPreview ,
32+ WatchPolicyRoomPreview ,
33+ } from "./WatchPreview" ;
2734
2835export type DraupnirWatchUnwatchCommandContext = {
2936 watchedPolicyRooms : WatchedPolicyRooms ;
30- roomResolver : RoomResolver ;
37+ roomJoiner : RoomJoiner ;
38+ policyRoomManager : PolicyRoomManager ;
39+ protectedRoomsSet : ProtectedRoomsSet ;
3140} ;
3241
3342export const DraupnirWatchPolicyRoomCommand = describeCommand ( {
@@ -37,16 +46,29 @@ export const DraupnirWatchPolicyRoomCommand = describeCommand({
3746 name : "policy room" ,
3847 acceptor : MatrixRoomReferencePresentationSchema ,
3948 } ) ,
49+ keywords : {
50+ keywordDescriptions : {
51+ "no-confirm" : {
52+ isFlag : true ,
53+ description : "Runs the command without the preview." ,
54+ } ,
55+ } ,
56+ } ,
4057 async executor (
41- { watchedPolicyRooms, roomResolver } : DraupnirWatchUnwatchCommandContext ,
58+ {
59+ watchedPolicyRooms,
60+ roomJoiner,
61+ policyRoomManager,
62+ protectedRoomsSet,
63+ } : DraupnirWatchUnwatchCommandContext ,
4264 _info ,
43- _keywords ,
65+ keywords ,
4466 _rest ,
4567 policyRoomReference
46- ) : Promise < Result < void > > {
47- const policyRoom = await roomResolver . resolveRoom ( policyRoomReference ) ;
68+ ) : Promise < Result < undefined | WatchPolicyRoomPreview > > {
69+ const policyRoom = await roomJoiner . joinRoom ( policyRoomReference ) ;
4870 if ( isError ( policyRoom ) ) {
49- return policyRoom ;
71+ return policyRoom . elaborate ( "Failed to resolve or join the room" ) ;
5072 }
5173 if (
5274 watchedPolicyRooms . allRooms . some (
@@ -56,10 +78,46 @@ export const DraupnirWatchPolicyRoomCommand = describeCommand({
5678 ) {
5779 return ResultError . Result ( "We are already watching this list." ) ;
5880 }
59- return await watchedPolicyRooms . watchPolicyRoomDirectly ( policyRoom . ok ) ;
81+ if ( keywords . getKeywordValue < boolean > ( "no-confirm" , false ) ) {
82+ const watchResult = await watchedPolicyRooms . watchPolicyRoomDirectly (
83+ policyRoom . ok
84+ ) ;
85+ if ( isError ( watchResult ) ) {
86+ return watchResult ;
87+ }
88+ return Ok ( undefined ) ;
89+ }
90+ const revisionIssuer = await policyRoomManager . getPolicyRoomRevisionIssuer (
91+ policyRoom . ok
92+ ) ;
93+ if ( isError ( revisionIssuer ) ) {
94+ return revisionIssuer . elaborate (
95+ "Failed to fetch policy room revision issuer"
96+ ) ;
97+ }
98+ return Ok (
99+ generateWatchPreview ( protectedRoomsSet , revisionIssuer . ok . currentRevision )
100+ ) ;
60101 } ,
61102} ) ;
62103
104+ DraupnirInterfaceAdaptor . describeRenderer ( DraupnirWatchPolicyRoomCommand , {
105+ isAlwaysSupposedToUseDefaultRenderer : true ,
106+ confirmationPromptJSXRenderer ( commandResult ) {
107+ if ( isError ( commandResult ) ) {
108+ return Ok ( undefined ) ;
109+ } else if ( commandResult . ok === undefined ) {
110+ return Ok ( undefined ) ;
111+ } else {
112+ return Ok ( renderWatchCommandPreview ( commandResult . ok ) ) ;
113+ }
114+ } ,
115+ } ) ;
116+ DraupnirContextToCommandContextTranslator . registerTranslation (
117+ DraupnirWatchPolicyRoomCommand ,
118+ buildWatchContext
119+ ) ;
120+
63121export const DraupnirUnwatchPolicyRoomCommand = describeCommand ( {
64122 summary :
65123 "Unwatches a list and stops applying the list's assocated policies to draupnir's protected rooms." ,
@@ -68,32 +126,35 @@ export const DraupnirUnwatchPolicyRoomCommand = describeCommand({
68126 acceptor : MatrixRoomReferencePresentationSchema ,
69127 } ) ,
70128 async executor (
71- { watchedPolicyRooms, roomResolver } : DraupnirWatchUnwatchCommandContext ,
129+ { watchedPolicyRooms, roomJoiner } : DraupnirWatchUnwatchCommandContext ,
72130 _info ,
73131 _keywords ,
74132 _rest ,
75133 policyRoomReference
76134 ) : Promise < Result < void > > {
77- const policyRoom = await roomResolver . resolveRoom ( policyRoomReference ) ;
135+ const policyRoom = await roomJoiner . resolveRoom ( policyRoomReference ) ;
78136 if ( isError ( policyRoom ) ) {
79137 return policyRoom ;
80138 }
81139 return await watchedPolicyRooms . unwatchPolicyRoom ( policyRoom . ok ) ;
82140 } ,
83141} ) ;
84142
85- for ( const command of [
86- DraupnirWatchPolicyRoomCommand ,
87- DraupnirUnwatchPolicyRoomCommand ,
88- ] ) {
89- DraupnirInterfaceAdaptor . describeRenderer ( command , {
90- isAlwaysSupposedToUseDefaultRenderer : true ,
91- } ) ;
92- DraupnirContextToCommandContextTranslator . registerTranslation (
93- command ,
94- ( draupnir : Draupnir ) => ( {
95- watchedPolicyRooms : draupnir . protectedRoomsSet . watchedPolicyRooms ,
96- roomResolver : draupnir . clientPlatform . toRoomResolver ( ) ,
97- } )
98- ) ;
143+ function buildWatchContext (
144+ draupnir : Draupnir
145+ ) : DraupnirWatchUnwatchCommandContext {
146+ return {
147+ watchedPolicyRooms : draupnir . protectedRoomsSet . watchedPolicyRooms ,
148+ roomJoiner : draupnir . clientPlatform . toRoomJoiner ( ) ,
149+ policyRoomManager : draupnir . policyRoomManager ,
150+ protectedRoomsSet : draupnir . protectedRoomsSet ,
151+ } ;
99152}
153+
154+ DraupnirInterfaceAdaptor . describeRenderer ( DraupnirUnwatchPolicyRoomCommand , {
155+ isAlwaysSupposedToUseDefaultRenderer : true ,
156+ } ) ;
157+ DraupnirContextToCommandContextTranslator . registerTranslation (
158+ DraupnirUnwatchPolicyRoomCommand ,
159+ buildWatchContext
160+ ) ;
0 commit comments