From b3e4b794391c50f789e6ba2685611eb30d193928 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 3 Jan 2024 21:55:53 -0600 Subject: [PATCH] Delete db/events.ts, db/memorelay.ts, move to @/storages.ts --- scripts/db.ts | 2 +- src/controllers/api/accounts.ts | 2 +- src/controllers/api/blocks.ts | 2 +- src/controllers/api/bookmarks.ts | 2 +- src/controllers/api/notifications.ts | 2 +- src/controllers/api/pleroma.ts | 2 +- src/controllers/api/search.ts | 2 +- src/controllers/api/timelines.ts | 2 +- src/controllers/nostr/relay.ts | 2 +- src/db/events.ts | 6 ------ src/db/memorelay.ts | 10 ---------- src/db/users.ts | 2 +- src/pipeline.ts | 3 +-- src/queries.ts | 3 +-- src/stats.ts | 2 +- src/storages.ts | 11 +++++++++++ src/utils/api.ts | 2 +- src/views.ts | 2 +- src/views/mastodon/relationships.ts | 2 +- src/views/mastodon/statuses.ts | 4 ++-- 20 files changed, 29 insertions(+), 36 deletions(-) delete mode 100644 src/db/events.ts delete mode 100644 src/db/memorelay.ts create mode 100644 src/storages.ts diff --git a/scripts/db.ts b/scripts/db.ts index cdc5174..cbaca91 100644 --- a/scripts/db.ts +++ b/scripts/db.ts @@ -1,8 +1,8 @@ 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'; +import { eventsDB } from '@/storages.ts'; interface DB { users: { diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 8316ede..34518cd 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -1,12 +1,12 @@ import { type AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; -import { eventsDB } from '@/db/events.ts'; import { insertUser } from '@/db/users.ts'; import { findReplyTag, nip19, z } from '@/deps.ts'; import { type DittoFilter } from '@/filter.ts'; import { getAuthor, getFollowedPubkeys } from '@/queries.ts'; import { booleanParamSchema, fileSchema } from '@/schema.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; +import { eventsDB } from '@/storages.ts'; import { addTag, deleteTag, getTagSet } from '@/tags.ts'; import { uploadFile } from '@/upload.ts'; import { lookupAccount, nostrNow } from '@/utils.ts'; diff --git a/src/controllers/api/blocks.ts b/src/controllers/api/blocks.ts index 2ff4f3e..b45e1c6 100644 --- a/src/controllers/api/blocks.ts +++ b/src/controllers/api/blocks.ts @@ -1,5 +1,5 @@ import { type AppController } from '@/app.ts'; -import { eventsDB } from '@/db/events.ts'; +import { eventsDB } from '@/storages.ts'; import { getTagSet } from '@/tags.ts'; import { renderAccounts } from '@/views.ts'; diff --git a/src/controllers/api/bookmarks.ts b/src/controllers/api/bookmarks.ts index 78edff0..81cd011 100644 --- a/src/controllers/api/bookmarks.ts +++ b/src/controllers/api/bookmarks.ts @@ -1,5 +1,5 @@ import { type AppController } from '@/app.ts'; -import { eventsDB } from '@/db/events.ts'; +import { eventsDB } from '@/storages.ts'; import { getTagSet } from '@/tags.ts'; import { renderStatuses } from '@/views.ts'; diff --git a/src/controllers/api/notifications.ts b/src/controllers/api/notifications.ts index f50f891..43c974a 100644 --- a/src/controllers/api/notifications.ts +++ b/src/controllers/api/notifications.ts @@ -1,5 +1,5 @@ import { type AppController } from '@/app.ts'; -import { eventsDB } from '@/db/events.ts'; +import { eventsDB } from '@/storages.ts'; import { paginated, paginationSchema } from '@/utils/api.ts'; import { renderNotification } from '@/views/mastodon/notifications.ts'; diff --git a/src/controllers/api/pleroma.ts b/src/controllers/api/pleroma.ts index 3b84227..04370e4 100644 --- a/src/controllers/api/pleroma.ts +++ b/src/controllers/api/pleroma.ts @@ -1,7 +1,7 @@ import { type AppController } from '@/app.ts'; -import { eventsDB } from '@/db/events.ts'; import { z } from '@/deps.ts'; import { configSchema, elixirTupleSchema } from '@/schemas/pleroma-api.ts'; +import { eventsDB } from '@/storages.ts'; import { createAdminEvent } from '@/utils/api.ts'; import { Conf } from '@/config.ts'; diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts index a514d66..1f34b07 100644 --- a/src/controllers/api/search.ts +++ b/src/controllers/api/search.ts @@ -1,9 +1,9 @@ import { AppController } from '@/app.ts'; -import { eventsDB } from '@/db/events.ts'; import { type Event, nip19, z } from '@/deps.ts'; import { type DittoFilter } from '@/filter.ts'; import { booleanParamSchema } from '@/schema.ts'; import { nostrIdSchema } from '@/schemas/nostr.ts'; +import { eventsDB } from '@/storages.ts'; import { dedupeEvents } from '@/utils.ts'; import { lookupNip05Cached } from '@/utils/nip05.ts'; import { renderAccount } from '@/views/mastodon/accounts.ts'; diff --git a/src/controllers/api/timelines.ts b/src/controllers/api/timelines.ts index 6d93280..47496a0 100644 --- a/src/controllers/api/timelines.ts +++ b/src/controllers/api/timelines.ts @@ -1,4 +1,4 @@ -import { eventsDB } from '@/db/events.ts'; +import { eventsDB } from '@/storages.ts'; import { z } from '@/deps.ts'; import { type DittoFilter } from '@/filter.ts'; import { getFeedPubkeys } from '@/queries.ts'; diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index a1f4de0..f1c3511 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -1,5 +1,5 @@ import { relayInfoController } from '@/controllers/nostr/relay-info.ts'; -import { eventsDB } from '@/db/events.ts'; +import { eventsDB } from '@/storages.ts'; import * as pipeline from '@/pipeline.ts'; import { jsonSchema } from '@/schema.ts'; import { diff --git a/src/db/events.ts b/src/db/events.ts deleted file mode 100644 index 7b06cdc..0000000 --- a/src/db/events.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { db } from '@/db.ts'; -import { EventsDB } from '@/storages/events-db.ts'; - -const eventsDB = new EventsDB(db); - -export { eventsDB }; diff --git a/src/db/memorelay.ts b/src/db/memorelay.ts deleted file mode 100644 index 3550346..0000000 --- a/src/db/memorelay.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Memorelay } from '@/storages/memorelay.ts'; - -/** In-memory data store for events using microfilters. */ -const memorelay = new Memorelay({ - max: 3000, - maxEntrySize: 5000, - sizeCalculation: (event) => JSON.stringify(event).length, -}); - -export { memorelay }; diff --git a/src/db/users.ts b/src/db/users.ts index 6b7f9ef..62340e1 100644 --- a/src/db/users.ts +++ b/src/db/users.ts @@ -1,8 +1,8 @@ import { Conf } from '@/config.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 { eventsDB } from '@/storages.ts'; const debug = Debug('ditto:users'); diff --git a/src/pipeline.ts b/src/pipeline.ts index 69ae941..a237b72 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,7 +1,5 @@ import { client } from '@/client.ts'; import { Conf } from '@/config.ts'; -import { eventsDB } from '@/db/events.ts'; -import { memorelay } from '@/db/memorelay.ts'; import { addRelays } from '@/db/relays.ts'; import { deleteAttachedMedia } from '@/db/unattached-media.ts'; import { findUser } from '@/db/users.ts'; @@ -10,6 +8,7 @@ import { isEphemeralKind } from '@/kinds.ts'; import { isLocallyFollowed } from '@/queries.ts'; import { reqmeister } from '@/reqmeister.ts'; import { updateStats } from '@/stats.ts'; +import { eventsDB, memorelay } from '@/storages.ts'; import { Sub } from '@/subs.ts'; import { getTagSet } from '@/tags.ts'; import { type EventData } from '@/types.ts'; diff --git a/src/queries.ts b/src/queries.ts index cfeeb36..29c07a7 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -1,5 +1,4 @@ -import { eventsDB } from '@/db/events.ts'; -import { memorelay } from '@/db/memorelay.ts'; +import { eventsDB, memorelay } from '@/storages.ts'; import { Debug, type Event, findReplyTag } from '@/deps.ts'; import { type AuthorMicrofilter, type DittoFilter, type IdMicrofilter, type Relation } from '@/filter.ts'; import { reqmeister } from '@/reqmeister.ts'; diff --git a/src/stats.ts b/src/stats.ts index e09791f..fd08cf5 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -1,6 +1,6 @@ import { type AuthorStatsRow, db, type DittoDB, type EventStatsRow } from '@/db.ts'; -import { eventsDB } from '@/db/events.ts'; import { Debug, type Event, findReplyTag, type InsertQueryBuilder } from '@/deps.ts'; +import { eventsDB } from '@/storages.ts'; type AuthorStat = keyof Omit; type EventStat = keyof Omit; diff --git a/src/storages.ts b/src/storages.ts new file mode 100644 index 0000000..15aadc6 --- /dev/null +++ b/src/storages.ts @@ -0,0 +1,11 @@ +import { db } from '@/db.ts'; +import { EventsDB } from '@/storages/events-db.ts'; +import { Memorelay } from '@/storages/memorelay.ts'; + +/** SQLite database to store events this Ditto server cares about. */ +const eventsDB = new EventsDB(db); + +/** In-memory data store for cached events. */ +const memorelay = new Memorelay({ max: 3000 }); + +export { eventsDB, memorelay }; diff --git a/src/utils/api.ts b/src/utils/api.ts index 9a3eaf0..dbedc6c 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -13,8 +13,8 @@ import { } from '@/deps.ts'; import * as pipeline from '@/pipeline.ts'; import { signAdminEvent, signEvent } from '@/sign.ts'; +import { eventsDB } from '@/storages.ts'; import { nostrNow } from '@/utils.ts'; -import { eventsDB } from '@/db/events.ts'; const debug = Debug('ditto:api'); diff --git a/src/views.ts b/src/views.ts index c90f26d..9ade600 100644 --- a/src/views.ts +++ b/src/views.ts @@ -1,6 +1,6 @@ import { AppContext } from '@/app.ts'; -import { eventsDB } from '@/db/events.ts'; import { type Filter } from '@/deps.ts'; +import { eventsDB } from '@/storages.ts'; import { renderAccount } from '@/views/mastodon/accounts.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; import { paginated, paginationSchema } from '@/utils/api.ts'; diff --git a/src/views/mastodon/relationships.ts b/src/views/mastodon/relationships.ts index ca2778b..43ed7b8 100644 --- a/src/views/mastodon/relationships.ts +++ b/src/views/mastodon/relationships.ts @@ -1,4 +1,4 @@ -import { eventsDB } from '@/db/events.ts'; +import { eventsDB } from '@/storages.ts'; import { hasTag } from '@/tags.ts'; async function renderRelationship(sourcePubkey: string, targetPubkey: string) { diff --git a/src/views/mastodon/statuses.ts b/src/views/mastodon/statuses.ts index a951e0a..c0b65c2 100644 --- a/src/views/mastodon/statuses.ts +++ b/src/views/mastodon/statuses.ts @@ -1,12 +1,12 @@ import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts'; import { Conf } from '@/config.ts'; -import { eventsDB } from '@/db/events.ts'; 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 { eventsDB } from '@/storages.ts'; +import { type DittoEvent } from '@/store.ts'; import { nostrDate } from '@/utils.ts'; import { unfurlCardCached } from '@/utils/unfurl.ts'; import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';