mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Make @ditto/policies its own package
This commit is contained in:
parent
c029520937
commit
6f9081bbaf
6 changed files with 37 additions and 28 deletions
|
|
@ -7,6 +7,7 @@
|
||||||
"./packages/ditto",
|
"./packages/ditto",
|
||||||
"./packages/lang",
|
"./packages/lang",
|
||||||
"./packages/metrics",
|
"./packages/metrics",
|
||||||
|
"./packages/policies",
|
||||||
"./packages/translators",
|
"./packages/translators",
|
||||||
"./packages/uploaders"
|
"./packages/uploaders"
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { MuteListPolicy } from '@ditto/policies';
|
||||||
import {
|
import {
|
||||||
streamingClientMessagesCounter,
|
streamingClientMessagesCounter,
|
||||||
streamingConnectionsGauge,
|
streamingConnectionsGauge,
|
||||||
|
|
@ -9,7 +10,6 @@ import { logi } from '@soapbox/logi';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { type AppController } from '@/app.ts';
|
import { type AppController } from '@/app.ts';
|
||||||
import { MuteListPolicy } from '@/policies/MuteListPolicy.ts';
|
|
||||||
import { getFeedPubkeys } from '@/queries.ts';
|
import { getFeedPubkeys } from '@/queries.ts';
|
||||||
import { hydrateEvents } from '@/storages/hydrate.ts';
|
import { hydrateEvents } from '@/storages/hydrate.ts';
|
||||||
import { Storages } from '@/storages.ts';
|
import { Storages } from '@/storages.ts';
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { MockRelay } from '@nostrify/nostrify/test';
|
import { MockRelay } from '@nostrify/nostrify/test';
|
||||||
|
|
||||||
import { assertEquals } from '@std/assert';
|
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 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' };
|
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 blockEventCopy = structuredClone(blockEvent);
|
||||||
const event1authorUserMeCopy = structuredClone(event1authorUserMe);
|
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);
|
await relay.event(blockEventCopy);
|
||||||
const policy = new MuteListPolicy(userBlack.pubkey, db);
|
await relay.event(userBlackCopy);
|
||||||
|
await relay.event(userMeCopy);
|
||||||
await store.event(blockEventCopy);
|
|
||||||
await store.event(userBlackCopy);
|
|
||||||
await store.event(userMeCopy);
|
|
||||||
|
|
||||||
const ok = await policy.call(event1authorUserMeCopy);
|
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 userMeCopy = structuredClone(userMe);
|
||||||
const event1authorUserMeCopy = structuredClone(event1authorUserMe);
|
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);
|
await relay.event(userBlackCopy);
|
||||||
const policy = new MuteListPolicy(userBlack.pubkey, db);
|
await relay.event(userMeCopy);
|
||||||
|
|
||||||
await store.event(userBlackCopy);
|
|
||||||
await store.event(userMeCopy);
|
|
||||||
|
|
||||||
const ok = await policy.call(event1authorUserMeCopy);
|
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 blockEventCopy = structuredClone(blockEvent);
|
||||||
const event1copy = structuredClone(event1);
|
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, relay);
|
||||||
const policy = new MuteListPolicy(userBlack.pubkey, db);
|
|
||||||
|
|
||||||
await store.event(userBlackCopy);
|
await relay.event(userBlackCopy);
|
||||||
await store.event(blockEventCopy);
|
await relay.event(blockEventCopy);
|
||||||
await store.event(userMeCopy);
|
await relay.event(userMeCopy);
|
||||||
await store.event(event1copy);
|
await relay.event(event1copy);
|
||||||
await store.event(event1authorUserMeCopy);
|
await relay.event(event1authorUserMeCopy);
|
||||||
|
|
||||||
const ok = await policy.call(event1copy);
|
const ok = await policy.call(event1copy);
|
||||||
|
|
||||||
|
|
@ -1,13 +1,18 @@
|
||||||
import { NostrEvent, NostrRelayOK, NPolicy, NStore } from '@nostrify/nostrify';
|
import type { NostrEvent, NostrRelayOK, NPolicy, NStore } from '@nostrify/nostrify';
|
||||||
|
|
||||||
import { getTagSet } from '@/utils/tags.ts';
|
|
||||||
|
|
||||||
export class MuteListPolicy implements NPolicy {
|
export class MuteListPolicy implements NPolicy {
|
||||||
constructor(private pubkey: string, private store: NStore) {}
|
constructor(private pubkey: string, private store: NStore) {}
|
||||||
|
|
||||||
async call(event: NostrEvent): Promise<NostrRelayOK> {
|
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 [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)) {
|
if (pubkeys.has(event.pubkey)) {
|
||||||
return ['OK', event.id, false, 'blocked: Your account has been deactivated.'];
|
return ['OK', event.id, false, 'blocked: Your account has been deactivated.'];
|
||||||
7
packages/policies/deno.json
Normal file
7
packages/policies/deno.json
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"name": "@ditto/policies",
|
||||||
|
"version": "1.1.0",
|
||||||
|
"exports": {
|
||||||
|
".": "./mod.ts"
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/policies/mod.ts
Normal file
1
packages/policies/mod.ts
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
export { MuteListPolicy } from './MuteListPolicy.ts';
|
||||||
Loading…
Add table
Reference in a new issue