Skip to content

Commit 6deb04f

Browse files
authored
Parse matrix.to links when banning (#609)
1 parent 27d9557 commit 6deb04f

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/commands/UnbanBanCommand.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ limitations under the License.
1616

1717
import { Mjolnir } from "../Mjolnir";
1818
import PolicyList from "../models/PolicyList";
19-
import { extractRequestError, LogLevel, LogService, MatrixGlob, RichReply } from "@vector-im/matrix-bot-sdk";
19+
import {
20+
extractRequestError,
21+
LogLevel,
22+
LogService,
23+
MatrixGlob,
24+
RichReply,
25+
Permalinks,
26+
} from "@vector-im/matrix-bot-sdk";
2027
import { RULE_ROOM, RULE_SERVER, RULE_USER, USER_RULE_TYPES } from "../models/ListRule";
2128
import { DEFAULT_LIST_EVENT_TYPE } from "./SetDefaultBanListCommand";
2229

@@ -51,7 +58,7 @@ export async function parseArguments(
5158
let list: PolicyList | null = null;
5259
let force = false;
5360
while (argumentIndex < 7 && argumentIndex < parts.length) {
54-
const arg = parts[argumentIndex++];
61+
let arg = parts[argumentIndex++];
5562
if (!arg) break;
5663
if (["user", "room", "server"].includes(arg.toLowerCase())) {
5764
if (arg.toLowerCase() === "user") ruleType = RULE_USER;
@@ -72,6 +79,22 @@ export async function parseArguments(
7279
}
7380
}
7481

82+
if (arg.startsWith("https://matrix.to/#/")) {
83+
// it should be a matrix.to link, parse it
84+
try {
85+
const permParts = Permalinks.parseUrl(arg);
86+
if (permParts.userId) {
87+
entity = permParts.userId;
88+
ruleType = RULE_USER;
89+
} else if (permParts.roomIdOrAlias) {
90+
entity = permParts.roomIdOrAlias;
91+
ruleType = RULE_ROOM;
92+
}
93+
} catch (e) {
94+
// okay it's not a matrix.to link, run it through the rest of the parser
95+
}
96+
}
97+
7598
if (entity) break;
7699
}
77100

test/commands/UnbanBanCommandTest.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,51 @@ describe("UnbanBanCommand", () => {
307307
expect(bits!.list!.listShortcode).toBe("test");
308308
});
309309

310+
it("should be able to parse matrix.to links", async () => {
311+
const mjolnir = createTestMjolnir();
312+
(<any>mjolnir).policyListManager.lists = [{ listShortcode: "test" }];
313+
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
314+
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
315+
};
316+
317+
const userCommand = "!mjolnir ban test https://matrix.to/#/@bad_user:test.org";
318+
const userBits = await parseArguments("!a", createFakeEvent(userCommand), mjolnir, userCommand.split(" "));
319+
expect(userBits!.ruleType).toBe(RULE_USER);
320+
expect(userBits!.entity).toBe("@bad_user:test.org");
321+
expect(userBits!.list).toBeDefined();
322+
expect(userBits!.list!.listShortcode).toBe("test");
323+
expect(userBits).toBeTruthy();
324+
expect(userBits!.reason).toBeFalsy();
325+
326+
const roomCommand = "!mjolnir ban test https://matrix.to/#/!bummerRoom:test.org";
327+
const commandBits = await parseArguments(
328+
"!a",
329+
createFakeEvent(roomCommand),
330+
mjolnir,
331+
roomCommand.split(" "),
332+
);
333+
expect(commandBits!.ruleType).toBe(RULE_ROOM);
334+
expect(commandBits!.entity).toBe("!bummerRoom:test.org");
335+
expect(commandBits!.list).toBeDefined();
336+
expect(commandBits!.list!.listShortcode).toBe("test");
337+
expect(commandBits).toBeTruthy();
338+
expect(commandBits!.reason).toBeFalsy();
339+
340+
const aliasCommand = "!mjolnir ban test https://matrix.to/#/#bad-actors:test.org";
341+
const aliasBits = await parseArguments(
342+
"!a",
343+
createFakeEvent(aliasCommand),
344+
mjolnir,
345+
aliasCommand.split(" "),
346+
);
347+
expect(aliasBits!.ruleType).toBe(RULE_ROOM);
348+
expect(aliasBits!.entity).toBe("#bad-actors:test.org");
349+
expect(aliasBits!.list).toBeDefined();
350+
expect(aliasBits!.list!.listShortcode).toBe("test");
351+
expect(aliasBits).toBeTruthy();
352+
expect(aliasBits!.reason).toBeFalsy();
353+
});
354+
310355
it("should error if wildcards used without --force", async () => {
311356
const mjolnir = createTestMjolnir();
312357
(<any>mjolnir).policyListManager.lists = [{ listShortcode: "test" }];

0 commit comments

Comments
 (0)