Merge a simple event kind policy.
This commit is contained in:
parent
433459d808
commit
1693b02ffd
1
mod.ts
1
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';
|
||||
|
13
src/policies/kind-policy.test.ts
Normal file
13
src/policies/kind-policy.test.ts
Normal 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');
|
||||
});
|
28
src/policies/kind-policy.ts
Normal file
28
src/policies/kind-policy.ts
Normal 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;
|
@ -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. */
|
||||
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 };
|
||||
|
Loading…
Reference in New Issue
Block a user