From 358396fdac9c7ce71b5ef7a06f6630baa34898f1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 29 Dec 2023 17:01:23 -0600 Subject: [PATCH] Drop users table --- scripts/db.ts | 12 +++++++++++- src/controllers/api/accounts.ts | 2 +- src/db.ts | 10 +--------- src/db/events.ts | 1 + src/db/migrations/010_drop_users.ts | 8 ++++++++ src/db/users.ts | 10 ++++------ src/views/mastodon/accounts.ts | 2 +- src/views/mastodon/statuses.ts | 3 ++- 8 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 src/db/migrations/010_drop_users.ts diff --git a/scripts/db.ts b/scripts/db.ts index f1b7989..cdc5174 100644 --- a/scripts/db.ts +++ b/scripts/db.ts @@ -1,8 +1,18 @@ import { Conf } from '@/config.ts'; import { db } from '@/db.ts'; import { eventsDB } from '@/db/events.ts'; +import { type Kysely } from '@/deps.ts'; import { signAdminEvent } from '@/sign.ts'; +interface DB { + users: { + pubkey: string; + username: string; + inserted_at: Date; + admin: 0 | 1; + }; +} + switch (Deno.args[0]) { case 'users-to-events': await usersToEvents(); @@ -14,7 +24,7 @@ switch (Deno.args[0]) { async function usersToEvents() { const { origin, host } = Conf.url; - for (const row of await db.selectFrom('users').selectAll().execute()) { + for (const row of await (db as unknown as Kysely).selectFrom('users').selectAll().execute()) { const event = await signAdminEvent({ kind: 30361, tags: [ diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index ddd8170..5e345ae 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -42,7 +42,7 @@ const createAccountController: AppController = async (c) => { pubkey, username: result.data.username, inserted_at: new Date(), - admin: 0, + admin: false, }); return c.json({ diff --git a/src/db.ts b/src/db.ts index 25d6d78..253fed9 100644 --- a/src/db.ts +++ b/src/db.ts @@ -10,7 +10,6 @@ interface DittoDB { events: EventRow; events_fts: EventFTSRow; tags: TagRow; - users: UserRow; relays: RelayRow; unattached_media: UnattachedMediaRow; author_stats: AuthorStatsRow; @@ -52,13 +51,6 @@ interface TagRow { event_id: string; } -interface UserRow { - pubkey: string; - username: string; - inserted_at: Date; - admin: 0 | 1; -} - interface RelayRow { url: string; domain: string; @@ -120,4 +112,4 @@ async function migrate() { await migrate(); -export { type AuthorStatsRow, db, type DittoDB, type EventRow, type EventStatsRow, type TagRow, type UserRow }; +export { type AuthorStatsRow, db, type DittoDB, type EventRow, type EventStatsRow, type TagRow }; diff --git a/src/db/events.ts b/src/db/events.ts index 2d18af2..0c03157 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -154,6 +154,7 @@ function getFilterQuery(filter: DittoFilter): EventQuery { } } + // FIXME: local filtering is broken. if (typeof filter.local === 'boolean') { query = filter.local ? query.innerJoin('users', 'users.pubkey', 'events.pubkey') as typeof query diff --git a/src/db/migrations/010_drop_users.ts b/src/db/migrations/010_drop_users.ts new file mode 100644 index 0000000..9649b64 --- /dev/null +++ b/src/db/migrations/010_drop_users.ts @@ -0,0 +1,8 @@ +import { Kysely } from '@/deps.ts'; + +export async function up(db: Kysely): Promise { + await db.schema.dropTable('users').execute(); +} + +export async function down(_db: Kysely): Promise { +} diff --git a/src/db/users.ts b/src/db/users.ts index 8f0a78a..195dfe2 100644 --- a/src/db/users.ts +++ b/src/db/users.ts @@ -1,10 +1,8 @@ import { Conf } from '@/config.ts'; -import { Debug, type Filter, type Insertable } from '@/deps.ts'; -import { type UserRow } from '@/db.ts'; +import { Debug, type Filter } from '@/deps.ts'; import { eventsDB } from '@/db/events.ts'; import * as pipeline from '@/pipeline.ts'; import { signAdminEvent } from '@/sign.ts'; -import { nostrNow } from '@/utils.ts'; const debug = Debug('ditto:users'); @@ -16,7 +14,7 @@ interface User { } /** Adds a user to the database. */ -async function insertUser(user: Insertable) { +async function insertUser(user: User) { debug('insertUser', JSON.stringify(user)); const { origin, host } = Conf.url; @@ -31,7 +29,7 @@ async function insertUser(user: Insertable) { ['alt', `@${user.username}@${host}'s account was updated by the admins of ${host}`], ], content: '', - created_at: nostrNow(), + created_at: Math.floor(user.inserted_at.getTime() / 1000), }); return pipeline.handleEvent(event); @@ -44,7 +42,7 @@ async function insertUser(user: Insertable) { * await findUser({ username: 'alex' }); * ``` */ -async function findUser(user: Partial>): Promise { +async function findUser(user: Partial): Promise { const filter: Filter = { kinds: [30361], authors: [Conf.pubkey], limit: 1 }; for (const [key, value] of Object.entries(user)) { diff --git a/src/views/mastodon/accounts.ts b/src/views/mastodon/accounts.ts index bfcd7ef..eae3dd7 100644 --- a/src/views/mastodon/accounts.ts +++ b/src/views/mastodon/accounts.ts @@ -1,9 +1,9 @@ import { Conf } from '@/config.ts'; -import { type DittoEvent } from '@/db/events.ts'; import { findUser } from '@/db/users.ts'; import { lodash, nip19, type UnsignedEvent } from '@/deps.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; import { verifyNip05Cached } from '@/utils/nip05.ts'; +import { type DittoEvent } from '@/store.ts'; import { Nip05, nostrDate, nostrNow, parseNip05 } from '@/utils.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts'; diff --git a/src/views/mastodon/statuses.ts b/src/views/mastodon/statuses.ts index 2363ef9..b49be49 100644 --- a/src/views/mastodon/statuses.ts +++ b/src/views/mastodon/statuses.ts @@ -6,13 +6,14 @@ import { findReplyTag, nip19 } from '@/deps.ts'; import { getMediaLinks, parseNoteContent } from '@/note.ts'; import { getAuthor } from '@/queries.ts'; import { jsonMediaDataSchema } from '@/schemas/nostr.ts'; +import { DittoEvent } from '@/store.ts'; import { nostrDate } from '@/utils.ts'; import { unfurlCardCached } from '@/utils/unfurl.ts'; import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { DittoAttachment, renderAttachment } from '@/views/mastodon/attachments.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts'; -async function renderStatus(event: eventsDB.DittoEvent<1>, viewerPubkey?: string) { +async function renderStatus(event: DittoEvent<1>, viewerPubkey?: string) { const account = event.author ? await renderAccount({ ...event.author, author_stats: event.author_stats }) : await accountFromPubkey(event.pubkey);