mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-10-01 01:36:06 -04:00
Enhance media protections (#516)
This commit is contained in:
parent
77357e46af
commit
f526b972a4
@ -35,7 +35,11 @@ export class MessageIsMedia extends Protection {
|
||||
|
||||
public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise<any> {
|
||||
if (event['type'] === 'm.room.message') {
|
||||
const content = event['content'] || {};
|
||||
let content = event['content'] || {};
|
||||
const relation = content["m.relates_to"]
|
||||
if (relation?.["rel_type"] === "m.replace") {
|
||||
content = content?.["m.new_content"] ?? content;
|
||||
}
|
||||
const msgtype = content['msgtype'] || 'm.text';
|
||||
const formattedBody = content['formatted_body'] || '';
|
||||
const isMedia = msgtype === 'm.image' || msgtype === 'm.video' || msgtype === 'm.sticker' || formattedBody.toLowerCase().includes('<img');
|
||||
|
@ -6,12 +6,16 @@ import { ProtectionSettingValidationError } from "../../src/protections/Protecti
|
||||
import { NumberProtectionSetting, StringProtectionSetting, StringListProtectionSetting } from "../../src/protections/ProtectionSettings";
|
||||
import { newTestUser, noticeListener } from "./clientHelper";
|
||||
import { matrixClient, mjolnir } from "./mjolnirSetupUtils";
|
||||
import {MessageIsMedia} from "../../src/protections/MessageIsMedia";
|
||||
|
||||
describe("Test: Protection settings", function() {
|
||||
let client;
|
||||
let room;
|
||||
this.beforeEach(async function () {
|
||||
client = await newTestUser(this.config.homeserverUrl, { name: { contains: "protection-settings" }});
|
||||
await client.start();
|
||||
room = await client.createRoom();
|
||||
await client.joinRoom(room)
|
||||
})
|
||||
this.afterEach(async function () {
|
||||
await client.stop();
|
||||
@ -158,5 +162,32 @@ describe("Test: Protection settings", function() {
|
||||
"Changed d0sNrt.test to asd2 (was asd1)"
|
||||
)
|
||||
});
|
||||
it("Events are checked for new content under media protections", async function() {
|
||||
this.timeout(20000);
|
||||
await client.joinRoom(this.config.managementRoom);
|
||||
|
||||
await this.mjolnir.protectionManager.registerProtection(new MessageIsMedia());
|
||||
|
||||
// send a regular media message to make sure protections are running
|
||||
await client.sendMessage(room, {msgtype: "m.image", body: ""})
|
||||
let reply = () => new Promise((resolve, reject) => {
|
||||
client.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => {
|
||||
if (event.content.body.includes("Redacting event")) {
|
||||
resolve(event);
|
||||
}
|
||||
}))
|
||||
});
|
||||
await reply;
|
||||
|
||||
await client.sendMessage(room, {body: "", msgtype: "m.text", "m.new_content": {msgtype: "m.image", body: ""}, "m.relates_to": {"rel_type": "m.replace"}})
|
||||
let reply2 = () => new Promise((resolve, reject) => {
|
||||
client.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => {
|
||||
if (event.content.body.includes("Redacting event")) {
|
||||
resolve(event);
|
||||
}
|
||||
}))
|
||||
});
|
||||
await reply2;
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user