Ensure relations are loaded throughout the API where needed

This commit is contained in:
Alex Gleason
2023-12-10 16:21:18 -06:00
parent 733b8ba9c5
commit a5369d9826
7 changed files with 44 additions and 21 deletions

View File

@@ -59,7 +59,7 @@ const createAccountController: AppController = async (c) => {
const verifyCredentialsController: AppController = async (c) => {
const pubkey = c.get('pubkey')!;
const event = await getAuthor(pubkey);
const event = await getAuthor(pubkey, { relations: ['author_stats'] });
if (event) {
return c.json(await renderAccount(event, { withSource: true }));
} else {
@@ -138,7 +138,15 @@ const accountStatusesController: AppController = async (c) => {
return c.json([]);
}
const filter: DittoFilter<1> = { authors: [pubkey], kinds: [1], relations: ['author'], since, until, limit };
const filter: DittoFilter<1> = {
authors: [pubkey],
kinds: [1],
relations: ['author', 'event_stats', 'author_stats'],
since,
until,
limit,
};
if (tagged) {
filter['#t'] = [tagged];
}
@@ -257,7 +265,9 @@ const favouritesController: AppController = async (c) => {
.map((event) => event.tags.find((tag) => tag[0] === 'e')?.[1])
.filter((id): id is string => !!id);
const events1 = await mixer.getFilters([{ kinds: [1], ids, relations: ['author'] }], { timeout: Time.seconds(1) });
const events1 = await mixer.getFilters([{ kinds: [1], ids, relations: ['author', 'event_stats', 'author_stats'] }], {
timeout: Time.seconds(1),
});
const statuses = await Promise.all(events1.map((event) => renderStatus(event, c.get('pubkey'))));
return paginated(c, events1, statuses);

View File

@@ -69,7 +69,7 @@ function searchEvents({ q, type, limit, account_id }: SearchQuery): Promise<Even
const filter: DittoFilter = {
kinds: typeToKinds(type),
search: q,
relations: ['author'],
relations: ['author', 'event_stats', 'author_stats'],
limit,
};
@@ -115,16 +115,20 @@ async function getLookupFilters({ q, type, resolve }: SearchQuery): Promise<Ditt
const result = nip19.decode(q);
switch (result.type) {
case 'npub':
if (accounts) filters.push({ kinds: [0], authors: [result.data] });
if (accounts) filters.push({ kinds: [0], authors: [result.data], relations: ['author_stats'] });
break;
case 'nprofile':
if (accounts) filters.push({ kinds: [0], authors: [result.data.pubkey] });
if (accounts) filters.push({ kinds: [0], authors: [result.data.pubkey], relations: ['author_stats'] });
break;
case 'note':
if (statuses) filters.push({ kinds: [1], ids: [result.data] });
if (statuses) {
filters.push({ kinds: [1], ids: [result.data], relations: ['author', 'event_stats', 'author_stats'] });
}
break;
case 'nevent':
if (statuses) filters.push({ kinds: [1], ids: [result.data.id] });
if (statuses) {
filters.push({ kinds: [1], ids: [result.data.id], relations: ['author', 'event_stats', 'author_stats'] });
}
break;
}
} catch (_e) {
@@ -136,11 +140,11 @@ async function getLookupFilters({ q, type, resolve }: SearchQuery): Promise<Ditt
} else if (accounts && ACCT_REGEX.test(q)) {
const pubkey = await lookupNip05Cached(q);
if (pubkey) {
filters.push({ kinds: [0], authors: [pubkey] });
filters.push({ kinds: [0], authors: [pubkey], relations: ['author_stats'] });
}
}
return filters.map((filter) => ({ ...filter, relations: ['author'] }));
return filters;
}
export { searchController };

View File

@@ -29,7 +29,7 @@ const createStatusSchema = z.object({
const statusController: AppController = async (c) => {
const id = c.req.param('id');
const event = await getEvent(id, { kind: 1, relations: ['author'] });
const event = await getEvent(id, { kind: 1, relations: ['author', 'event_stats', 'author_stats'] });
if (event) {
return c.json(await renderStatus(event, c.get('pubkey')));
}
@@ -89,7 +89,7 @@ const createStatusController: AppController = async (c) => {
const contextController: AppController = async (c) => {
const id = c.req.param('id');
const event = await getEvent(id, { kind: 1, relations: ['author'] });
const event = await getEvent(id, { kind: 1, relations: ['author', 'event_stats', 'author_stats'] });
async function renderStatuses(events: Event<1>[]) {
const statuses = await Promise.all(events.map((event) => renderStatus(event, c.get('pubkey'))));
@@ -110,7 +110,7 @@ const contextController: AppController = async (c) => {
const favouriteController: AppController = async (c) => {
const id = c.req.param('id');
const target = await getEvent(id, { kind: 1, relations: ['author'] });
const target = await getEvent(id, { kind: 1, relations: ['author', 'event_stats', 'author_stats'] });
if (target) {
await createEvent({

View File

@@ -35,7 +35,7 @@ const hashtagTimelineController: AppController = (c) => {
/** Render statuses for timelines. */
async function renderStatuses(c: AppContext, filters: DittoFilter<1>[]) {
const events = await mixer.getFilters(
filters.map((filter) => ({ ...filter, relations: ['author'] })),
filters.map((filter) => ({ ...filter, relations: ['author', 'event_stats', 'author_stats'] })),
{ timeout: Time.seconds(1) },
);