{"version":3,"file":"comment-reaction-Chz_blWj.mjs","names":[],"sources":["../src/database/repositories/comment-reaction.ts"],"sourcesContent":["import type { Kysely } from \"kysely\";\nimport { ulid } from \"ulidx\";\n\nimport { SQL_BATCH_SIZE, chunks } from \"../../utils/chunks.js\";\nimport type { Database } from \"../types.js\";\n\n/** Per-comment reaction counts: `{ like: 12, love: 3 }`. */\nexport type ReactionCounts = Record<string, number>;\n\nexport interface ToggleReactionInput {\n\tcommentId: string;\n\treaction: string;\n\tvoterHash: string;\n}\n\n/**\n * Repository for comment reactions (likes / emoji).\n *\n * Reactions are deduped per (comment, voter, reaction) by a unique index, so\n * a second toggle of the same reaction by the same voter removes it.\n */\nexport class CommentReactionRepository {\n\tconstructor(private db: Kysely<Database>) {}\n\n\t/**\n\t * Toggle a reaction for a voter on a comment.\n\t *\n\t * @returns `{ reacted: true }` if the reaction was added, `{ reacted: false }`\n\t *   if an existing reaction was removed.\n\t */\n\tasync toggle(input: ToggleReactionInput): Promise<{ reacted: boolean }> {\n\t\t// Idempotent add: insert, or no-op if this voter already has this\n\t\t// reaction. Using ON CONFLICT (rather than read-then-write) avoids a\n\t\t// TOCTOU race where two concurrent toggles both see \"no row\" and the\n\t\t// second INSERT violates the unique index — the old path surfaced that\n\t\t// as a 500. The unique index doubles as the conflict target.\n\t\tconst inserted = await this.db\n\t\t\t.insertInto(\"_emdash_comment_reactions\")\n\t\t\t.values({\n\t\t\t\tid: ulid(),\n\t\t\t\tcomment_id: input.commentId,\n\t\t\t\treaction: input.reaction,\n\t\t\t\tvoter_hash: input.voterHash,\n\t\t\t\tcreated_at: new Date().toISOString(),\n\t\t\t})\n\t\t\t.onConflict((oc) => oc.columns([\"comment_id\", \"voter_hash\", \"reaction\"]).doNothing())\n\t\t\t.executeTakeFirst();\n\n\t\tif ((inserted.numInsertedOrUpdatedRows ?? 0n) > 0n) {\n\t\t\treturn { reacted: true };\n\t\t}\n\n\t\t// The reaction already existed → toggle it off.\n\t\tawait this.db\n\t\t\t.deleteFrom(\"_emdash_comment_reactions\")\n\t\t\t.where(\"comment_id\", \"=\", input.commentId)\n\t\t\t.where(\"voter_hash\", \"=\", input.voterHash)\n\t\t\t.where(\"reaction\", \"=\", input.reaction)\n\t\t\t.execute();\n\t\treturn { reacted: false };\n\t}\n\n\t/**\n\t * Aggregate reaction counts for a set of comments.\n\t *\n\t * @returns a Map keyed by comment id; comments with no reactions are absent.\n\t */\n\tasync countsForComments(commentIds: string[]): Promise<Map<string, ReactionCounts>> {\n\t\tconst result = new Map<string, ReactionCounts>();\n\t\tif (commentIds.length === 0) return result;\n\n\t\tfor (const batch of chunks(commentIds, SQL_BATCH_SIZE)) {\n\t\t\tconst rows = await this.db\n\t\t\t\t.selectFrom(\"_emdash_comment_reactions\")\n\t\t\t\t.select([\"comment_id\", \"reaction\"])\n\t\t\t\t.select((eb) => eb.fn.count<number>(\"id\").as(\"count\"))\n\t\t\t\t.where(\"comment_id\", \"in\", batch)\n\t\t\t\t.groupBy([\"comment_id\", \"reaction\"])\n\t\t\t\t.execute();\n\n\t\t\tfor (const row of rows) {\n\t\t\t\tconst counts = result.get(row.comment_id) ?? {};\n\t\t\t\tcounts[row.reaction] = Number(row.count);\n\t\t\t\tresult.set(row.comment_id, counts);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Which reactions a given voter has set, per comment.\n\t *\n\t * @returns a Map keyed by comment id whose values are the reaction names the\n\t *   voter has active on that comment.\n\t */\n\tasync viewerReactions(commentIds: string[], voterHash: string): Promise<Map<string, string[]>> {\n\t\tconst result = new Map<string, string[]>();\n\t\tif (commentIds.length === 0) return result;\n\n\t\tfor (const batch of chunks(commentIds, SQL_BATCH_SIZE)) {\n\t\t\tconst rows = await this.db\n\t\t\t\t.selectFrom(\"_emdash_comment_reactions\")\n\t\t\t\t.select([\"comment_id\", \"reaction\"])\n\t\t\t\t.where(\"comment_id\", \"in\", batch)\n\t\t\t\t.where(\"voter_hash\", \"=\", voterHash)\n\t\t\t\t.execute();\n\n\t\t\tfor (const row of rows) {\n\t\t\t\tconst list = result.get(row.comment_id) ?? [];\n\t\t\t\tlist.push(row.reaction);\n\t\t\t\tresult.set(row.comment_id, list);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Count a voter's reactions within a recent time window (for rate limiting).\n\t */\n\tasync countRecentByVoter(voterHash: string, windowMinutes = 10): Promise<number> {\n\t\tconst cutoff = new Date(Date.now() - windowMinutes * 60 * 1000).toISOString();\n\t\tconst result = await this.db\n\t\t\t.selectFrom(\"_emdash_comment_reactions\")\n\t\t\t.select((eb) => eb.fn.count<number>(\"id\").as(\"count\"))\n\t\t\t.where(\"voter_hash\", \"=\", voterHash)\n\t\t\t.where(\"created_at\", \">\", cutoff)\n\t\t\t.executeTakeFirst();\n\t\treturn Number(result?.count ?? 0);\n\t}\n}\n"],"mappings":";;;;;;;;;;AAqBA,IAAa,4BAAb,MAAuC;CACtC,YAAY,AAAQ,IAAsB;EAAtB;;;;;;;;CAQpB,MAAM,OAAO,OAA2D;AAkBvE,QAZiB,MAAM,KAAK,GAC1B,WAAW,4BAA4B,CACvC,OAAO;GACP,IAAI,MAAM;GACV,YAAY,MAAM;GAClB,UAAU,MAAM;GAChB,YAAY,MAAM;GAClB,6BAAY,IAAI,MAAM,EAAC,aAAa;GACpC,CAAC,CACD,YAAY,OAAO,GAAG,QAAQ;GAAC;GAAc;GAAc;GAAW,CAAC,CAAC,WAAW,CAAC,CACpF,kBAAkB,EAEN,4BAA4B,MAAM,GAC/C,QAAO,EAAE,SAAS,MAAM;AAIzB,QAAM,KAAK,GACT,WAAW,4BAA4B,CACvC,MAAM,cAAc,KAAK,MAAM,UAAU,CACzC,MAAM,cAAc,KAAK,MAAM,UAAU,CACzC,MAAM,YAAY,KAAK,MAAM,SAAS,CACtC,SAAS;AACX,SAAO,EAAE,SAAS,OAAO;;;;;;;CAQ1B,MAAM,kBAAkB,YAA4D;EACnF,MAAM,yBAAS,IAAI,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