From 57d7f7ff40d28f8cd2350801f45a92b1907461fa Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jan 2024 16:30:11 -0600 Subject: [PATCH] Support `pinned` param in accountStatusesController --- src/controllers/api/accounts.ts | 13 +++++++++---- src/queries.ts | 6 ++---- src/views.ts | 4 ++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index b25dbe4..6ad3944 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -7,12 +7,12 @@ import { type DittoFilter } from '@/filter.ts'; import { getAuthor, getFollowedPubkeys } from '@/queries.ts'; import { booleanParamSchema, fileSchema } from '@/schema.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; -import { addTag, deleteTag } from '@/tags.ts'; +import { addTag, deleteTag, getTagSet } from '@/tags.ts'; import { uploadFile } from '@/upload.ts'; import { lookupAccount, nostrNow } from '@/utils.ts'; import { paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/web.ts'; import { createEvent } from '@/utils/web.ts'; -import { renderAccounts, renderEventAccounts } from '@/views.ts'; +import { renderAccounts, renderEventAccounts, renderStatuses } from '@/views.ts'; import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { renderRelationship } from '@/views/mastodon/relationships.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; @@ -134,9 +134,14 @@ const accountStatusesController: AppController = async (c) => { const { since, until } = paginationSchema.parse(c.req.query()); const { pinned, limit, exclude_replies, tagged } = accountStatusesQuerySchema.parse(c.req.query()); - // Nostr doesn't support pinned statuses. if (pinned) { - return c.json([]); + const [pinEvent] = await eventsDB.getEvents([{ kinds: [10001], authors: [pubkey], limit: 1 }]); + if (pinEvent) { + const pinnedEventIds = getTagSet(pinEvent.tags, 'e'); + return renderStatuses(c, [...pinnedEventIds].reverse()); + } else { + return c.json([]); + } } const filter: DittoFilter<1> = { diff --git a/src/queries.ts b/src/queries.ts index 95020d0..de60553 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -4,6 +4,7 @@ import { type Event, findReplyTag } from '@/deps.ts'; import { type AuthorMicrofilter, type DittoFilter, type IdMicrofilter, type Relation } from '@/filter.ts'; import { reqmeister } from '@/reqmeister.ts'; import { type DittoEvent } from '@/store.ts'; +import { getTagSet } from '@/tags.ts'; interface GetEventOpts { /** Signal to abort the request. */ @@ -87,10 +88,7 @@ const getFollows = async (pubkey: string, signal?: AbortSignal): Promise { const event = await getFollows(pubkey, signal); if (!event) return []; - - return event.tags - .filter((tag) => tag[0] === 'p') - .map((tag) => tag[1]); + return [...getTagSet(event.tags, 'p')]; } /** Get pubkeys the user follows, including the user's own pubkey. */ diff --git a/src/views.ts b/src/views.ts index 7c22170..b87baad 100644 --- a/src/views.ts +++ b/src/views.ts @@ -47,6 +47,10 @@ async function renderAccounts(c: AppContext, authors: string[], signal = AbortSi /** Render statuses by event IDs. */ async function renderStatuses(c: AppContext, ids: string[], signal = AbortSignal.timeout(1000)) { + if (!ids.length) { + return c.json([]); + } + const { limit } = paginationSchema.parse(c.req.query()); const events = await eventsDB.getEvents(