mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-10-01 01:36:06 -04:00
Optimise further
This commit is contained in:
parent
4ec3f30069
commit
a51cd63dde
@ -20,7 +20,6 @@ import { LogLevel, Permalinks, UserID } from "@vector-im/matrix-bot-sdk";
|
|||||||
import { NumberProtectionSetting } from "./ProtectionSettings";
|
import { NumberProtectionSetting } from "./ProtectionSettings";
|
||||||
|
|
||||||
export const DEFAULT_MAX_MENTIONS = 10;
|
export const DEFAULT_MAX_MENTIONS = 10;
|
||||||
const USER_ID_SIGIL_REGEX = /(@|%40)/;
|
|
||||||
|
|
||||||
export class MentionSpam extends Protection {
|
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 {
|
public checkMentions(body: unknown|undefined, htmlBody: unknown|undefined, mentionArray: unknown|undefined): boolean {
|
||||||
const max = this.settings.maxMentions.value;
|
const max = this.settings.maxMentions.value;
|
||||||
const minMessageLength = max * 3; // "@:a"
|
|
||||||
if (Array.isArray(mentionArray)) {
|
if (Array.isArray(mentionArray)) {
|
||||||
if (mentionArray.length > this.settings.maxMentions.value) {
|
if (mentionArray.length > this.settings.maxMentions.value) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (typeof body === "string" && body.length > minMessageLength) {
|
if (typeof body === "string" && body.split('@').length - 1 > max) {
|
||||||
let found = 0;
|
return true;
|
||||||
for (const word of body.split(/\s/)) {
|
|
||||||
if (USER_ID_SIGIL_REGEX.test(word.trim())) {
|
|
||||||
if (++found > max) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (typeof htmlBody === "string" && htmlBody.length > minMessageLength) {
|
if (typeof htmlBody === "string" && htmlBody.split('%40').length - 1 > max) {
|
||||||
let found = 0;
|
return true;
|
||||||
for (const word of htmlBody.split(/\s/)) {
|
|
||||||
if (USER_ID_SIGIL_REGEX.test(word.trim())) {
|
|
||||||
if (++found > max) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user