an atproto based link aggregator
1import { localDb } from '$lib/server/db';
2import { votes } from '$lib/server/db/schema';
3import { eq, inArray, sql, and } from 'drizzle-orm';
4
5/**
6 * Get vote counts for a list of URIs from the local votes table
7 */
8export async function getVoteCounts(uris: string[]): Promise<Map<string, number>> {
9 if (uris.length === 0) return new Map();
10
11 const counts = await localDb
12 .select({
13 targetUri: votes.targetUri,
14 count: sql<number>`SUM(${votes.value})`
15 })
16 .from(votes)
17 .where(inArray(votes.targetUri, uris))
18 .groupBy(votes.targetUri);
19
20 const result = new Map<string, number>();
21 for (const row of counts) {
22 result.set(row.targetUri, row.count);
23 }
24 return result;
25}
26
27/**
28 * Get user's votes for a list of URIs
29 */
30export async function getUserVotes(userDid: string, uris: string[]): Promise<Map<string, number>> {
31 if (uris.length === 0) return new Map();
32
33 const userVoteRows = await localDb
34 .select({ targetUri: votes.targetUri, value: votes.value })
35 .from(votes)
36 .where(and(eq(votes.userDid, userDid), inArray(votes.targetUri, uris)));
37
38 const result = new Map<string, number>();
39 for (const vote of userVoteRows) {
40 result.set(vote.targetUri, vote.value);
41 }
42 return result;
43}