-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
544 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
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 { Protection } from "./IProtection"; | ||
import { Mjolnir } from "../Mjolnir"; | ||
import * as nsfw from 'nsfwjs'; | ||
import {LogLevel} from "matrix-bot-sdk"; | ||
import { node } from '@tensorflow/tfjs-node'; | ||
|
||
|
||
export class NsfwProtection extends Protection { | ||
settings = {}; | ||
// @ts-ignore | ||
private model: any; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
async initialize() { | ||
this.model = await nsfw.load(); | ||
} | ||
|
||
public get name(): string { | ||
return 'NsfwProtection'; | ||
} | ||
|
||
public get description(): string { | ||
return "Scans all images sent into a protected room to determine if the image is " + | ||
"NSFW. If it is, the image will automatically be redacted."; | ||
} | ||
|
||
public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise<any> { | ||
if (event['type'] === 'm.room.message') { | ||
const content = event['content'] || {}; | ||
const msgtype = content['msgtype'] || 'm.text'; | ||
const isMedia = msgtype === 'm.image'; | ||
|
||
if (isMedia) { | ||
const 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); | ||
|
||
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 { | ||
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}`); | ||
|
||
} | ||
} | ||
} | ||
} | ||
decodedImage.dispose(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {newTestUser} from "./clientHelper"; | ||
|
||
import {MatrixClient} from "matrix-bot-sdk"; | ||
import {getFirstReaction} from "./commands/commandUtils"; | ||
import {strict as assert} from "assert"; | ||
import { readFileSync } from 'fs'; | ||
|
||
describe("Test: NSFW protection", function () { | ||
let client: MatrixClient; | ||
let room: string; | ||
this.beforeEach(async function () { | ||
client = await newTestUser(this.config.homeserverUrl, {name: {contains: "nsfw-protection"}}); | ||
await client.start(); | ||
const mjolnirId = await this.mjolnir.client.getUserId(); | ||
room = await client.createRoom({ invite: [mjolnirId] }); | ||
await client.joinRoom(room); | ||
await client.joinRoom(this.config.managementRoom); | ||
await client.setUserPowerLevel(mjolnirId, room, 100); | ||
}) | ||
this.afterEach(async function () { | ||
await client.stop(); | ||
}) | ||
|
||
function delay(ms: number) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
|
||
|
||
it("Nsfw protection doesn't redact sfw images", async function() { | ||
this.timeout(20000); | ||
|
||
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` }); | ||
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => { | ||
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir enable NsfwProtection` }); | ||
}); | ||
|
||
const data = readFileSync('test_tree.jpg'); | ||
const mxc = await client.uploadContent(data, 'image/png'); | ||
let content = {"msgtype": "m.image", "body": "test.jpeg", "url": mxc}; | ||
let imageMessage = await client.sendMessage(room, content); | ||
|
||
await delay(500); | ||
let processedImage = await client.getEvent(room, imageMessage); | ||
assert.equal(Object.keys(processedImage.content).length, 3, "This event should not have been redacted"); | ||
}); | ||
|
||
it("Nsfw protection redacts nsfw images", async function() { | ||
this.timeout(20000); | ||
// dial the sensitivity on the protection way up so that all images are flagged as NSFW | ||
this.mjolnir.config.nsfwSensitivity = 0.0; | ||
|
||
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` }); | ||
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => { | ||
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir enable NsfwProtection` }); | ||
}); | ||
|
||
const data = readFileSync('test_tree.jpg'); | ||
const mxc = await client.uploadContent(data, 'image/png'); | ||
let content = {"msgtype": "m.image", "body": "test.jpeg", "url": mxc}; | ||
let imageMessage = await client.sendMessage(room, content); | ||
|
||
await delay(500); | ||
let processedImage = await client.getEvent(room, imageMessage); | ||
assert.equal(Object.keys(processedImage.content).length, 0, "This event should have been redacted"); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.