Skip to content

Commit

Permalink
Update for simulated capabilities from MPS v2.10.0 and add their rend…
Browse files Browse the repository at this point in the history
…erers, commands. (#727)

* Set renderers and glue for new simulated capabilities.

the-draupnir-project/planning#2.

* Distinguish simulated capability messages when rendering.

* Update for MPS 2.10.0

* Add `!protections capability reset <protection name>`  command.

This will allow you to use the default capabilities for a protection.

* Update CHANGELOG for simulated capabilities.
  • Loading branch information
Gnuxie authored Feb 12, 2025
1 parent d3b55a8 commit 9587d6f
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 26 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ and this project adheres to

## [Unreleased] - None

### Added

- Simulated capabilities for all available protection capabilities. These allow
protections to run without effects.

- A command `!draupnir protections capability reset <protection name>` to
restore the default capability set.

## [v2.1.0] - 2025-02-02

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"jsdom": "^24.0.0",
"matrix-appservice-bridge": "^10.3.1",
"matrix-bot-sdk": "npm:@vector-im/matrix-bot-sdk@^0.7.1-element.6",
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@2.9.0",
"matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@2.9.0",
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@2.10.0",
"matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@2.10.0",
"pg": "^8.8.0",
"yaml": "^2.3.2"
},
Expand Down
31 changes: 23 additions & 8 deletions src/capabilities/DraupnirRendererMessageCollector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
RendererMessageCollector,
} from "./RendererMessageCollector";
import {
Capability,
DescriptionMeta,
RoomMessageSender,
Task,
Expand All @@ -17,8 +18,6 @@ import {
DocumentNode,
} from "@the-draupnir-project/interface-manager";
import { sendMatrixEventsFromDeadDocument } from "../commands/interface-manager/MPSMatrixInterfaceAdaptor";
import { Result } from "@gnuxie/typescript-result";

export class DraupnirRendererMessageCollector
implements RendererMessageCollector
{
Expand All @@ -28,21 +27,37 @@ export class DraupnirRendererMessageCollector
) {
// nothing to do.
}
private sendMessage(document: DocumentNode): void {
private sendMessage(capability: Capability, document: DocumentNode): void {
void Task(
sendMatrixEventsFromDeadDocument(
this.roomMessageSender,
this.managementRoomID,
<root>{document}</root>,
<root>
{capability.isSimulated ? (
<fragment>⚠️ (preview) </fragment>
) : (
<fragment></fragment>
)}
{document}
</root>,
{}
) as Promise<Result<void>>
)
);
}
addMessage(protection: DescriptionMeta, message: DocumentNode): void {
this.sendMessage(message);
addMessage(
protection: DescriptionMeta,
capability: Capability,
message: DocumentNode
): void {
this.sendMessage(capability, message);
}
addOneliner(protection: DescriptionMeta, message: DocumentNode): void {
addOneliner(
protection: DescriptionMeta,
capability: Capability,
message: DocumentNode
): void {
this.sendMessage(
capability,
<fragment>
<code>{protection.name}</code>: {message}
</fragment>
Expand Down
29 changes: 24 additions & 5 deletions src/capabilities/RendererMessageCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: AFL-3.0

import { DocumentNode } from "@the-draupnir-project/interface-manager";
import { DescriptionMeta } from "matrix-protection-suite";
import { Capability, DescriptionMeta } from "matrix-protection-suite";

export enum MessageType {
Document = "Document",
Expand All @@ -12,13 +12,22 @@ export enum MessageType {
}

export interface RendererMessageCollector {
addMessage(protection: DescriptionMeta, message: DocumentNode): void;
addOneliner(protection: DescriptionMeta, message: DocumentNode): void;
addMessage(
protection: DescriptionMeta,
capability: Capability,
message: DocumentNode
): void;
addOneliner(
protection: DescriptionMeta,
capability: Capability,
message: DocumentNode
): void;
getMessages(): RendererMessage[];
}

export interface RendererMessage {
protection: DescriptionMeta;
capability: Capability;
message: DocumentNode;
type: MessageType;
}
Expand All @@ -33,17 +42,27 @@ export class AbstractRendererMessageCollector
public getMessages(): RendererMessage[] {
return this.messages;
}
addMessage(protection: DescriptionMeta, message: DocumentNode): void {
addMessage(
protection: DescriptionMeta,
capability: Capability,
message: DocumentNode
): void {
this.messages.push({
protection,
capability,
message,
type: MessageType.Document,
});
}

addOneliner(protection: DescriptionMeta, message: DocumentNode): void {
addOneliner(
protection: DescriptionMeta,
capability: Capability,
message: DocumentNode
): void {
this.messages.push({
protection,
capability,
message,
type: MessageType.OneLine,
});
Expand Down
25 changes: 24 additions & 1 deletion src/capabilities/ServerACLConsequencesRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class StandardServerConsequencesRenderer implements ServerConsequences {
if (isError(capabilityResult)) {
this.messageCollector.addMessage(
this.description,
this.capability,
renderFailedSingularConsequence(
this.description,
title,
Expand All @@ -72,7 +73,11 @@ class StandardServerConsequencesRenderer implements ServerConsequences {
}
// only add the message if we changed anything in the room.
if (capabilityResult.ok) {
this.messageCollector.addOneliner(this.description, title);
this.messageCollector.addOneliner(
this.description,
this.capability,
title
);
}
return capabilityResult;
}
Expand All @@ -85,6 +90,7 @@ class StandardServerConsequencesRenderer implements ServerConsequences {
if (isError(capabilityResult)) {
this.messageCollector.addMessage(
this.description,
this.capability,
renderFailedSingularConsequence(
this.description,
title,
Expand All @@ -95,6 +101,7 @@ class StandardServerConsequencesRenderer implements ServerConsequences {
}
this.messageCollector.addMessage(
this.description,
this.capability,
renderRoomSetResult(capabilityResult.ok, {
summary: (
<fragment>
Expand All @@ -121,6 +128,7 @@ class StandardServerConsequencesRenderer implements ServerConsequences {
if (isError(capabilityResult)) {
this.messageCollector.addMessage(
this.description,
this.capability,
renderFailedSingularConsequence(
this.description,
title,
Expand All @@ -131,6 +139,7 @@ class StandardServerConsequencesRenderer implements ServerConsequences {
}
this.messageCollector.addMessage(
this.description,
this.capability,
renderRoomSetResult(capabilityResult.ok, {
summary: (
<fragment>
Expand All @@ -154,6 +163,7 @@ describeCapabilityRenderer<ServerConsequences, Draupnir>({
capability
);
},
isDefaultForInterface: true,
});

describeCapabilityContextGlue<Draupnir, ServerACLConsequencesContext>({
Expand All @@ -169,3 +179,16 @@ describeCapabilityContextGlue<Draupnir, ServerACLConsequencesContext>({
});
},
});

describeCapabilityContextGlue<Draupnir, ServerACLConsequencesContext>({
name: "SimulatedServerConsequences",
glueMethod: function (
protectionDescription,
draupnir,
capabilityProvider
): Capability {
return capabilityProvider.factory(protectionDescription, {
protectedRoomsSet: draupnir.protectedRoomsSet,
} as ServerACLConsequencesContext);
},
});
4 changes: 3 additions & 1 deletion src/capabilities/StandardEventConsequencesRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class StandardEventConsequencesRenderer implements EventConsequences {
if (isError(capabilityResult)) {
this.messageCollector.addOneliner(
this.description,
this.capability,
renderFailedSingularConsequence(
this.description,
title,
Expand All @@ -65,7 +66,7 @@ class StandardEventConsequencesRenderer implements EventConsequences {
);
return capabilityResult;
}
this.messageCollector.addOneliner(this.description, title);
this.messageCollector.addOneliner(this.description, this.capability, title);
return capabilityResult;
}
}
Expand All @@ -81,6 +82,7 @@ describeCapabilityRenderer<EventConsequences, Draupnir>({
capability
);
},
isDefaultForInterface: true,
});

describeCapabilityContextGlue<Draupnir, { eventRedacter: RoomEventRedacter }>({
Expand Down
23 changes: 22 additions & 1 deletion src/capabilities/StandardUserConsequencesRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class StandardUserConsequencesRenderer implements UserConsequences {
if (isError(capabilityResult)) {
this.messageCollector.addMessage(
this.description,
this.capability,
renderFailedSingularConsequence(
this.description,
title,
Expand All @@ -135,7 +136,7 @@ class StandardUserConsequencesRenderer implements UserConsequences {
);
return capabilityResult;
}
this.messageCollector.addOneliner(this.description, title);
this.messageCollector.addOneliner(this.description, this.capability, title);
return Ok(undefined);
}
public async consequenceForUsersInRoomSet(
Expand All @@ -149,6 +150,7 @@ class StandardUserConsequencesRenderer implements UserConsequences {
);
this.messageCollector.addMessage(
this.description,
this.capability,
renderFailedSingularConsequence(
this.description,
title,
Expand All @@ -163,6 +165,7 @@ class StandardUserConsequencesRenderer implements UserConsequences {
}
this.messageCollector.addMessage(
this.description,
this.capability,
renderResultForUserInSetMap(usersInSetMap, {
ingword: "Banning",
nnedword: "banned",
Expand All @@ -188,6 +191,7 @@ class StandardUserConsequencesRenderer implements UserConsequences {
);
this.messageCollector.addMessage(
this.description,
this.capability,
renderFailedSingularConsequence(
this.description,
title,
Expand All @@ -202,6 +206,7 @@ class StandardUserConsequencesRenderer implements UserConsequences {
}
this.messageCollector.addMessage(
this.description,
this.capability,
renderResultForUsersInRoom(resultMap, {
summary: (
<fragment>
Expand Down Expand Up @@ -233,6 +238,7 @@ class StandardUserConsequencesRenderer implements UserConsequences {
);
this.messageCollector.addMessage(
this.description,
this.capability,
renderFailedSingularConsequence(
this.description,
title,
Expand All @@ -247,6 +253,7 @@ class StandardUserConsequencesRenderer implements UserConsequences {
}
this.messageCollector.addMessage(
this.description,
this.capability,
renderRoomSetResultForUser(usersInSetMap, userID, "unbanned", {
description: this.description,
})
Expand All @@ -266,6 +273,7 @@ describeCapabilityRenderer<UserConsequences, Draupnir>({
capability
);
},
isDefaultForInterface: true,
});

describeCapabilityContextGlue<Draupnir, StandardUserConsequencesContext>({
Expand All @@ -282,3 +290,16 @@ describeCapabilityContextGlue<Draupnir, StandardUserConsequencesContext>({
});
},
});

describeCapabilityContextGlue<Draupnir, StandardUserConsequencesContext>({
name: "SimulatedUserConsequences",
glueMethod: function (
protectionDescription,
draupnir,
capabilityProvider
): Capability {
return capabilityProvider.factory(protectionDescription, {
setMembership: draupnir.protectedRoomsSet.setRoomMembership,
} as StandardUserConsequencesContext);
},
});
6 changes: 6 additions & 0 deletions src/commands/DraupnirCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { DraupnirImportCommand } from "./ImportCommand";
import { DraupnirKickCommand } from "./KickCommand";
import {
DraupnirListProtectionsCommand,
DraupnirProtectionsCapabilityResetCommand,
DraupnirProtectionsConfigAddCommand,
DraupnirProtectionsConfigRemoveCommand,
DraupnirProtectionsConfigResetCommand,
Expand Down Expand Up @@ -75,6 +76,11 @@ const DraupnirCommands = new StandardCommandTable("draupnir")
"protections",
"capability",
])
.internCommand(DraupnirProtectionsCapabilityResetCommand, [
"protections",
"capability",
"reset",
])
.internCommand(DraupnirProtectionsEnableCommand, ["protections", "enable"])
.internCommand(DraupnirProtectionsDisableCommand, ["protections", "disable"])
.internCommand(DraupnirProtectionsConfigAddCommand, [
Expand Down
Loading

0 comments on commit 9587d6f

Please sign in to comment.