mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Optimize relationships database calls
This commit is contained in:
parent
75935d0b53
commit
41bcd77853
2 changed files with 60 additions and 16 deletions
|
|
@ -160,7 +160,25 @@ const relationshipsController: AppController = async (c) => {
|
||||||
return c.json({ error: 'Missing `id[]` query parameters.' }, 422);
|
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);
|
return c.json(result);
|
||||||
};
|
};
|
||||||
|
|
@ -325,7 +343,7 @@ const followController: AppController = async (c) => {
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
|
|
||||||
const relationship = await renderRelationship(sourcePubkey, targetPubkey);
|
const relationship = await getRelationship(sourcePubkey, targetPubkey);
|
||||||
relationship.following = true;
|
relationship.following = true;
|
||||||
|
|
||||||
return c.json(relationship);
|
return c.json(relationship);
|
||||||
|
|
@ -342,7 +360,7 @@ const unfollowController: AppController = async (c) => {
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
|
|
||||||
const relationship = await renderRelationship(sourcePubkey, targetPubkey);
|
const relationship = await getRelationship(sourcePubkey, targetPubkey);
|
||||||
return c.json(relationship);
|
return c.json(relationship);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -379,7 +397,7 @@ const muteController: AppController = async (c) => {
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
|
|
||||||
const relationship = await renderRelationship(sourcePubkey, targetPubkey);
|
const relationship = await getRelationship(sourcePubkey, targetPubkey);
|
||||||
return c.json(relationship);
|
return c.json(relationship);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -394,7 +412,7 @@ const unmuteController: AppController = async (c) => {
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
|
|
||||||
const relationship = await renderRelationship(sourcePubkey, targetPubkey);
|
const relationship = await getRelationship(sourcePubkey, targetPubkey);
|
||||||
return c.json(relationship);
|
return c.json(relationship);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -447,6 +465,23 @@ const familiarFollowersController: AppController = async (c) => {
|
||||||
return c.json(results);
|
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 {
|
export {
|
||||||
accountController,
|
accountController,
|
||||||
accountLookupController,
|
accountLookupController,
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,27 @@
|
||||||
import { Storages } from '@/storages.ts';
|
import { NostrEvent } from '@nostrify/nostrify';
|
||||||
|
|
||||||
import { hasTag } from '@/utils/tags.ts';
|
import { hasTag } from '@/utils/tags.ts';
|
||||||
|
|
||||||
async function renderRelationship(sourcePubkey: string, targetPubkey: string) {
|
interface RenderRelationshipOpts {
|
||||||
const db = await Storages.db();
|
sourcePubkey: string;
|
||||||
|
targetPubkey: string;
|
||||||
|
event3: NostrEvent | undefined;
|
||||||
|
target3: NostrEvent | undefined;
|
||||||
|
event10000: NostrEvent | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
const events = await db.query([
|
function renderRelationship({ sourcePubkey, targetPubkey, event3, target3, event10000 }: RenderRelationshipOpts) {
|
||||||
{ kinds: [3], authors: [sourcePubkey], limit: 1 },
|
// const db = await Storages.db();
|
||||||
{ kinds: [3], authors: [targetPubkey], limit: 1 },
|
|
||||||
{ kinds: [10000], authors: [sourcePubkey], limit: 1 },
|
|
||||||
]);
|
|
||||||
|
|
||||||
const event3 = events.find((event) => event.kind === 3 && event.pubkey === sourcePubkey);
|
// const events = await db.query([
|
||||||
const target3 = events.find((event) => event.kind === 3 && event.pubkey === targetPubkey);
|
// { kinds: [3], authors: [sourcePubkey], limit: 1 },
|
||||||
const event10000 = events.find((event) => event.kind === 10000 && event.pubkey === sourcePubkey);
|
// { 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 {
|
return {
|
||||||
id: targetPubkey,
|
id: targetPubkey,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue