From 69ace9cfa57f8a7e4fcf5ee9a4c75df63d1f53df Mon Sep 17 00:00:00 2001 From: Chirayu Desai Date: Mon, 28 Jun 2021 15:08:11 +0530 Subject: [PATCH] Add a new protection, MessageIsMedia * Simply redacts any media sent to the room, by anyone * Meant to serve as a temporary measure to prevent image spam, in conjunction with FirstMessageIsImage --- src/protections/MessageIsMedia.ts | 49 +++++++++++++++++++++++++++++++ src/protections/protections.ts | 5 ++++ 2 files changed, 54 insertions(+) create mode 100644 src/protections/MessageIsMedia.ts diff --git a/src/protections/MessageIsMedia.ts b/src/protections/MessageIsMedia.ts new file mode 100644 index 0000000..a26289e --- /dev/null +++ b/src/protections/MessageIsMedia.ts @@ -0,0 +1,49 @@ +/* +Copyright 2019 - 2021 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 { IProtection } from "./IProtection"; +import { Mjolnir } from "../Mjolnir"; +import { LogLevel, Permalinks, UserID } from "matrix-bot-sdk"; +import { logMessage } from "../LogProxy"; +import config from "../config"; + +export class MessageIsMedia implements IProtection { + + constructor() { + } + + public get name(): string { + return 'MessageIsMediaProtection'; + } + + public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise { + if (event['type'] === 'm.room.message') { + const content = event['content'] || {}; + const msgtype = content['msgtype'] || 'm.text'; + const formattedBody = content['formatted_body'] || ''; + const isMedia = msgtype === 'm.image' || msgtype === 'm.video' || formattedBody.toLowerCase().includes(' new MessageIsVoice(), }, + [new MessageIsMedia().name]: { + description: "If a user posts an image or video, that message will be redacted. No bans are issued.", + factory: () => new MessageIsMedia(), + } }; export interface PossibleProtections {