diff --git a/src/policies/anti-duplication-policy.ts b/src/policies/anti-duplication-policy.ts index 7db9701..bc984bf 100755 --- a/src/policies/anti-duplication-policy.ts +++ b/src/policies/anti-duplication-policy.ts @@ -16,10 +16,10 @@ interface AntiDuplication { * It stores a hashcode for each content in an SQLite database and rate-limits them. * Only messages that meet the minimum length criteria are selected. */ -const antiDuplicationPolicy: Policy = async (msg, opts) => { - const ttl = opts?.ttl ?? 60000; - const minLength = opts?.minLength ?? 50; - const databaseUrl = opts?.databaseUrl || 'sqlite:///tmp/strfry-anti-duplication-policy.sqlite3'; +const antiDuplicationPolicy: Policy = async (msg, opts = {}) => { + const ttl = opts.ttl ?? 60000; + const minLength = opts.minLength ?? 50; + const databaseUrl = opts.databaseUrl ?? 'sqlite:///tmp/strfry-anti-duplication-policy.sqlite3'; const { kind, content } = msg.event; diff --git a/src/policies/keyword-policy.test.ts b/src/policies/keyword-policy.test.ts index c134d02..eda6cf1 100644 --- a/src/policies/keyword-policy.test.ts +++ b/src/policies/keyword-policy.test.ts @@ -4,12 +4,14 @@ import { buildEvent, buildInputMessage } from '../test.ts'; import keywordPolicy from './keyword-policy.ts'; Deno.test('blocks banned pubkeys', async () => { - const words = ['https://t.me/spam']; + const words = ['https://t.me/spam', 'hello world']; const msg0 = buildInputMessage(); const msg1 = buildInputMessage({ event: buildEvent({ content: '🔥🔥🔥 https://t.me/spam 我想死' }) }); + const msg2 = buildInputMessage({ event: buildEvent({ content: 'hElLo wOrLd!' }) }); assertEquals((await keywordPolicy(msg0, words)).action, 'accept'); assertEquals((await keywordPolicy(msg1, words)).action, 'reject'); assertEquals((await keywordPolicy(msg1, [])).action, 'accept'); + assertEquals((await keywordPolicy(msg2, words)).action, 'reject'); }); diff --git a/src/policies/keyword-policy.ts b/src/policies/keyword-policy.ts index d4ffdef..a377840 100644 --- a/src/policies/keyword-policy.ts +++ b/src/policies/keyword-policy.ts @@ -2,23 +2,16 @@ import { Policy } from '../types.ts'; /** Reject events containing any of the strings in its content. */ const keywordPolicy: Policy> = ({ event: { id, content } }, words = []) => { - let isMatch = false; - for (const word of words) { - if (content.toLocaleLowerCase().includes(word.toLowerCase())) { - isMatch = true; - break; + if (content.toLowerCase().includes(word.toLowerCase())) { + return { + id, + action: 'reject', + msg: 'blocked: contains a banned word or phrase.', + }; } } - if (isMatch) { - return { - id, - action: 'reject', - msg: 'blocked: contains a banned word or phrase.', - }; - } - return { id, action: 'accept', diff --git a/src/policies/rate-limit-policy.ts b/src/policies/rate-limit-policy.ts index 4359bc6..deff9db 100755 --- a/src/policies/rate-limit-policy.ts +++ b/src/policies/rate-limit-policy.ts @@ -18,11 +18,13 @@ interface RateLimit { * IPs are stored in an SQLite database. If you are running internal services, * it's a good idea to at least whitelist `127.0.0.1` etc. */ -const rateLimitPolicy: Policy = async (msg, opts) => { - const interval = opts?.interval ?? 60000; - const max = opts?.max ?? 10; - const whitelist = opts?.whitelist || []; - const databaseUrl = opts?.databaseUrl || 'sqlite:///tmp/strfry-rate-limit-policy.sqlite3'; +const rateLimitPolicy: Policy = async (msg, opts = {}) => { + const { + interval = 60000, + max = 10, + whitelist = [], + databaseUrl = 'sqlite:///tmp/strfry-rate-limit-policy.sqlite3', + } = opts; if ((msg.sourceType === 'IP4' || msg.sourceType === 'IP6') && !whitelist.includes(msg.sourceInfo)) { const db = new Keydb(databaseUrl); diff --git a/src/policies/regex-policy.ts b/src/policies/regex-policy.ts index c50c1b6..4d8cc75 100644 --- a/src/policies/regex-policy.ts +++ b/src/policies/regex-policy.ts @@ -2,9 +2,7 @@ import { Policy } from '../types.ts'; /** Reject events whose content matches the regex. */ const regexPolicy: Policy = ({ event: { id, content } }, regex) => { - const isMatch = regex ? regex.test(content) : false; - - if (isMatch) { + if (regex?.test(content)) { return { id, action: 'reject',