From d4a8ec21fef73bdf600ee56cd17f8bd904381e65 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 8 Oct 2024 14:17:27 -0300 Subject: [PATCH] fix: add 'pure' option in EventsDB if pure is true, EventsDB will return a Nostr event, otherwise it will return a Ditto event --- src/storages/EventsDB.test.ts | 26 +++++++++++++------------- src/storages/EventsDB.ts | 15 +++++++++++++-- src/test.ts | 3 ++- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/storages/EventsDB.test.ts b/src/storages/EventsDB.test.ts index b24032aa..e19fe775 100644 --- a/src/storages/EventsDB.test.ts +++ b/src/storages/EventsDB.test.ts @@ -7,7 +7,7 @@ import { Conf } from '@/config.ts'; import { createTestDB } from '@/test.ts'; Deno.test('count filters', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; const event1 = await eventFixture('event-1'); @@ -18,7 +18,7 @@ Deno.test('count filters', async () => { }); Deno.test('insert and filter events', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; const event1 = await eventFixture('event-1'); @@ -35,7 +35,7 @@ Deno.test('insert and filter events', async () => { }); Deno.test('query events with domain search filter', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store, kysely } = db; const event1 = await eventFixture('event-1'); @@ -55,7 +55,7 @@ Deno.test('query events with domain search filter', async () => { }); Deno.test('query events with language search filter', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store, kysely } = db; const en = genEvent({ kind: 1, content: 'hello world!' }); @@ -72,7 +72,7 @@ Deno.test('query events with language search filter', async () => { }); Deno.test('delete events', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; const sk = generateSecretKey(); @@ -96,7 +96,7 @@ Deno.test('delete events', async () => { }); Deno.test("user cannot delete another user's event", async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; const event = genEvent({ kind: 1, content: 'hello world', created_at: 1 }); @@ -113,7 +113,7 @@ Deno.test("user cannot delete another user's event", async () => { }); Deno.test('admin can delete any event', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; const sk = generateSecretKey(); @@ -137,7 +137,7 @@ Deno.test('admin can delete any event', async () => { }); Deno.test('throws a RelayError when inserting an event deleted by the admin', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; const event = genEvent(); @@ -154,7 +154,7 @@ Deno.test('throws a RelayError when inserting an event deleted by the admin', as }); Deno.test('throws a RelayError when inserting an event deleted by a user', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; const sk = generateSecretKey(); @@ -173,7 +173,7 @@ Deno.test('throws a RelayError when inserting an event deleted by a user', async }); Deno.test('inserting replaceable events', async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; const sk = generateSecretKey(); @@ -190,7 +190,7 @@ Deno.test('inserting replaceable events', async () => { }); Deno.test("throws a RelayError when querying an event with a large 'since'", async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; await assertRejects( @@ -201,7 +201,7 @@ Deno.test("throws a RelayError when querying an event with a large 'since'", asy }); Deno.test("throws a RelayError when querying an event with a large 'until'", async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; await assertRejects( @@ -212,7 +212,7 @@ Deno.test("throws a RelayError when querying an event with a large 'until'", asy }); Deno.test("throws a RelayError when querying an event with a large 'kind'", async () => { - await using db = await createTestDB(); + await using db = await createTestDB({ pure: true }); const { store } = db; await assertRejects( diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index 905883b7..b303dad0 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -30,6 +30,8 @@ interface EventsDBOpts { pubkey: string; /** Timeout in milliseconds for database queries. */ timeout: number; + /** Whether the event returned should be a Nostr event or a Ditto event. Defaults to false. */ + pure?: boolean; } /** SQL database storage adapter for Nostr events. */ @@ -203,7 +205,7 @@ class EventsDB extends NPostgres { /** Parse an event row from the database. */ protected override parseEventRow(row: DittoTables['nostr_events']): DittoEvent { - return { + const event: DittoEvent = { id: row.id, kind: row.kind, pubkey: row.pubkey, @@ -211,8 +213,17 @@ class EventsDB extends NPostgres { created_at: Number(row.created_at), tags: row.tags, sig: row.sig, - language: (row.language || undefined) as LanguageCode, }; + + if (this.opts.pure) { + return event; + } + + if (row.language) { + event.language = row.language as LanguageCode; + } + + return event; } /** Delete events based on filters from the database. */ diff --git a/src/test.ts b/src/test.ts index c8fcfe6b..3f2d1c38 100644 --- a/src/test.ts +++ b/src/test.ts @@ -35,7 +35,7 @@ export function genEvent(t: Partial = {}, sk: Uint8Array = generateS } /** Create a database for testing. It uses `TEST_DATABASE_URL`, or creates an in-memory database by default. */ -export async function createTestDB() { +export async function createTestDB(opts?: { pure?: boolean }) { const { testDatabaseUrl } = Conf; const { kysely } = DittoDB.create(testDatabaseUrl, { poolSize: 1 }); @@ -45,6 +45,7 @@ export async function createTestDB() { kysely, timeout: Conf.db.timeouts.default, pubkey: Conf.pubkey, + pure: opts?.pure ?? false, }); return {