mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 20:35:02 +00:00
perf(automod): also merge regexes in match_links, match_words
This commit is contained in:
parent
aea6999753
commit
44f5b77cc7
5 changed files with 85 additions and 43 deletions
|
@ -7,12 +7,18 @@ import { TRegex } from "../../../validatorUtils";
|
|||
import { getTextMatchPartialSummary } from "../functions/getTextMatchPartialSummary";
|
||||
import { MatchableTextType, matchMultipleTextTypesOnMessage } from "../functions/matchMultipleTextTypesOnMessage";
|
||||
import { automodTrigger } from "../helpers";
|
||||
import { mergeRegexes } from "../../../utils/mergeRegexes";
|
||||
import { mergeWordsIntoRegex } from "../../../utils/mergeWordsIntoRegex";
|
||||
|
||||
interface MatchResultType {
|
||||
type: MatchableTextType;
|
||||
link: string;
|
||||
}
|
||||
|
||||
const regexCache = new WeakMap<any, RegExp[]>();
|
||||
|
||||
const quickLinkCheck = /^https?:\/\//i;
|
||||
|
||||
export const MatchLinksTrigger = automodTrigger<MatchResultType>()({
|
||||
configType: t.type({
|
||||
include_domains: tNullable(t.array(t.string)),
|
||||
|
@ -52,7 +58,7 @@ export const MatchLinksTrigger = automodTrigger<MatchResultType>()({
|
|||
|
||||
for (const link of links) {
|
||||
// "real link" = a link that Discord highlights
|
||||
if (trigger.only_real_links && !link.input.match(/^https?:\/\//i)) {
|
||||
if (trigger.only_real_links && !quickLinkCheck.test(link.input)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -62,7 +68,13 @@ export const MatchLinksTrigger = automodTrigger<MatchResultType>()({
|
|||
// In order of specificity, regex > word > domain
|
||||
|
||||
if (trigger.exclude_regex) {
|
||||
for (const sourceRegex of trigger.exclude_regex) {
|
||||
if (!regexCache.has(trigger.exclude_regex)) {
|
||||
const toCache = mergeRegexes(trigger.exclude_regex, "i");
|
||||
regexCache.set(trigger.exclude_regex, toCache);
|
||||
}
|
||||
const regexes = regexCache.get(trigger.exclude_regex)!;
|
||||
|
||||
for (const sourceRegex of regexes) {
|
||||
const matches = await pluginData.state.regexRunner.exec(sourceRegex, link.input).catch(allowTimeout);
|
||||
if (matches) {
|
||||
continue typeLoop;
|
||||
|
@ -71,7 +83,13 @@ export const MatchLinksTrigger = automodTrigger<MatchResultType>()({
|
|||
}
|
||||
|
||||
if (trigger.include_regex) {
|
||||
for (const sourceRegex of trigger.include_regex) {
|
||||
if (!regexCache.has(trigger.include_regex)) {
|
||||
const toCache = mergeRegexes(trigger.include_regex, "i");
|
||||
regexCache.set(trigger.include_regex, toCache);
|
||||
}
|
||||
const regexes = regexCache.get(trigger.include_regex)!;
|
||||
|
||||
for (const sourceRegex of regexes) {
|
||||
const matches = await pluginData.state.regexRunner.exec(sourceRegex, link.input).catch(allowTimeout);
|
||||
if (matches) {
|
||||
return { extra: { type, link: link.input } };
|
||||
|
@ -80,8 +98,13 @@ export const MatchLinksTrigger = automodTrigger<MatchResultType>()({
|
|||
}
|
||||
|
||||
if (trigger.exclude_words) {
|
||||
for (const word of trigger.exclude_words) {
|
||||
const regex = new RegExp(escapeStringRegexp(word), "i");
|
||||
if (!regexCache.has(trigger.exclude_words)) {
|
||||
const toCache = mergeWordsIntoRegex(trigger.exclude_words, "i");
|
||||
regexCache.set(trigger.exclude_words, [toCache]);
|
||||
}
|
||||
const regexes = regexCache.get(trigger.exclude_words)!;
|
||||
|
||||
for (const regex of regexes) {
|
||||
if (regex.test(link.input)) {
|
||||
continue typeLoop;
|
||||
}
|
||||
|
@ -89,8 +112,13 @@ export const MatchLinksTrigger = automodTrigger<MatchResultType>()({
|
|||
}
|
||||
|
||||
if (trigger.include_words) {
|
||||
for (const word of trigger.include_words) {
|
||||
const regex = new RegExp(escapeStringRegexp(word), "i");
|
||||
if (!regexCache.has(trigger.include_words)) {
|
||||
const toCache = mergeWordsIntoRegex(trigger.include_words, "i");
|
||||
regexCache.set(trigger.include_words, [toCache]);
|
||||
}
|
||||
const regexes = regexCache.get(trigger.include_words)!;
|
||||
|
||||
for (const regex of regexes) {
|
||||
if (regex.test(link.input)) {
|
||||
return { extra: { type, link: link.input } };
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue