trendingStatusesController: sort a simpler way

This commit is contained in:
Alex Gleason 2024-06-02 12:15:32 -05:00
parent 45d9a113c3
commit a911e36a7e
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 5 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import { NostrEvent } from '@nostrify/nostrify';
import { z } from 'zod'; import { z } from 'zod';
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
@ -92,12 +93,13 @@ const trendingStatusesController: AppController = async (c) => {
return c.json([]); return c.json([]);
} }
const events = await store.query([{ ids }]) const results = await store.query([{ ids }])
.then((events) => hydrateEvents({ events, store })); .then((events) => hydrateEvents({ events, store }));
// Sort events in the order they appear in the label. // Sort events in the order they appear in the label.
const indexes = ids.reduce<Record<string, number>>((acc, id, index) => ({ ...acc, [id]: index }), {}); const events = ids
events.sort((a, b) => indexes[a.id] - indexes[b.id]); .map((id) => results.find((event) => event.id === id))
.filter((event): event is NostrEvent => !!event);
const statuses = await Promise.all( const statuses = await Promise.all(
events.map((event) => renderStatus(event, {})), events.map((event) => renderStatus(event, {})),