From a10e810068e1fad39e38cec3df81a7664eedd510 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 2 Oct 2024 13:13:44 -0500 Subject: [PATCH] Create push_subscriptions table --- src/db/DittoTables.ts | 12 ++++++------ src/db/migrations/023_add_nip46_tokens.ts | 2 +- src/db/migrations/038_push_subscriptions.ts | 20 ++++++++++++++++++++ src/pipeline.ts | 2 ++ 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/db/migrations/038_push_subscriptions.ts diff --git a/src/db/DittoTables.ts b/src/db/DittoTables.ts index 26b2e6fa..b4950afa 100644 --- a/src/db/DittoTables.ts +++ b/src/db/DittoTables.ts @@ -1,4 +1,4 @@ -import { Nullable } from 'kysely'; +import { Generated, Nullable } from 'kysely'; import { NPostgresSchema } from '@nostrify/db'; @@ -58,11 +58,11 @@ interface EventZapRow { } interface PushSubscriptionRow { - id: bigint; + id: Generated; pubkey: string; endpoint: string; - key_p256dh: string; - key_auth: string; + p256dh: string; + auth: string; data: { alerts?: { mention?: boolean; @@ -78,6 +78,6 @@ interface PushSubscriptionRow { }; policy?: 'all' | 'followed' | 'follower' | 'none'; } | null; - created_at: Date; - updated_at: Date; + created_at: Generated; + updated_at: Generated; } diff --git a/src/db/migrations/023_add_nip46_tokens.ts b/src/db/migrations/023_add_nip46_tokens.ts index 144bd1ec..01d71640 100644 --- a/src/db/migrations/023_add_nip46_tokens.ts +++ b/src/db/migrations/023_add_nip46_tokens.ts @@ -3,7 +3,7 @@ import { Kysely, sql } from 'kysely'; export async function up(db: Kysely): Promise { await db.schema .createTable('nip46_tokens') - .addColumn('api_token', 'text', (col) => col.primaryKey().unique().notNull()) + .addColumn('api_token', 'text', (col) => col.primaryKey().notNull()) .addColumn('user_pubkey', 'text', (col) => col.notNull()) .addColumn('server_seckey', 'bytea', (col) => col.notNull()) .addColumn('server_pubkey', 'text', (col) => col.notNull()) diff --git a/src/db/migrations/038_push_subscriptions.ts b/src/db/migrations/038_push_subscriptions.ts new file mode 100644 index 00000000..5e80ddc6 --- /dev/null +++ b/src/db/migrations/038_push_subscriptions.ts @@ -0,0 +1,20 @@ +import { Kysely, sql } from 'kysely'; + +export async function up(db: Kysely): Promise { + await db.schema + .createTable('push_subscriptions') + .addColumn('id', 'bigint', (c) => c.primaryKey().autoIncrement()) + .addColumn('pubkey', 'char(64)', (c) => c.notNull()) + .addColumn('token', 'char(64)', (c) => c.notNull()) + .addColumn('endpoint', 'text', (c) => c.notNull()) + .addColumn('p256dh', 'text', (c) => c.notNull()) + .addColumn('auth', 'text', (c) => c.notNull()) + .addColumn('data', 'jsonb') + .addColumn('created_at', 'timestamp', (c) => c.notNull().defaultTo(sql`CURRENT_TIMESTAMP`)) + .addColumn('updated_at', 'timestamp', (c) => c.notNull().defaultTo(sql`CURRENT_TIMESTAMP`)) + .execute(); +} + +export async function down(db: Kysely): Promise { + await db.schema.dropTable('push_subscriptions').execute(); +} diff --git a/src/pipeline.ts b/src/pipeline.ts index a00456a9..f854209b 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -230,6 +230,8 @@ async function streamOut(event: NostrEvent): Promise { if (isFresh(event)) { const pubsub = await Storages.pubsub(); await pubsub.event(event); + + // TODO: Web Push } }