feat(handleEvent7): update keys/reactions directly

TODO: convert reactions to jsonb
TODO: convert to kysely rather than raw SQL
This commit is contained in:
P. Reis 2024-12-29 18:16:19 -03:00
parent aae00cdc10
commit bc24bb975c

View file

@ -112,17 +112,29 @@ async function handleEvent7(kysely: Kysely<DittoTables>, event: NostrEvent, x: n
if (id && emoji && (['+', '-'].includes(emoji) || /^\p{RGI_Emoji}$/v.test(emoji))) {
const baka = await sql`
UPDATE event_stats
SET reactions = jsonb_set(
reactions::jsonb,
UPDATE
event_stats
SET
reactions = CASE WHEN (
jsonb_set(
reactions :: jsonb,
ARRAY[${emoji}],
CASE WHEN reactions::jsonb->${emoji} IS NULL THEN ${x}
ELSE to_jsonb((reactions::jsonb->${emoji})::int + ${x}::int)
END
)
WHERE event_id = ${id}`
CASE WHEN reactions :: jsonb -> ${emoji} IS NULL THEN ${x} ELSE to_jsonb(
(reactions :: jsonb -> ${emoji}):: int + ${x} :: int
) END
):: jsonb ->> ${emoji}
):: int = 0 THEN reactions :: jsonb - ${emoji} ELSE jsonb_set(
reactions :: jsonb,
ARRAY[${emoji}],
CASE WHEN reactions :: jsonb -> ${emoji} IS NULL THEN ${x} ELSE to_jsonb(
(reactions :: jsonb -> ${emoji}):: int + ${x} :: int
) END
) END,
reactions_count = reactions_count + ${x}
WHERE
event_id = ${id}
`
.execute(kysely);
console.log(baka);
}
}