Merge a simple event kind policy.

This commit is contained in:
Brian Lee 2023-09-20 11:28:29 -07:00
parent 433459d808
commit 1693b02ffd
4 changed files with 46 additions and 1 deletions

1
mod.ts
View File

@ -1,5 +1,6 @@
export { type AntiDuplication, default as antiDuplicationPolicy } from './src/policies/anti-duplication-policy.ts'; export { type AntiDuplication, default as antiDuplicationPolicy } from './src/policies/anti-duplication-policy.ts';
export { default as filterPolicy, type Filter } from './src/policies/filter-policy.ts'; export { default as filterPolicy, type Filter } from './src/policies/filter-policy.ts';
export { default as kindPolicy } from './src/policies/kind-policy.ts';
export { default as hellthreadPolicy, type Hellthread } from './src/policies/hellthread-policy.ts'; export { default as hellthreadPolicy, type Hellthread } from './src/policies/hellthread-policy.ts';
export { default as keywordPolicy } from './src/policies/keyword-policy.ts'; export { default as keywordPolicy } from './src/policies/keyword-policy.ts';
export { default as noopPolicy } from './src/policies/noop-policy.ts'; export { default as noopPolicy } from './src/policies/noop-policy.ts';

View File

@ -0,0 +1,13 @@
import { assertEquals } from '../deps.ts';
import { buildEvent, buildInputMessage } from '../test.ts';
import kindPolicy from './kind-policy.ts';
Deno.test('rejects events by kind', async () => {
const msgA = buildInputMessage({ event: buildEvent({ kind: 1 }) });
const msgB = buildInputMessage({ event: buildEvent({ kind: 7 }) });
assertEquals((await kindPolicy(msgA, [7])).action, 'accept');
assertEquals((await kindPolicy(msgA, [7])).action, 'accept');
assertEquals((await kindPolicy(msgB, [7])).action, 'reject');
});

View File

@ -0,0 +1,28 @@
import { KindList, Policy } from '../types.ts';
/**
* Reject events by kind.
*
* @example
* ```ts
* // Block DMs, likes, and channel messages.
* kindPolicy({ kinds: [4, 7, 42] });
* ```
*/
const kindPolicy: Policy<KindList> = ({ event: { id, kind } }, kinds = []) => {
if (kinds.includes(kind)) {
return {
id,
action: 'reject',
msg: 'blocked: this event kind is not currently permitted',
};
}
return {
id,
action: 'accept',
msg: '',
};
};
export default kindPolicy;

View File

@ -54,4 +54,7 @@ type Policy<Opts = unknown> = (msg: InputMessage, opts?: Opts) => Promise<Output
/** Sync or async iterable of pubkeys, designed for efficiently loading from large files. */ /** Sync or async iterable of pubkeys, designed for efficiently loading from large files. */
type IterablePubkeys = Iterable<string> | AsyncIterable<string>; type IterablePubkeys = Iterable<string> | AsyncIterable<string>;
export type { Event, InputMessage, IterablePubkeys, OutputMessage, Policy }; /** Syncronous iterable of event kinds. */
type KindList = number[];
export type { Event, InputMessage, IterablePubkeys, KindList, OutputMessage, Policy };