2021-06-28 09:38:11 +00:00
/ *
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 .
* /
2022-02-02 15:31:11 +00:00
import { Protection } from "./IProtection" ;
2021-06-28 09:38:11 +00:00
import { Mjolnir } from "../Mjolnir" ;
import { LogLevel , Permalinks , UserID } from "matrix-bot-sdk" ;
2022-02-02 15:31:11 +00:00
export class MessageIsMedia extends Protection {
2021-06-28 09:38:11 +00:00
2022-01-25 14:47:50 +00:00
settings = { } ;
2021-06-28 09:38:11 +00:00
constructor ( ) {
2022-02-02 15:31:11 +00:00
super ( ) ;
2021-06-28 09:38:11 +00:00
}
public get name ( ) : string {
return 'MessageIsMediaProtection' ;
}
2022-02-02 15:31:11 +00:00
public get description ( ) : string {
return "If a user posts an image or video, that message will be redacted. No bans are issued." ;
}
2021-06-28 09:38:11 +00:00
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 formattedBody = content [ 'formatted_body' ] || '' ;
const isMedia = msgtype === 'm.image' || msgtype === 'm.video' || formattedBody . toLowerCase ( ) . includes ( '<img' ) ;
if ( isMedia ) {
2022-09-29 13:49:09 +00:00
await mjolnir . managementRoomOutput . logMessage ( LogLevel . WARN , "MessageIsMedia" , ` Redacting event from ${ event [ 'sender' ] } for posting an image/video. ${ Permalinks . forEvent ( roomId , event [ 'event_id' ] , [ new UserID ( await mjolnir . client . getUserId ( ) ) . domain ] ) } ` ) ;
2021-06-28 09:38:11 +00:00
// Redact the event
2022-08-09 10:29:27 +00:00
if ( ! mjolnir . config . noop ) {
2021-06-28 09:38:11 +00:00
await mjolnir . client . redactEvent ( roomId , event [ 'event_id' ] , "Images/videos are not permitted here" ) ;
} else {
2022-09-29 13:49:09 +00:00
await mjolnir . managementRoomOutput . logMessage ( LogLevel . WARN , "MessageIsMedia" , ` Tried to redact ${ event [ 'event_id' ] } in ${ roomId } but Mjolnir is running in no-op mode ` , roomId ) ;
2021-06-28 09:38:11 +00:00
}
}
}
}
}