Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve messaging and checks in NSFW protection #529

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/protections/NsfwProtection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,50 @@ export class NsfwProtection extends Protection {
if (event['type'] === 'm.room.message') {
const content = event['content'] || {};
const msgtype = content['msgtype'] || 'm.text';
const isMedia = msgtype === 'm.image';
const formattedBody = content['formatted_body'] || '';
const isMedia = msgtype === 'm.image' || formattedBody.toLowerCase().includes('<img');

if (isMedia) {
const mxc = content["url"];
let mxc;
if (formattedBody) {
const body = content["body"]
const mxcRegex = /(mxc?:\/\/[^\s]+)/g;
const url = body.match(mxcRegex)
if (url) {
mxc = url[0]
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
mxc = content["url"];
}
const image = await mjolnir.client.downloadContent(mxc);
const decodedImage = await node.decodeImage(image.data, 3);
const predictions = await this.model.classify(decodedImage);
// try and grab a human-readable alias for more helpful management room output
const maybeAlias = await mjolnir.client.getPublishedAlias(roomId)
const room = maybeAlias ? maybeAlias : roomId

for (const prediction of predictions) {
if (["Hentai", "Porn"].includes(prediction["className"])) {
if (prediction["probability"] > mjolnir.config.nsfwSensitivity) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.INFO, "NSFWProtection", `Redacting ${event["event_id"]} for inappropriate content.`);
try {
await mjolnir.client.redactEvent(roomId, event["event_id"]);
} catch (err) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "NSFWProtection", `There was an error redacting ${event["event_id"]}: ${err}`);
await mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "NSFWProtection", `There was an error redacting ${event["event_id"] in {room}}: ${err}`);
}
let eventId = event["event_id"]
let body = `Redacted an image in ${room} ${eventId}`
let formatted_body = `<details>
<summary>Redacted an image in ${room}</summary>
<pre>${eventId} ${room}</pre>
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
</details>`
let msg = {
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
msgtype: "m.notice",
body: body,
format: "org.matrix.custom.html",
formatted_body: formatted_body
};
await mjolnir.client.sendMessage(mjolnir.managementRoomId, msg);
break
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/integration/nsfwProtectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,20 @@ describe("Test: NSFW protection", function () {
let content = {"msgtype": "m.image", "body": "test.jpeg", "url": mxc};
let imageMessage = await client.sendMessage(room, content);

let formatted_body = `<img src=${mxc} />`
let htmlContent = {
msgtype: "m.image",
body: formatted_body,
format: "org.matrix.custom.html",
formatted_body: formatted_body
};
let htmlMessage = await client.sendMessage(room, htmlContent)

await delay(500);
let processedImage = await client.getEvent(room, imageMessage);
assert.equal(Object.keys(processedImage.content).length, 0, "This event should have been redacted");

let processedHtml = await client.getEvent(room, htmlMessage)
assert.equal(Object.keys(processedHtml.content).length, 0, "This html image event should have been redacted")
});
});
Loading