From 7eab62b9a9e30c892b510d92dad68d396dbd745b Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 17 Sep 2024 14:02:46 -0300 Subject: [PATCH 1/2] refactor: getFollowedPubkeys() and getFeedPubkeys() functions return Set --- src/controllers/api/accounts.ts | 4 ++-- src/controllers/api/streaming.ts | 2 +- src/controllers/api/timelines.ts | 2 +- src/queries.ts | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index c946b697..e5037a02 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -381,7 +381,7 @@ const followersController: AppController = (c) => { const followingController: AppController = async (c) => { const pubkey = c.req.param('pubkey'); const pubkeys = await getFollowedPubkeys(pubkey); - return renderAccounts(c, pubkeys); + return renderAccounts(c, [...pubkeys]); }; /** https://docs.joinmastodon.org/methods/accounts/#block */ @@ -460,7 +460,7 @@ const familiarFollowersController: AppController = async (c) => { const follows = await getFollowedPubkeys(pubkey); const results = await Promise.all(ids.map(async (id) => { - const followLists = await store.query([{ kinds: [3], authors: follows, '#p': [id] }]) + const followLists = await store.query([{ kinds: [3], authors: [...follows], '#p': [id] }]) .then((events) => hydrateEvents({ events, store })); const accounts = await Promise.all( diff --git a/src/controllers/api/streaming.ts b/src/controllers/api/streaming.ts index cfa8c3c5..aafa9915 100644 --- a/src/controllers/api/streaming.ts +++ b/src/controllers/api/streaming.ts @@ -215,7 +215,7 @@ async function topicToFilter( // HACK: this puts the user's entire contacts list into RAM, // and then calls `matchFilters` over it. Refreshing the page // is required after following a new user. - return pubkey ? { kinds: [1, 6], authors: await getFeedPubkeys(pubkey) } : undefined; + return pubkey ? { kinds: [1, 6], authors: [...await getFeedPubkeys(pubkey)] } : undefined; } } diff --git a/src/controllers/api/timelines.ts b/src/controllers/api/timelines.ts index 5fce4602..483676e1 100644 --- a/src/controllers/api/timelines.ts +++ b/src/controllers/api/timelines.ts @@ -13,7 +13,7 @@ import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts'; const homeTimelineController: AppController = async (c) => { const params = c.get('pagination'); const pubkey = await c.get('signer')?.getPublicKey()!; - const authors = await getFeedPubkeys(pubkey); + const authors = [...await getFeedPubkeys(pubkey)]; return renderStatuses(c, [{ authors, kinds: [1, 6], ...params }]); }; diff --git a/src/queries.ts b/src/queries.ts index 9ee86a36..9c188176 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -56,16 +56,16 @@ const getFollows = async (pubkey: string, signal?: AbortSignal): Promise { +async function getFollowedPubkeys(pubkey: string, signal?: AbortSignal): Promise> { const event = await getFollows(pubkey, signal); - if (!event) return []; - return [...getTagSet(event.tags, 'p')]; + if (!event) return new Set(); + return getTagSet(event.tags, 'p'); } /** Get pubkeys the user follows, including the user's own pubkey. */ -async function getFeedPubkeys(pubkey: string): Promise { +async function getFeedPubkeys(pubkey: string): Promise> { const authors = await getFollowedPubkeys(pubkey); - return [...authors, pubkey]; + return new Set([...authors, pubkey]); } async function getAncestors(store: NStore, event: NostrEvent, result: NostrEvent[] = []): Promise { From 19069c041745251040827b133fa948adf749d6a8 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 17 Sep 2024 14:37:04 -0300 Subject: [PATCH 2/2] refactor: return set in a different way --- src/queries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/queries.ts b/src/queries.ts index 9c188176..3ca805a8 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -65,7 +65,7 @@ async function getFollowedPubkeys(pubkey: string, signal?: AbortSignal): Promise /** Get pubkeys the user follows, including the user's own pubkey. */ async function getFeedPubkeys(pubkey: string): Promise> { const authors = await getFollowedPubkeys(pubkey); - return new Set([...authors, pubkey]); + return authors.add(pubkey); } async function getAncestors(store: NStore, event: NostrEvent, result: NostrEvent[] = []): Promise {