From 1693b02ffdee13c4bde9079cd86d12fdd6446912 Mon Sep 17 00:00:00 2001 From: Brian Lee Date: Wed, 20 Sep 2023 11:28:29 -0700 Subject: [PATCH] Merge a simple event kind policy. --- mod.ts | 1 + src/policies/kind-policy.test.ts | 13 +++++++++++++ src/policies/kind-policy.ts | 28 ++++++++++++++++++++++++++++ src/types.ts | 5 ++++- 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/policies/kind-policy.test.ts create mode 100644 src/policies/kind-policy.ts diff --git a/mod.ts b/mod.ts index ba3098a..8d705a2 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,6 @@ 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 kindPolicy } from './src/policies/kind-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 noopPolicy } from './src/policies/noop-policy.ts'; diff --git a/src/policies/kind-policy.test.ts b/src/policies/kind-policy.test.ts new file mode 100644 index 0000000..163cdfe --- /dev/null +++ b/src/policies/kind-policy.test.ts @@ -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'); +}); diff --git a/src/policies/kind-policy.ts b/src/policies/kind-policy.ts new file mode 100644 index 0000000..37e939d --- /dev/null +++ b/src/policies/kind-policy.ts @@ -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 = ({ 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; \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 0b1ff5c..7621073 100644 --- a/src/types.ts +++ b/src/types.ts @@ -54,4 +54,7 @@ type Policy = (msg: InputMessage, opts?: Opts) => Promise | AsyncIterable; -export type { Event, InputMessage, IterablePubkeys, OutputMessage, Policy }; +/** Syncronous iterable of event kinds. */ +type KindList = number[]; + +export type { Event, InputMessage, IterablePubkeys, KindList, OutputMessage, Policy };