From 41bcd77853a145f0ec530bc46ed077043cde2308 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 6 Aug 2024 15:34:10 -0500 Subject: [PATCH] Optimize relationships database calls --- src/controllers/api/accounts.ts | 45 +++++++++++++++++++++++++---- src/views/mastodon/relationships.ts | 31 +++++++++++++------- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 460aa6dd..d7541f8b 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -160,7 +160,25 @@ const relationshipsController: AppController = async (c) => { return c.json({ error: 'Missing `id[]` query parameters.' }, 422); } - const result = await Promise.all(ids.data.map((id) => renderRelationship(pubkey, id))); + const db = await Storages.db(); + + const [sourceEvents, targetEvents] = await Promise.all([ + db.query([{ kinds: [3, 10000], authors: [pubkey] }]), + db.query([{ kinds: [3], authors: ids.data }]), + ]); + + const event3 = sourceEvents.find((event) => event.kind === 3 && event.pubkey === pubkey); + const event10000 = sourceEvents.find((event) => event.kind === 10000 && event.pubkey === pubkey); + + const result = ids.data.map((id) => + renderRelationship({ + sourcePubkey: pubkey, + targetPubkey: id, + event3, + target3: targetEvents.find((event) => event.kind === 3 && event.pubkey === id), + event10000, + }) + ); return c.json(result); }; @@ -325,7 +343,7 @@ const followController: AppController = async (c) => { c, ); - const relationship = await renderRelationship(sourcePubkey, targetPubkey); + const relationship = await getRelationship(sourcePubkey, targetPubkey); relationship.following = true; return c.json(relationship); @@ -342,7 +360,7 @@ const unfollowController: AppController = async (c) => { c, ); - const relationship = await renderRelationship(sourcePubkey, targetPubkey); + const relationship = await getRelationship(sourcePubkey, targetPubkey); return c.json(relationship); }; @@ -379,7 +397,7 @@ const muteController: AppController = async (c) => { c, ); - const relationship = await renderRelationship(sourcePubkey, targetPubkey); + const relationship = await getRelationship(sourcePubkey, targetPubkey); return c.json(relationship); }; @@ -394,7 +412,7 @@ const unmuteController: AppController = async (c) => { c, ); - const relationship = await renderRelationship(sourcePubkey, targetPubkey); + const relationship = await getRelationship(sourcePubkey, targetPubkey); return c.json(relationship); }; @@ -447,6 +465,23 @@ const familiarFollowersController: AppController = async (c) => { return c.json(results); }; +async function getRelationship(sourcePubkey: string, targetPubkey: string) { + const db = await Storages.db(); + + const [sourceEvents, targetEvents] = await Promise.all([ + db.query([{ kinds: [3, 10000], authors: [sourcePubkey] }]), + db.query([{ kinds: [3], authors: [targetPubkey] }]), + ]); + + return renderRelationship({ + sourcePubkey, + targetPubkey, + event3: sourceEvents.find((event) => event.kind === 3 && event.pubkey === sourcePubkey), + target3: targetEvents.find((event) => event.kind === 3 && event.pubkey === targetPubkey), + event10000: sourceEvents.find((event) => event.kind === 10000 && event.pubkey === sourcePubkey), + }); +} + export { accountController, accountLookupController, diff --git a/src/views/mastodon/relationships.ts b/src/views/mastodon/relationships.ts index 425ea563..833a2d56 100644 --- a/src/views/mastodon/relationships.ts +++ b/src/views/mastodon/relationships.ts @@ -1,18 +1,27 @@ -import { Storages } from '@/storages.ts'; +import { NostrEvent } from '@nostrify/nostrify'; + import { hasTag } from '@/utils/tags.ts'; -async function renderRelationship(sourcePubkey: string, targetPubkey: string) { - const db = await Storages.db(); +interface RenderRelationshipOpts { + sourcePubkey: string; + targetPubkey: string; + event3: NostrEvent | undefined; + target3: NostrEvent | undefined; + event10000: NostrEvent | undefined; +} - const events = await db.query([ - { kinds: [3], authors: [sourcePubkey], limit: 1 }, - { kinds: [3], authors: [targetPubkey], limit: 1 }, - { kinds: [10000], authors: [sourcePubkey], limit: 1 }, - ]); +function renderRelationship({ sourcePubkey, targetPubkey, event3, target3, event10000 }: RenderRelationshipOpts) { + // const db = await Storages.db(); - const event3 = events.find((event) => event.kind === 3 && event.pubkey === sourcePubkey); - const target3 = events.find((event) => event.kind === 3 && event.pubkey === targetPubkey); - const event10000 = events.find((event) => event.kind === 10000 && event.pubkey === sourcePubkey); + // const events = await db.query([ + // { kinds: [3], authors: [sourcePubkey], limit: 1 }, + // { kinds: [3], authors: [targetPubkey], limit: 1 }, + // { kinds: [10000], authors: [sourcePubkey], limit: 1 }, + // ]); + + // const event3 = events.find((event) => event.kind === 3 && event.pubkey === sourcePubkey); + // const target3 = events.find((event) => event.kind === 3 && event.pubkey === targetPubkey); + // const event10000 = events.find((event) => event.kind === 10000 && event.pubkey === sourcePubkey); return { id: targetPubkey,