From a51cd63ddec7bed49cf04dfb4aecbaa79d189361 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Fri, 20 Sep 2024 09:22:43 +0100 Subject: [PATCH] Optimise further --- src/protections/MentionSpam.ts | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/src/protections/MentionSpam.ts b/src/protections/MentionSpam.ts index 418774c..7df348c 100644 --- a/src/protections/MentionSpam.ts +++ b/src/protections/MentionSpam.ts @@ -20,7 +20,6 @@ import { LogLevel, Permalinks, UserID } from "@vector-im/matrix-bot-sdk"; import { NumberProtectionSetting } from "./ProtectionSettings"; export const DEFAULT_MAX_MENTIONS = 10; -const USER_ID_SIGIL_REGEX = /(@|%40)/; export class MentionSpam extends Protection { @@ -41,31 +40,16 @@ export class MentionSpam extends Protection { public checkMentions(body: unknown|undefined, htmlBody: unknown|undefined, mentionArray: unknown|undefined): boolean { const max = this.settings.maxMentions.value; - const minMessageLength = max * 3; // "@:a" if (Array.isArray(mentionArray)) { if (mentionArray.length > this.settings.maxMentions.value) { return true; } } - if (typeof body === "string" && body.length > minMessageLength) { - let found = 0; - for (const word of body.split(/\s/)) { - if (USER_ID_SIGIL_REGEX.test(word.trim())) { - if (++found > max) { - return true; - } - } - } + if (typeof body === "string" && body.split('@').length - 1 > max) { + return true; } - if (typeof htmlBody === "string" && htmlBody.length > minMessageLength) { - let found = 0; - for (const word of htmlBody.split(/\s/)) { - if (USER_ID_SIGIL_REGEX.test(word.trim())) { - if (++found > max) { - return true; - } - } - } + if (typeof htmlBody === "string" && htmlBody.split('%40').length - 1 > max) { + return true; } return false; }