Skip to content

Commit b05e24e

Browse files
authored
Add seperate room for policy change notification protection. (#933)
This reverts commit 3d138d9 but we also modified it to make it stil work.
1 parent d77f494 commit b05e24e

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

src/commands/StatusCommand.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
MatrixRoomReference,
3030
StringRoomID,
3131
} from "@the-draupnir-project/matrix-basic-types";
32+
import { PolicyChangeNotification } from "../protections/PolicyChangeNotification";
3233
import { RoomTakedownProtection } from "../protections/RoomTakedown/RoomTakedownProtection";
3334
import { renderRoomPill } from "@the-draupnir-project/mps-interface-adaptor";
3435

@@ -97,13 +98,19 @@ DraupnirInterfaceAdaptor.describeRenderer(DraupnirStatusCommand, {
9798
});
9899

99100
type DraupnirNotificationRoomsInfo = {
101+
readonly policyChangeNotificationRoomID: StringRoomID | undefined;
100102
readonly roomDiscoveryNotificationRoomID: StringRoomID | undefined;
101103
};
102104

103105
function extractProtectionNotificationRooms(
104106
draupnir: Draupnir
105107
): DraupnirNotificationRoomsInfo {
106108
return {
109+
policyChangeNotificationRoomID: (
110+
draupnir.protectedRoomsSet.protections.findEnabledProtection(
111+
PolicyChangeNotification.name
112+
) as PolicyChangeNotification | undefined
113+
)?.notificationRoomID,
107114
roomDiscoveryNotificationRoomID: (
108115
draupnir.protectedRoomsSet.protections.findEnabledProtection(
109116
RoomTakedownProtection.name
@@ -151,7 +158,10 @@ export function renderPolicyList(list: WatchedPolicyRoom): DocumentNode {
151158
}
152159

153160
function renderNotificationRooms(info: StatusInfo): DocumentNode {
154-
if (info.roomDiscoveryNotificationRoomID === undefined) {
161+
if (
162+
info.roomDiscoveryNotificationRoomID === undefined &&
163+
info.policyChangeNotificationRoomID === undefined
164+
) {
155165
return <fragment></fragment>;
156166
}
157167
const renderNotificationRoom = (
@@ -172,6 +182,10 @@ function renderNotificationRooms(info: StatusInfo): DocumentNode {
172182
<b>Notification rooms:</b>
173183
<br />
174184
<ul>
185+
{renderNotificationRoom(
186+
"Policy change",
187+
info.policyChangeNotificationRoomID
188+
)}
175189
{renderNotificationRoom(
176190
"Room discovery",
177191
info.roomDiscoveryNotificationRoomID

src/protections/PolicyChangeNotification.tsx

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import {
1212
AbstractProtection,
1313
ActionResult,
14+
EDStatic,
1415
Logger,
1516
Ok,
1617
PolicyListRevision,
@@ -20,7 +21,7 @@ import {
2021
PolicyRuleMatchType,
2122
ProtectedRoomsSet,
2223
ProtectionDescription,
23-
UnknownConfig,
24+
StringRoomIDSchema,
2425
describeProtection,
2526
isError,
2627
} from "matrix-protection-suite";
@@ -40,15 +41,31 @@ import {
4041
DocumentNode,
4142
} from "@the-draupnir-project/interface-manager";
4243
import { renderRuleHashes, renderRuleClearText } from "../commands/Rules";
44+
import { NotificationRoomCreator } from "./NotificationRoom/NotificationRoom";
45+
import { Type } from "@sinclair/typebox";
46+
import { Result } from "@gnuxie/typescript-result";
4347

4448
const log = new Logger("PolicyChangeNotification");
4549

50+
const PolicyChangeNotificationSettings = Type.Object({
51+
notificationRoomID: Type.Optional(
52+
Type.Union([StringRoomIDSchema, Type.Undefined()], {
53+
default: undefined,
54+
description: "The room where notifications should be sent.",
55+
})
56+
),
57+
});
58+
59+
export type PolicyChangeNotificationSettings = EDStatic<
60+
typeof PolicyChangeNotificationSettings
61+
>;
62+
4663
export type PolicyChangeNotificationCapabilitites = Record<never, never>;
4764

4865
export type PolicyChangeNotificationProtectionDescription =
4966
ProtectionDescription<
5067
Draupnir,
51-
UnknownConfig,
68+
typeof PolicyChangeNotificationSettings,
5269
PolicyChangeNotificationCapabilitites
5370
>;
5471

@@ -62,7 +79,8 @@ export class PolicyChangeNotification
6279
description: PolicyChangeNotificationProtectionDescription,
6380
capabilities: PolicyChangeNotificationCapabilitites,
6481
protectedRoomsSet: ProtectedRoomsSet,
65-
private readonly draupnir: Draupnir
82+
private readonly draupnir: Draupnir,
83+
public readonly notificationRoomID: StringRoomID
6684
) {
6785
super(description, capabilities, protectedRoomsSet, {});
6886
}
@@ -92,7 +110,7 @@ export class PolicyChangeNotification
92110
}
93111
const sendResult = await sendMatrixEventsFromDeadDocument(
94112
this.draupnir.clientPlatform.toRoomMessageSender(),
95-
this.draupnir.managementRoomID,
113+
this.notificationRoomID,
96114
<root>{renderGroupedChanges(groupedChanges.ok)}</root>,
97115
{}
98116
);
@@ -159,24 +177,40 @@ function renderGroupedChanges(groupedChanges: GroupedChange[]): DocumentNode {
159177
return <fragment>{groupedChanges.map(renderListChanges)}</fragment>;
160178
}
161179

162-
describeProtection<PolicyChangeNotificationCapabilitites, Draupnir>({
180+
describeProtection<
181+
PolicyChangeNotificationCapabilitites,
182+
Draupnir,
183+
typeof PolicyChangeNotificationSettings
184+
>({
163185
name: PolicyChangeNotification.name,
164186
description: "Provides notification of policy changes from watched lists.",
165187
capabilityInterfaces: {},
166188
defaultCapabilities: {},
189+
configSchema: PolicyChangeNotificationSettings,
167190
async factory(
168191
description,
169192
protectedRoomsSet,
170193
draupnir,
171194
capabilities,
172-
_settings
195+
settings
173196
) {
197+
if (settings.notificationRoomID === undefined) {
198+
return (await NotificationRoomCreator.createNotificationRoomFromDraupnir(
199+
draupnir,
200+
description as unknown as ProtectionDescription,
201+
settings,
202+
"notificationRoomID",
203+
"Policy Change Notifications",
204+
log
205+
)) as Result<PolicyChangeNotification>;
206+
}
174207
return Ok(
175208
new PolicyChangeNotification(
176209
description,
177210
capabilities,
178211
protectedRoomsSet,
179-
draupnir
212+
draupnir,
213+
settings.notificationRoomID
180214
)
181215
);
182216
},

0 commit comments

Comments
 (0)