Merge branch 'reactions' into 'main'

favouritedByController: filter only likes

See merge request soapbox-pub/ditto!304
This commit is contained in:
Alex Gleason 2024-05-23 18:54:01 +00:00
commit 20cca186ce
2 changed files with 15 additions and 4 deletions

View File

@ -253,7 +253,10 @@ const favouriteController: AppController = async (c) => {
const favouritedByController: AppController = (c) => { const favouritedByController: AppController = (c) => {
const id = c.req.param('id'); const id = c.req.param('id');
const params = paginationSchema.parse(c.req.query()); const params = paginationSchema.parse(c.req.query());
return renderEventAccounts(c, [{ kinds: [7], '#e': [id], ...params }]);
return renderEventAccounts(c, [{ kinds: [7], '#e': [id], ...params }], {
filterFn: ({ content }) => content === '+',
});
}; };
/** https://docs.joinmastodon.org/methods/statuses/#boost */ /** https://docs.joinmastodon.org/methods/statuses/#boost */

View File

@ -1,4 +1,4 @@
import { NostrFilter } from '@nostrify/nostrify'; import { NostrEvent, NostrFilter } from '@nostrify/nostrify';
import { AppContext } from '@/app.ts'; import { AppContext } from '@/app.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
@ -8,18 +8,26 @@ import { paginated, paginationSchema } from '@/utils/api.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
import { accountFromPubkey } from '@/views/mastodon/accounts.ts'; import { accountFromPubkey } from '@/views/mastodon/accounts.ts';
interface RenderEventAccountsOpts {
signal?: AbortSignal;
filterFn?: (event: NostrEvent) => boolean;
}
/** Render account objects for the author of each event. */ /** Render account objects for the author of each event. */
async function renderEventAccounts(c: AppContext, filters: NostrFilter[], signal = AbortSignal.timeout(1000)) { async function renderEventAccounts(c: AppContext, filters: NostrFilter[], opts?: RenderEventAccountsOpts) {
if (!filters.length) { if (!filters.length) {
return c.json([]); return c.json([]);
} }
const { signal = AbortSignal.timeout(1000), filterFn } = opts ?? {};
const store = await Storages.db(); const store = await Storages.db();
const events = await store.query(filters, { signal }) const events = await store.query(filters, { signal })
// Deduplicate by author. // Deduplicate by author.
.then((events) => Array.from(new Map(events.map((event) => [event.pubkey, event])).values())) .then((events) => Array.from(new Map(events.map((event) => [event.pubkey, event])).values()))
.then((events) => hydrateEvents({ events, store, signal })); .then((events) => hydrateEvents({ events, store, signal }))
.then((events) => filterFn ? events.filter(filterFn) : events);
const accounts = await Promise.all( const accounts = await Promise.all(
events.map(({ author, pubkey }) => { events.map(({ author, pubkey }) => {