Skip to content

Commit bdbfadc

Browse files
committed
Move to @the-draupnir-project/interface-manager for defining commands.
1 parent 79a0320 commit bdbfadc

File tree

74 files changed

+2543
-5720
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2543
-5720
lines changed

src/Draupnir.ts

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
isError,
3232
} from "matrix-protection-suite";
3333
import { UnlistedUserRedactionQueue } from "./queues/UnlistedUserRedactionQueue";
34-
import { findCommandTable } from "./commands/interface-manager/InterfaceCommand";
3534
import { ThrottlingQueue } from "./queues/ThrottlingQueue";
3635
import ManagementRoomOutput from "./ManagementRoomOutput";
3736
import { ReportPoller } from "./report/ReportPoller";
@@ -42,12 +41,6 @@ import {
4241
SynapseAdminClient,
4342
} from "matrix-protection-suite-for-matrix-bot-sdk";
4443
import { IConfig } from "./config";
45-
import {
46-
COMMAND_PREFIX,
47-
DraupnirContext,
48-
extractCommandFromMessageBody,
49-
handleCommand,
50-
} from "./commands/CommandHandler";
5144
import { LogLevel } from "matrix-bot-sdk";
5245
import {
5346
ARGUMENT_PROMPT_LISTENER,
@@ -59,7 +52,6 @@ import { RendererMessageCollector } from "./capabilities/RendererMessageCollecto
5952
import { DraupnirRendererMessageCollector } from "./capabilities/DraupnirRendererMessageCollector";
6053
import { renderProtectionFailedToStart } from "./protections/ProtectedRoomsSetRenderers";
6154
import { draupnirStatusInfo, renderStatusInfo } from "./commands/StatusCommand";
62-
import { renderMatrixAndSend } from "./commands/interface-manager/DeadDocumentMatrix";
6355
import { isInvitationForUser } from "./protections/invitation/inviteCore";
6456
import {
6557
StringRoomID,
@@ -68,9 +60,13 @@ import {
6860
isStringRoomID,
6961
isStringRoomAlias,
7062
MatrixRoomReference,
71-
userLocalpart,
7263
userServerName,
7364
} from "@the-draupnir-project/matrix-basic-types";
65+
import {
66+
MatrixAdaptorContext,
67+
sendMatrixEventsFromDeadDocument,
68+
} from "./commands/interface-manager/MPSMatrixInterfaceAdaptor";
69+
import { makeDraupnirCommandDispatcher } from "./commands/DraupnirCommandDispatcher";
7470

7571
const log = new Logger("Draupnir");
7672

@@ -80,14 +76,14 @@ const log = new Logger("Draupnir");
8076
// to Mjolnir because it needs to be started after Mjolnir started and not before.
8177
// And giving it to the class was a dumb easy way of doing that.
8278

83-
export class Draupnir implements Client {
79+
export class Draupnir implements Client, MatrixAdaptorContext {
8480
/**
8581
* This is for users who are not listed on a watchlist,
8682
* but have been flagged by the automatic spam detection as suispicous
8783
*/
8884
public unlistedUserRedactionQueue = new UnlistedUserRedactionQueue();
8985

90-
private readonly commandTable = findCommandTable("draupnir");
86+
private readonly commandDispatcher = makeDraupnirCommandDispatcher(this);
9187
public taskQueue: ThrottlingQueue;
9288
/**
9389
* Reporting back to the management room.
@@ -106,8 +102,6 @@ export class Draupnir implements Client {
106102

107103
public readonly reactionHandler: MatrixReactionHandler;
108104

109-
public readonly commandContext: Omit<DraupnirContext, "event">;
110-
111105
private readonly timelineEventListener = this.handleTimelineEvent.bind(this);
112106

113107
public readonly capabilityMessageRenderer: RendererMessageCollector;
@@ -146,38 +140,16 @@ export class Draupnir implements Client {
146140
if (config.pollReports) {
147141
this.reportPoller = new ReportPoller(this, this.reportManager);
148142
}
149-
150-
this.commandContext = {
151-
draupnir: this,
152-
roomID: this.managementRoomID,
153-
client: this.client,
154-
reactionHandler: this.reactionHandler,
155-
clientPlatform: this.clientPlatform,
156-
};
157143
this.reactionHandler.on(
158144
ARGUMENT_PROMPT_LISTENER,
159-
makeListenerForArgumentPrompt(
160-
this.client,
161-
this.clientPlatform,
162-
this.managementRoomID,
163-
this.reactionHandler,
164-
this.commandTable,
165-
this.commandContext
166-
)
145+
makeListenerForArgumentPrompt(this.commandDispatcher)
167146
);
168147
this.reactionHandler.on(
169148
DEFAUILT_ARGUMENT_PROMPT_LISTENER,
170-
makeListenerForPromptDefault(
171-
this.client,
172-
this.clientPlatform,
173-
this.managementRoomID,
174-
this.reactionHandler,
175-
this.commandTable,
176-
this.commandContext
177-
)
149+
makeListenerForPromptDefault(this.commandDispatcher)
178150
);
179151
this.capabilityMessageRenderer = new DraupnirRendererMessageCollector(
180-
this.client,
152+
this.clientPlatform.toRoomMessageSender(),
181153
this.managementRoomID
182154
);
183155
}
@@ -256,7 +228,7 @@ export class Draupnir implements Client {
256228
draupnir,
257229
(error, protectionName, description) =>
258230
renderProtectionFailedToStart(
259-
client,
231+
clientPlatform.toRoomMessageSender(),
260232
managementRoom.toRoomIDOrAlias(),
261233
error,
262234
protectionName,
@@ -291,11 +263,11 @@ export class Draupnir implements Client {
291263
"Mjolnir@startup",
292264
"Startup complete. Now monitoring rooms."
293265
);
294-
await renderMatrixAndSend(
295-
renderStatusInfo(statusInfo),
266+
await sendMatrixEventsFromDeadDocument(
267+
this.clientPlatform.toRoomMessageSender(),
296268
this.managementRoomID,
297-
undefined,
298-
this.client
269+
renderStatusInfo(statusInfo),
270+
{}
299271
);
300272
} catch (ex) {
301273
log.error(`Caught an error when trying to show status at startup`, ex);
@@ -355,33 +327,12 @@ export class Draupnir implements Client {
355327
);
356328
return;
357329
}
358-
const commandBeingRun = extractCommandFromMessageBody(
359-
event.content.body,
330+
this.commandDispatcher.handleCommandMessageEvent(
360331
{
361-
prefix: COMMAND_PREFIX,
362-
localpart: userLocalpart(this.clientUserID),
363-
userId: this.clientUserID,
364-
additionalPrefixes: this.config.commands.additionalPrefixes,
365-
allowNoPrefix: this.config.commands.allowNoPrefix,
366-
}
367-
);
368-
if (commandBeingRun === undefined) {
369-
return;
370-
}
371-
log.info(`Command being run by ${event.sender}: ${commandBeingRun}`);
372-
void Task(
373-
this.client
374-
.sendReadReceipt(roomID, event.event_id)
375-
.then((_) => Ok(undefined))
376-
);
377-
void Task(
378-
handleCommand(
379-
roomID,
380332
event,
381-
commandBeingRun,
382-
this,
383-
this.commandTable
384-
).then((_) => Ok(undefined))
333+
roomID,
334+
},
335+
event.content.body
385336
);
386337
}
387338
this.reportManager.handleTimelineEvent(roomID, event);
@@ -414,4 +365,11 @@ export class Draupnir implements Client {
414365
public handleEventReport(report: EventReport): void {
415366
this.protectedRoomsSet.handleEventReport(report);
416367
}
368+
369+
/**
370+
* This is needed to implement the MatrixInterfaceAdaptor interface.
371+
*/
372+
public get commandRoomID() {
373+
return this.managementRoomID;
374+
}
417375
}

src/appservice/AppService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const log = new Logger("AppService");
5656
*/
5757
export class MjolnirAppService {
5858
private readonly api: Api;
59-
private readonly commands: AppserviceCommandHandler;
59+
public readonly commands: AppserviceCommandHandler;
6060

6161
/**
6262
* The constructor is private because we want to ensure intialization steps are followed,

src/appservice/bot/AccessCommands.tsx

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,57 @@
22
//
33
// SPDX-License-Identifier: AFL-3.0
44

5-
import {
6-
defineInterfaceCommand,
7-
findTableCommand,
8-
} from "../../commands/interface-manager/InterfaceCommand";
9-
import {
10-
findPresentationType,
11-
parameters,
12-
ParsedKeywords,
13-
} from "../../commands/interface-manager/ParameterParsing";
14-
import { AppserviceContext } from "./AppserviceCommandHandler";
5+
import { AppserviceAdaptorContext } from "./AppserviceBotPrerequisite";
156
import { ActionResult } from "matrix-protection-suite";
16-
import { defineMatrixInterfaceAdaptor } from "../../commands/interface-manager/MatrixInterfaceAdaptor";
17-
import { tickCrossRenderer } from "../../commands/interface-manager/MatrixHelpRenderer";
18-
import { MatrixUserID } from "@the-draupnir-project/matrix-basic-types";
7+
import {
8+
MatrixUserIDPresentationType,
9+
describeCommand,
10+
tuple,
11+
} from "@the-draupnir-project/interface-manager";
12+
import { AppserviceBotInterfaceAdaptor } from "./AppserviceBotCommandDispatcher";
1913

20-
defineInterfaceCommand({
21-
designator: ["allow"],
22-
table: "appservice bot",
23-
parameters: parameters([
24-
{
25-
name: "user",
26-
acceptor: findPresentationType("MatrixUserID"),
27-
description: "The user that should be allowed to provision a bot",
28-
},
29-
]),
30-
command: async function (
31-
this: AppserviceContext,
32-
_keywords: ParsedKeywords,
33-
user: MatrixUserID
34-
): Promise<ActionResult<void>> {
35-
return await this.appservice.accessControl.allow(user.toString());
36-
},
14+
export const AppserviceAllowCommand = describeCommand({
15+
parameters: tuple({
16+
name: "user",
17+
acceptor: MatrixUserIDPresentationType,
18+
description: "The user that should be allowed to provision a bot",
19+
}),
3720
summary:
3821
"Allow a user to provision themselves a draupnir using the appservice.",
22+
async executor(
23+
context: AppserviceAdaptorContext,
24+
_info,
25+
_keywords,
26+
_rest,
27+
user
28+
): Promise<ActionResult<void>> {
29+
return await context.appservice.accessControl.allow(user.toString());
30+
},
3931
});
4032

41-
defineMatrixInterfaceAdaptor({
42-
interfaceCommand: findTableCommand("appservice bot", "allow"),
43-
renderer: tickCrossRenderer,
33+
AppserviceBotInterfaceAdaptor.describeRenderer(AppserviceAllowCommand, {
34+
isAlwaysSupposedToUseDefaultRenderer: true,
4435
});
4536

46-
defineInterfaceCommand({
47-
designator: ["remove"],
48-
table: "appservice bot",
49-
parameters: parameters([
50-
{
51-
name: "user",
52-
acceptor: findPresentationType("MatrixUserID"),
53-
description:
54-
"The user which shall not be allowed to provision bots anymore",
55-
},
56-
]),
57-
command: async function (
58-
this: AppserviceContext,
59-
_keywords: ParsedKeywords,
60-
user: MatrixUserID
37+
export const AppserviceRemoveCommand = describeCommand({
38+
parameters: tuple({
39+
name: "user",
40+
acceptor: MatrixUserIDPresentationType,
41+
description:
42+
"The user which shall not be allowed to provision bots anymore",
43+
}),
44+
summary: "Stop a user from using any provisioned draupnir in the appservice.",
45+
async executor(
46+
context: AppserviceAdaptorContext,
47+
_info,
48+
_keywords,
49+
_rest,
50+
user
6151
): Promise<ActionResult<void>> {
62-
return await this.appservice.accessControl.remove(user.toString());
52+
return await context.appservice.accessControl.remove(user.toString());
6353
},
64-
summary: "Stop a user from using any provisioned draupnir in the appservice.",
6554
});
6655

67-
defineMatrixInterfaceAdaptor({
68-
interfaceCommand: findTableCommand("appservice bot", "remove"),
69-
renderer: tickCrossRenderer,
56+
AppserviceBotInterfaceAdaptor.describeRenderer(AppserviceRemoveCommand, {
57+
isAlwaysSupposedToUseDefaultRenderer: true,
7058
});

0 commit comments

Comments
 (0)