More refactoring

This commit is contained in:
Alex Gleason 2023-03-29 23:51:15 -05:00
parent 48501f3c5f
commit b32bfef342
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 21 additions and 26 deletions

View File

@ -16,10 +16,10 @@ interface AntiDuplication {
* It stores a hashcode for each content in an SQLite database and rate-limits them. * 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. * Only messages that meet the minimum length criteria are selected.
*/ */
const antiDuplicationPolicy: Policy<AntiDuplication> = async (msg, opts) => { const antiDuplicationPolicy: Policy<AntiDuplication> = async (msg, opts = {}) => {
const ttl = opts?.ttl ?? 60000; const ttl = opts.ttl ?? 60000;
const minLength = opts?.minLength ?? 50; const minLength = opts.minLength ?? 50;
const databaseUrl = opts?.databaseUrl || 'sqlite:///tmp/strfry-anti-duplication-policy.sqlite3'; const databaseUrl = opts.databaseUrl ?? 'sqlite:///tmp/strfry-anti-duplication-policy.sqlite3';
const { kind, content } = msg.event; const { kind, content } = msg.event;

View File

@ -4,12 +4,14 @@ import { buildEvent, buildInputMessage } from '../test.ts';
import keywordPolicy from './keyword-policy.ts'; import keywordPolicy from './keyword-policy.ts';
Deno.test('blocks banned pubkeys', async () => { Deno.test('blocks banned pubkeys', async () => {
const words = ['https://t.me/spam']; const words = ['https://t.me/spam', 'hello world'];
const msg0 = buildInputMessage(); const msg0 = buildInputMessage();
const msg1 = buildInputMessage({ event: buildEvent({ content: '🔥🔥🔥 https://t.me/spam 我想死' }) }); 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(msg0, words)).action, 'accept');
assertEquals((await keywordPolicy(msg1, words)).action, 'reject'); assertEquals((await keywordPolicy(msg1, words)).action, 'reject');
assertEquals((await keywordPolicy(msg1, [])).action, 'accept'); assertEquals((await keywordPolicy(msg1, [])).action, 'accept');
assertEquals((await keywordPolicy(msg2, words)).action, 'reject');
}); });

View File

@ -2,22 +2,15 @@ import { Policy } from '../types.ts';
/** Reject events containing any of the strings in its content. */ /** Reject events containing any of the strings in its content. */
const keywordPolicy: Policy<Iterable<string>> = ({ event: { id, content } }, words = []) => { const keywordPolicy: Policy<Iterable<string>> = ({ event: { id, content } }, words = []) => {
let isMatch = false;
for (const word of words) { for (const word of words) {
if (content.toLocaleLowerCase().includes(word.toLowerCase())) { if (content.toLowerCase().includes(word.toLowerCase())) {
isMatch = true;
break;
}
}
if (isMatch) {
return { return {
id, id,
action: 'reject', action: 'reject',
msg: 'blocked: contains a banned word or phrase.', msg: 'blocked: contains a banned word or phrase.',
}; };
} }
}
return { return {
id, id,

View File

@ -18,11 +18,13 @@ interface RateLimit {
* IPs are stored in an SQLite database. If you are running internal services, * 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. * it's a good idea to at least whitelist `127.0.0.1` etc.
*/ */
const rateLimitPolicy: Policy<RateLimit> = async (msg, opts) => { const rateLimitPolicy: Policy<RateLimit> = async (msg, opts = {}) => {
const interval = opts?.interval ?? 60000; const {
const max = opts?.max ?? 10; interval = 60000,
const whitelist = opts?.whitelist || []; max = 10,
const databaseUrl = opts?.databaseUrl || 'sqlite:///tmp/strfry-rate-limit-policy.sqlite3'; whitelist = [],
databaseUrl = 'sqlite:///tmp/strfry-rate-limit-policy.sqlite3',
} = opts;
if ((msg.sourceType === 'IP4' || msg.sourceType === 'IP6') && !whitelist.includes(msg.sourceInfo)) { if ((msg.sourceType === 'IP4' || msg.sourceType === 'IP6') && !whitelist.includes(msg.sourceInfo)) {
const db = new Keydb(databaseUrl); const db = new Keydb(databaseUrl);

View File

@ -2,9 +2,7 @@ import { Policy } from '../types.ts';
/** Reject events whose content matches the regex. */ /** Reject events whose content matches the regex. */
const regexPolicy: Policy<RegExp> = ({ event: { id, content } }, regex) => { const regexPolicy: Policy<RegExp> = ({ event: { id, content } }, regex) => {
const isMatch = regex ? regex.test(content) : false; if (regex?.test(content)) {
if (isMatch) {
return { return {
id, id,
action: 'reject', action: 'reject',