Respect no-op in more places

This commit is contained in:
Travis Ralston 2019-12-09 19:20:47 -07:00
parent f9e3c33935
commit 7de3744875
3 changed files with 29 additions and 6 deletions

View File

@ -18,6 +18,7 @@ import { IProtection } from "./IProtection";
import { Mjolnir } from "../Mjolnir";
import { LogLevel, LogService } from "matrix-bot-sdk";
import { logMessage } from "../LogProxy";
import config from "../config";
export const MAX_PER_MINUTE = 10; // if this is exceeded, we'll ban the user for spam and redact their messages
const TIMESTAMP_THRESHOLD = 30000; // 30s out of phase
@ -63,12 +64,20 @@ export class BasicFlooding implements IProtection {
this.recentlyBanned.push(event['sender']); // flag to reduce spam
// Redact all the things the user said too
for (const eventId of forUser.map(e => e.eventId)) {
await mjolnir.client.redactEvent(roomId, eventId, "spam");
if (!config.noop) {
for (const eventId of forUser.map(e => e.eventId)) {
await mjolnir.client.redactEvent(roomId, eventId, "spam");
}
} else {
await logMessage(LogLevel.WARN, "BasicFlooding", `Tried to redact messages for ${event['sender']} in ${roomId} but Mjolnir is running in no-op mode`);
}
await logMessage(LogLevel.WARN, "BasicFlooding", `Banning ${event['sender']} in ${roomId} for flooding (${messageCount} messages in the last minute)`);
await mjolnir.client.banUser(event['sender'], roomId, "spam");
if (!config.noop) {
await mjolnir.client.banUser(event['sender'], roomId, "spam");
} else {
await logMessage(LogLevel.WARN, "BasicFlooding", `Tried to ban ${event['sender']} in ${roomId} but Mjolnir is running in no-op mode`);
}
// Free up some memory now that we're ready to handle it elsewhere
forUser = forRoom[event['sender']] = []; // reset the user's list

View File

@ -18,6 +18,7 @@ import { IProtection } from "./IProtection";
import { Mjolnir } from "../Mjolnir";
import { LogLevel, LogService } from "matrix-bot-sdk";
import { logMessage } from "../LogProxy";
import config from "../config";
export class FirstMessageIsImage implements IProtection {
@ -63,10 +64,18 @@ export class FirstMessageIsImage implements IProtection {
this.recentlyBanned.push(event['sender']); // flag to reduce spam
// Redact the event
await mjolnir.client.redactEvent(roomId, event['event_id'], "spam");
if (!config.noop) {
await mjolnir.client.redactEvent(roomId, event['event_id'], "spam");
} else {
await logMessage(LogLevel.WARN, "FirstMessageIsImage", `Tried to redact ${event['event_id']} in ${roomId} but Mjolnir is running in no-op mode`);
}
await logMessage(LogLevel.WARN, "FirstMessageIsImage", `Banning ${event['sender']} for posting an image as the first thing after joining in ${roomId}.`);
await mjolnir.client.banUser(event['sender'], roomId, "spam");
if (!config.noop) {
await mjolnir.client.banUser(event['sender'], roomId, "spam");
} else {
await logMessage(LogLevel.WARN, "FirstMessageIsImage", `Tried to ban ${event['sender']} in ${roomId} but Mjolnir is running in no-op mode`);
}
}
}

View File

@ -16,6 +16,7 @@ limitations under the License.
import { LogLevel, LogService, MatrixClient, Permalinks } from "matrix-bot-sdk";
import { logMessage } from "../LogProxy";
import config from "../config";
export class AutomaticRedactionQueue {
private usersToRedact: Set<string> = new Set<string>();
@ -36,7 +37,11 @@ export class AutomaticRedactionQueue {
const permalink = Permalinks.forEvent(roomId, event['event_id']);
try {
LogService.info("AutomaticRedactionQueue", `Redacting event because the user is listed as bad: ${permalink}`)
await mjolnirClient.redactEvent(roomId, event['event_id']);
if (!config.noop) {
await mjolnirClient.redactEvent(roomId, event['event_id']);
} else {
await logMessage(LogLevel.WARN, "AutomaticRedactionQueue", `Tried to redact ${permalink} but Mjolnir is running in no-op mode`);
}
} catch (e) {
logMessage(LogLevel.WARN, "AutomaticRedactionQueue", `Unable to redact message: ${permalink}`);
LogService.warn("AutomaticRedactionQueue", e);