Merge branch 'relationships-perf' into 'main'

Optimize relationships database calls

See merge request soapbox-pub/ditto!446
This commit is contained in:
Alex Gleason 2024-08-06 20:46:09 +00:00
commit 6ae48e60e5
2 changed files with 60 additions and 16 deletions

View file

@ -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,

View file

@ -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,