Skip to content

Commit

Permalink
Add licence + test for spam.
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Oct 25, 2024
1 parent e895b9c commit 57a0b1f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Mjolnir.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
Copyright 2019-2024 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
58 changes: 58 additions & 0 deletions test/integration/forwardedMentionsTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
/*
Copyright 2024 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { MatrixClient, UserID } from "@vector-im/matrix-bot-sdk";
import { Mjolnir } from "../../src/Mjolnir";
import { newTestUser, noticeListener } from "./clientHelper";
import { strict as assert } from "assert";
import expect from "expect";

describe("Test: config.forwardMentionsToManagementRoom behaves correctly.", function () {
let moderator: MatrixClient;
Expand Down Expand Up @@ -56,4 +73,45 @@ describe("Test: config.forwardMentionsToManagementRoom behaves correctly.", func
"Forwarded mention format mismatch",
);
});

it("only forwards the first mention from a user.", async function () {
const mjolnir: Mjolnir = this.mjolnir!;
const botUserId = await mjolnir.client.getUserId();
mjolnir.config.forwardMentionsToManagementRoom = true;

const mentioninguser = await newTestUser(this.config.homeserverUrl, { name: { contains: "mentioninguser" } });
await moderator.joinRoom(mjolnir.managementRoomId);
const protectedRoom = await moderator.createRoom({ preset: "public_chat" });
await mjolnir.client.joinRoom(protectedRoom);
await mentioninguser.joinRoom(protectedRoom);
await mjolnir.protectedRoomsTracker.addProtectedRoom(protectedRoom);

await moderator.start();
let mentionCount = 0;
moderator.on(
"room.message",
noticeListener(this.mjolnir.managementRoomId, (event) => {
if (event.content.body.includes(`Bot mentioned`)) {
mentionCount++;
}
}),
);

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

for (let index = 0; index < 5; index++) {
await mentioninguser.sendMessage(protectedRoom, {
msgtype: "m.text",
body: `Moderator: Testing this ${index}`,
["m.mentions"]: {
user_ids: [botUserId],
},
});
await delay(2000);
}

expect(mentionCount).toBe(1);
});
});

0 comments on commit 57a0b1f

Please sign in to comment.