Merge branch 'ditto-policies' into 'main'

Make @ditto/policies its own package

See merge request soapbox-pub/ditto!674
This commit is contained in:
Alex Gleason 2025-02-18 19:14:38 +00:00
commit fc2928e72e
6 changed files with 37 additions and 28 deletions

View file

@ -7,6 +7,7 @@
"./packages/ditto",
"./packages/lang",
"./packages/metrics",
"./packages/policies",
"./packages/translators",
"./packages/uploaders"
],

View file

@ -1,3 +1,4 @@
import { MuteListPolicy } from '@ditto/policies';
import {
streamingClientMessagesCounter,
streamingConnectionsGauge,
@ -9,7 +10,6 @@ import { logi } from '@soapbox/logi';
import { z } from 'zod';
import { type AppController } from '@/app.ts';
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
import { getFeedPubkeys } from '@/queries.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { Storages } from '@/storages.ts';

View file

@ -1,8 +1,8 @@
import { MockRelay } from '@nostrify/nostrify/test';
import { assertEquals } from '@std/assert';
import { UserStore } from '@/storages/UserStore.ts';
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
import { MuteListPolicy } from './MuteListPolicy.ts';
import userBlack from '~/fixtures/events/kind-0-black.json' with { type: 'json' };
import userMe from '~/fixtures/events/event-0-makes-repost-with-quote-repost.json' with { type: 'json' };
@ -16,14 +16,12 @@ Deno.test('block event: muted user cannot post', async () => {
const blockEventCopy = structuredClone(blockEvent);
const event1authorUserMeCopy = structuredClone(event1authorUserMe);
const db = new MockRelay();
const relay = new MockRelay();
const policy = new MuteListPolicy(userBlack.pubkey, relay);
const store = new UserStore(userBlackCopy.pubkey, db);
const policy = new MuteListPolicy(userBlack.pubkey, db);
await store.event(blockEventCopy);
await store.event(userBlackCopy);
await store.event(userMeCopy);
await relay.event(blockEventCopy);
await relay.event(userBlackCopy);
await relay.event(userMeCopy);
const ok = await policy.call(event1authorUserMeCopy);
@ -35,13 +33,11 @@ Deno.test('allow event: user is NOT muted because there is no muted event', asyn
const userMeCopy = structuredClone(userMe);
const event1authorUserMeCopy = structuredClone(event1authorUserMe);
const db = new MockRelay();
const relay = new MockRelay();
const policy = new MuteListPolicy(userBlack.pubkey, relay);
const store = new UserStore(userBlackCopy.pubkey, db);
const policy = new MuteListPolicy(userBlack.pubkey, db);
await store.event(userBlackCopy);
await store.event(userMeCopy);
await relay.event(userBlackCopy);
await relay.event(userMeCopy);
const ok = await policy.call(event1authorUserMeCopy);
@ -55,16 +51,15 @@ Deno.test('allow event: user is NOT muted because he is not in mute event', asyn
const blockEventCopy = structuredClone(blockEvent);
const event1copy = structuredClone(event1);
const db = new MockRelay();
const relay = new MockRelay();
const store = new UserStore(userBlackCopy.pubkey, db);
const policy = new MuteListPolicy(userBlack.pubkey, db);
const policy = new MuteListPolicy(userBlack.pubkey, relay);
await store.event(userBlackCopy);
await store.event(blockEventCopy);
await store.event(userMeCopy);
await store.event(event1copy);
await store.event(event1authorUserMeCopy);
await relay.event(userBlackCopy);
await relay.event(blockEventCopy);
await relay.event(userMeCopy);
await relay.event(event1copy);
await relay.event(event1authorUserMeCopy);
const ok = await policy.call(event1copy);

View file

@ -1,13 +1,18 @@
import { NostrEvent, NostrRelayOK, NPolicy, NStore } from '@nostrify/nostrify';
import { getTagSet } from '@/utils/tags.ts';
import type { NostrEvent, NostrRelayOK, NPolicy, NStore } from '@nostrify/nostrify';
export class MuteListPolicy implements NPolicy {
constructor(private pubkey: string, private store: NStore) {}
async call(event: NostrEvent): Promise<NostrRelayOK> {
const pubkeys = new Set<string>();
const [muteList] = await this.store.query([{ authors: [this.pubkey], kinds: [10000], limit: 1 }]);
const pubkeys = getTagSet(muteList?.tags ?? [], 'p');
for (const [name, value] of muteList?.tags ?? []) {
if (name === 'p') {
pubkeys.add(value);
}
}
if (pubkeys.has(event.pubkey)) {
return ['OK', event.id, false, 'blocked: Your account has been deactivated.'];

View file

@ -0,0 +1,7 @@
{
"name": "@ditto/policies",
"version": "1.1.0",
"exports": {
".": "./mod.ts"
}
}

1
packages/policies/mod.ts Normal file
View file

@ -0,0 +1 @@
export { MuteListPolicy } from './MuteListPolicy.ts';