From b30df3f8d4d638602c10b60336dc563a095c4664 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sat, 11 Apr 2020 16:56:55 +0300 Subject: [PATCH] Automod: add include_words/exclude_words and include_regex/exclude_regex to match_links trigger --- backend/src/plugins/Automod/Automod.ts | 51 ++++++++++++++++++++++---- backend/src/plugins/Automod/types.ts | 4 ++ backend/src/utils.ts | 15 +++++--- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/backend/src/plugins/Automod/Automod.ts b/backend/src/plugins/Automod/Automod.ts index d9707af4..17b69153 100644 --- a/backend/src/plugins/Automod/Automod.ts +++ b/backend/src/plugins/Automod/Automod.ts @@ -458,14 +458,39 @@ export class AutomodPlugin extends ZeppelinPlugin Include + // In order of specificity, regex > word > domain + + if (trigger.exclude_regex) { + for (const pattern of trigger.exclude_regex) { + if (pattern.test(link.input)) { + return null; } - if (trigger.include_subdomains && normalizedHostname.endsWith(`.${domain}`)) { - return domain; + } + } + + if (trigger.include_regex) { + for (const pattern of trigger.include_regex) { + if (pattern.test(link.input)) { + return link.input; + } + } + } + + if (trigger.exclude_words) { + for (const word of trigger.exclude_words) { + const regex = new RegExp(escapeStringRegexp(word), "i"); + if (regex.test(link.input)) { + return null; + } + } + } + + if (trigger.include_words) { + for (const word of trigger.include_words) { + const regex = new RegExp(escapeStringRegexp(word), "i"); + if (regex.test(link.input)) { + return link.input; } } } @@ -483,6 +508,18 @@ export class AutomodPlugin extends ZeppelinPlugin { - if (!protocolRegex.test(match)) { - match = `https://${match}`; - } + const withProtocol = protocolRegex.test(match) ? match : `https://${match}`; - let matchUrl: url.URL; + let matchUrl: MatchedURL; try { - matchUrl = new url.URL(match); + matchUrl = new url.URL(withProtocol) as MatchedURL; + matchUrl.input = match; } catch (e) { return urls; }