fix: add 'pure' option in EventsDB

if pure is true, EventsDB will return a Nostr event, otherwise it will return a Ditto event
This commit is contained in:
P. Reis 2024-10-08 14:17:27 -03:00
parent df27959d35
commit d4a8ec21fe
3 changed files with 28 additions and 16 deletions

View file

@ -7,7 +7,7 @@ import { Conf } from '@/config.ts';
import { createTestDB } from '@/test.ts'; import { createTestDB } from '@/test.ts';
Deno.test('count filters', async () => { Deno.test('count filters', async () => {
await using db = await createTestDB(); await using db = await createTestDB({ pure: true });
const { store } = db; const { store } = db;
const event1 = await eventFixture('event-1'); const event1 = await eventFixture('event-1');
@ -18,7 +18,7 @@ Deno.test('count filters', async () => {
}); });
Deno.test('insert and filter events', async () => { Deno.test('insert and filter events', async () => {
await using db = await createTestDB(); await using db = await createTestDB({ pure: true });
const { store } = db; const { store } = db;
const event1 = await eventFixture('event-1'); 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 () => { 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 { store, kysely } = db;
const event1 = await eventFixture('event-1'); 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 () => { 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 { store, kysely } = db;
const en = genEvent({ kind: 1, content: 'hello world!' }); 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 () => { Deno.test('delete events', async () => {
await using db = await createTestDB(); await using db = await createTestDB({ pure: true });
const { store } = db; const { store } = db;
const sk = generateSecretKey(); const sk = generateSecretKey();
@ -96,7 +96,7 @@ Deno.test('delete events', async () => {
}); });
Deno.test("user cannot delete another user's event", 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 { store } = db;
const event = genEvent({ kind: 1, content: 'hello world', created_at: 1 }); 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 () => { Deno.test('admin can delete any event', async () => {
await using db = await createTestDB(); await using db = await createTestDB({ pure: true });
const { store } = db; const { store } = db;
const sk = generateSecretKey(); 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 () => { 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 { store } = db;
const event = genEvent(); 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 () => { 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 { store } = db;
const sk = generateSecretKey(); 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 () => { Deno.test('inserting replaceable events', async () => {
await using db = await createTestDB(); await using db = await createTestDB({ pure: true });
const { store } = db; const { store } = db;
const sk = generateSecretKey(); 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 () => { 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; const { store } = db;
await assertRejects( 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 () => { 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; const { store } = db;
await assertRejects( 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 () => { 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; const { store } = db;
await assertRejects( await assertRejects(

View file

@ -30,6 +30,8 @@ interface EventsDBOpts {
pubkey: string; pubkey: string;
/** Timeout in milliseconds for database queries. */ /** Timeout in milliseconds for database queries. */
timeout: number; 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. */ /** SQL database storage adapter for Nostr events. */
@ -203,7 +205,7 @@ class EventsDB extends NPostgres {
/** Parse an event row from the database. */ /** Parse an event row from the database. */
protected override parseEventRow(row: DittoTables['nostr_events']): DittoEvent { protected override parseEventRow(row: DittoTables['nostr_events']): DittoEvent {
return { const event: DittoEvent = {
id: row.id, id: row.id,
kind: row.kind, kind: row.kind,
pubkey: row.pubkey, pubkey: row.pubkey,
@ -211,8 +213,17 @@ class EventsDB extends NPostgres {
created_at: Number(row.created_at), created_at: Number(row.created_at),
tags: row.tags, tags: row.tags,
sig: row.sig, 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. */ /** Delete events based on filters from the database. */

View file

@ -35,7 +35,7 @@ export function genEvent(t: Partial<NostrEvent> = {}, sk: Uint8Array = generateS
} }
/** Create a database for testing. It uses `TEST_DATABASE_URL`, or creates an in-memory database by default. */ /** 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 { testDatabaseUrl } = Conf;
const { kysely } = DittoDB.create(testDatabaseUrl, { poolSize: 1 }); const { kysely } = DittoDB.create(testDatabaseUrl, { poolSize: 1 });
@ -45,6 +45,7 @@ export async function createTestDB() {
kysely, kysely,
timeout: Conf.db.timeouts.default, timeout: Conf.db.timeouts.default,
pubkey: Conf.pubkey, pubkey: Conf.pubkey,
pure: opts?.pure ?? false,
}); });
return { return {