Remove relations filters, switch some stuff to use optimizer (requires bravery)

This commit is contained in:
Alex Gleason
2024-03-06 12:55:02 -06:00
parent 1499f9b417
commit 8b9566d79b
8 changed files with 59 additions and 146 deletions

View File

@@ -15,6 +15,7 @@ import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderRelationship } from '@/views/mastodon/relationships.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
import { DittoFilter } from '@/interfaces/DittoFilter.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
const usernameSchema = z
.string().min(1).max(30)
@@ -147,7 +148,6 @@ const accountStatusesController: AppController = async (c) => {
const filter: DittoFilter = {
authors: [pubkey],
kinds: [1],
relations: ['author', 'event_stats', 'author_stats'],
since,
until,
limit,
@@ -157,11 +157,16 @@ const accountStatusesController: AppController = async (c) => {
filter['#t'] = [tagged];
}
let events = await eventsDB.query([filter], { signal });
if (exclude_replies) {
events = events.filter((event) => !findReplyTag(event.tags));
}
const events = await eventsDB.query([filter], { signal })
.then((events) =>
hydrateEvents({ events, relations: ['author', 'event_stats', 'author_stats'], storage: eventsDB, signal })
)
.then((events) => {
if (exclude_replies) {
return events.filter((event) => !findReplyTag(event.tags));
}
return events;
});
const statuses = await Promise.all(events.map((event) => renderStatus(event, c.get('pubkey'))));
return paginated(c, events, statuses);
@@ -304,10 +309,10 @@ const favouritesController: AppController = async (c) => {
.map((event) => event.tags.find((tag) => tag[0] === 'e')?.[1])
.filter((id): id is string => !!id);
const events1 = await eventsDB.query(
[{ kinds: [1], ids, relations: ['author', 'event_stats', 'author_stats'] }],
{ signal },
);
const events1 = await eventsDB.query([{ kinds: [1], ids }], { signal })
.then((events) =>
hydrateEvents({ events, relations: ['author', 'event_stats', 'author_stats'], storage: eventsDB, signal })
);
const statuses = await Promise.all(events1.map((event) => renderStatus(event, c.get('pubkey'))));
return paginated(c, events1, statuses);

View File

@@ -8,6 +8,7 @@ import { dedupeEvents } from '@/utils.ts';
import { nip05Cache } from '@/utils/nip05.ts';
import { renderAccount } from '@/views/mastodon/accounts.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
/** Matches NIP-05 names with or without an @ in front. */
const ACCT_REGEX = /^@?(?:([\w.+-]+)@)?([\w.-]+)$/;
@@ -69,7 +70,6 @@ function searchEvents({ q, type, limit, account_id }: SearchQuery, signal: Abort
const filter: DittoFilter = {
kinds: typeToKinds(type),
search: q,
relations: ['author', 'event_stats', 'author_stats'],
limit,
};
@@ -77,7 +77,10 @@ function searchEvents({ q, type, limit, account_id }: SearchQuery, signal: Abort
filter.authors = [account_id];
}
return searchStore.query([filter], { signal });
return searchStore.query([filter], { signal })
.then((events) =>
hydrateEvents({ events, relations: ['author', 'event_stats', 'author_stats'], storage: searchStore, signal })
);
}
/** Get event kinds to search from `type` query param. */
@@ -95,8 +98,12 @@ function typeToKinds(type: SearchQuery['type']): number[] {
/** Resolve a searched value into an event, if applicable. */
async function lookupEvent(query: SearchQuery, signal: AbortSignal): Promise<NostrEvent | undefined> {
const filters = await getLookupFilters(query, signal);
const [event] = await searchStore.query(filters, { limit: 1, signal });
return event;
return searchStore.query(filters, { limit: 1, signal })
.then((events) =>
hydrateEvents({ events, relations: ['author', 'event_stats', 'author_stats'], storage: searchStore, signal })
)
.then(([event]) => event);
}
/** Get filters to lookup the input value. */
@@ -115,19 +122,19 @@ async function getLookupFilters({ q, type, resolve }: SearchQuery, signal: Abort
const result = nip19.decode(q);
switch (result.type) {
case 'npub':
if (accounts) filters.push({ kinds: [0], authors: [result.data], relations: ['author_stats'] });
if (accounts) filters.push({ kinds: [0], authors: [result.data] });
break;
case 'nprofile':
if (accounts) filters.push({ kinds: [0], authors: [result.data.pubkey], relations: ['author_stats'] });
if (accounts) filters.push({ kinds: [0], authors: [result.data.pubkey] });
break;
case 'note':
if (statuses) {
filters.push({ kinds: [1], ids: [result.data], relations: ['author', 'event_stats', 'author_stats'] });
filters.push({ kinds: [1], ids: [result.data] });
}
break;
case 'nevent':
if (statuses) {
filters.push({ kinds: [1], ids: [result.data.id], relations: ['author', 'event_stats', 'author_stats'] });
filters.push({ kinds: [1], ids: [result.data.id] });
}
break;
}
@@ -141,7 +148,7 @@ async function getLookupFilters({ q, type, resolve }: SearchQuery, signal: Abort
try {
const { pubkey } = await nip05Cache.fetch(q, { signal });
if (pubkey) {
filters.push({ kinds: [0], authors: [pubkey], relations: ['author_stats'] });
filters.push({ kinds: [0], authors: [pubkey] });
}
} catch (_e) {
// do nothing