Refactor some code that uses getFilters to import the whole module under a namespace

This commit is contained in:
Alex Gleason 2023-08-17 15:41:27 -05:00
parent 4f9b5c1431
commit 299a1a0db0
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 16 additions and 16 deletions

View File

@ -1,7 +1,7 @@
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { type Filter, findReplyTag, z } from '@/deps.ts'; import { type Filter, findReplyTag, z } from '@/deps.ts';
import { publish } from '@/client.ts'; import { publish } from '@/client.ts';
import { getFilters } from '@/mixer.ts'; import * as mixer from '@/mixer.ts';
import { getAuthor, getFollows } from '@/queries.ts'; import { getAuthor, getFollows } from '@/queries.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { signEvent } from '@/sign.ts'; import { signEvent } from '@/sign.ts';
@ -117,7 +117,7 @@ const accountStatusesController: AppController = async (c) => {
filter['#t'] = [tagged]; filter['#t'] = [tagged];
} }
let events = await getFilters([filter]); let events = await mixer.getFilters([filter]);
events.sort(eventDateComparator); events.sort(eventDateComparator);
if (exclude_replies) { if (exclude_replies) {

View File

@ -1,4 +1,4 @@
import { getFilters, insertEvent } from '@/db/events.ts'; import * as eventsDB from '@/db/events.ts';
import { findUser } from '@/db/users.ts'; import { findUser } from '@/db/users.ts';
import { jsonSchema } from '@/schema.ts'; import { jsonSchema } from '@/schema.ts';
import { import {
@ -46,7 +46,7 @@ function connectStream(socket: WebSocket) {
} }
async function handleReq([_, sub, ...filters]: ClientREQ) { async function handleReq([_, sub, ...filters]: ClientREQ) {
for (const event of await getFilters(prepareFilters(filters))) { for (const event of await eventsDB.getFilters(prepareFilters(filters))) {
send(['EVENT', sub, event]); send(['EVENT', sub, event]);
} }
send(['EOSE', sub]); send(['EOSE', sub]);
@ -54,7 +54,7 @@ function connectStream(socket: WebSocket) {
async function handleEvent([_, event]: ClientEVENT) { async function handleEvent([_, event]: ClientEVENT) {
if (await findUser({ pubkey: event.pubkey })) { if (await findUser({ pubkey: event.pubkey })) {
insertEvent(event); eventsDB.insertEvent(event);
send(['OK', event.id, true, '']); send(['OK', event.id, true, '']);
} else { } else {
send(['OK', event.id, false, 'blocked: only registered users can post']); send(['OK', event.id, false, 'blocked: only registered users can post']);

View File

@ -1,7 +1,7 @@
import { type Event, matchFilters } from '@/deps.ts'; import { type Event, matchFilters } from '@/deps.ts';
import { getFilters as getFiltersClient } from '@/client.ts'; import * as client from '@/client.ts';
import { getFilters as getFiltersDB } from '@/db/events.ts'; import * as eventsDB from '@/db/events.ts';
import { eventDateComparator } from '@/utils.ts'; import { eventDateComparator } from '@/utils.ts';
import type { DittoFilter, GetFiltersOpts } from '@/types.ts'; import type { DittoFilter, GetFiltersOpts } from '@/types.ts';
@ -12,8 +12,8 @@ async function getFilters<K extends number>(
opts?: GetFiltersOpts, opts?: GetFiltersOpts,
): Promise<Event<K>[]> { ): Promise<Event<K>[]> {
const results = await Promise.allSettled([ const results = await Promise.allSettled([
getFiltersClient(filters, opts), client.getFilters(filters, opts),
getFiltersDB(filters, opts), eventsDB.getFilters(filters, opts),
]); ]);
const events = results const events = results

View File

@ -1,7 +1,7 @@
import { type Event, type Filter, findReplyTag } from '@/deps.ts'; import { type Event, type Filter, findReplyTag } from '@/deps.ts';
import { type PaginationParams } from '@/utils.ts'; import { type PaginationParams } from '@/utils.ts';
import { getFilters as getFiltersMixer } from './mixer.ts'; import * as mixer from './mixer.ts';
interface GetEventOpts<K extends number> { interface GetEventOpts<K extends number> {
/** Timeout in milliseconds. */ /** Timeout in milliseconds. */
@ -20,19 +20,19 @@ const getEvent = async <K extends number = number>(
if (kind) { if (kind) {
filter.kinds = [kind]; filter.kinds = [kind];
} }
const [event] = await getFiltersMixer([filter], { limit: 1, timeout }); const [event] = await mixer.getFilters([filter], { limit: 1, timeout });
return event; return event;
}; };
/** Get a Nostr `set_medatadata` event for a user's pubkey. */ /** Get a Nostr `set_medatadata` event for a user's pubkey. */
const getAuthor = async (pubkey: string, timeout = 1000): Promise<Event<0> | undefined> => { const getAuthor = async (pubkey: string, timeout = 1000): Promise<Event<0> | undefined> => {
const [event] = await getFiltersMixer([{ authors: [pubkey], kinds: [0] }], { timeout }); const [event] = await mixer.getFilters([{ authors: [pubkey], kinds: [0] }], { timeout });
return event; return event;
}; };
/** Get users the given pubkey follows. */ /** Get users the given pubkey follows. */
const getFollows = async (pubkey: string, timeout = 1000): Promise<Event<3> | undefined> => { const getFollows = async (pubkey: string, timeout = 1000): Promise<Event<3> | undefined> => {
const [event] = await getFiltersMixer([{ authors: [pubkey], kinds: [3] }], { timeout }); const [event] = await mixer.getFilters([{ authors: [pubkey], kinds: [3] }], { timeout });
return event; return event;
}; };
@ -53,12 +53,12 @@ async function getFeed(pubkey: string, params: PaginationParams): Promise<Event<
...params, ...params,
}; };
return getFiltersMixer([filter], { timeout: 5000 }); return mixer.getFilters([filter], { timeout: 5000 });
} }
/** Get a feed of all known text notes. */ /** Get a feed of all known text notes. */
function getPublicFeed(params: PaginationParams): Promise<Event<1>[]> { function getPublicFeed(params: PaginationParams): Promise<Event<1>[]> {
return getFiltersMixer([{ kinds: [1], ...params }], { timeout: 5000 }); return mixer.getFilters([{ kinds: [1], ...params }], { timeout: 5000 });
} }
async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise<Event<1>[]> { async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise<Event<1>[]> {
@ -80,7 +80,7 @@ async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise
} }
function getDescendants(eventId: string): Promise<Event<1>[]> { function getDescendants(eventId: string): Promise<Event<1>[]> {
return getFiltersMixer([{ kinds: [1], '#e': [eventId] }], { limit: 200, timeout: 2000 }); return mixer.getFilters([{ kinds: [1], '#e': [eventId] }], { limit: 200, timeout: 2000 });
} }
export { getAncestors, getAuthor, getDescendants, getEvent, getFeed, getFollows, getPublicFeed }; export { getAncestors, getAuthor, getDescendants, getEvent, getFeed, getFollows, getPublicFeed };