mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
feat(handleEvent7): update keys/reactions directly with kysely, not raw SQL
TODO: create migration to convert event_stats to jsonb
This commit is contained in:
parent
bc24bb975c
commit
a1078de07b
1 changed files with 28 additions and 26 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import { NostrEvent, NSchema as n, NStore } from '@nostrify/nostrify';
|
import { NostrEvent, NSchema as n, NStore } from '@nostrify/nostrify';
|
||||||
import { Kysely, sql, UpdateObject } from 'kysely';
|
import { Kysely, sql, UpdateObject, ValueExpression } from 'kysely';
|
||||||
import { SetRequired } from 'type-fest';
|
import { SetRequired } from 'type-fest';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
|
@ -111,31 +111,33 @@ async function handleEvent7(kysely: Kysely<DittoTables>, event: NostrEvent, x: n
|
||||||
const emoji = event.content;
|
const emoji = event.content;
|
||||||
|
|
||||||
if (id && emoji && (['+', '-'].includes(emoji) || /^\p{RGI_Emoji}$/v.test(emoji))) {
|
if (id && emoji && (['+', '-'].includes(emoji) || /^\p{RGI_Emoji}$/v.test(emoji))) {
|
||||||
const baka = await sql`
|
await kysely.updateTable('event_stats')
|
||||||
UPDATE
|
.set((eb) => {
|
||||||
event_stats
|
// Updated reactions.
|
||||||
SET
|
const result = eb.fn('jsonb_set', [
|
||||||
reactions = CASE WHEN (
|
sql`${eb.ref('reactions')}::jsonb`,
|
||||||
jsonb_set(
|
sql<string[]>`ARRAY[${emoji}]`,
|
||||||
reactions :: jsonb,
|
eb.case()
|
||||||
ARRAY[${emoji}],
|
.when(sql`reactions::jsonb -> ${emoji}`, 'is', null)
|
||||||
CASE WHEN reactions :: jsonb -> ${emoji} IS NULL THEN ${x} ELSE to_jsonb(
|
.then(sql`${x}::jsonb`) // Set the emoji count for the first time.
|
||||||
(reactions :: jsonb -> ${emoji}):: int + ${x} :: int
|
.else(eb.fn('to_jsonb', [sql`(reactions::jsonb -> ${emoji})::int + ${x}`])) // Increment or decrement the emoji count.
|
||||||
) END
|
.end(),
|
||||||
):: jsonb ->> ${emoji}
|
]);
|
||||||
):: int = 0 THEN reactions :: jsonb - ${emoji} ELSE jsonb_set(
|
|
||||||
reactions :: jsonb,
|
// Only reactions with a count greater than zero.
|
||||||
ARRAY[${emoji}],
|
const cleanedReactions = eb.case()
|
||||||
CASE WHEN reactions :: jsonb -> ${emoji} IS NULL THEN ${x} ELSE to_jsonb(
|
.when(sql`(${result} -> ${emoji})::int`, '<', 1)
|
||||||
(reactions :: jsonb -> ${emoji}):: int + ${x} :: int
|
.then(sql`${result} - ${emoji}`)
|
||||||
) END
|
.else(result)
|
||||||
) END,
|
.end() as ValueExpression<DittoTables, 'event_stats', string>;
|
||||||
reactions_count = reactions_count + ${x}
|
|
||||||
WHERE
|
return {
|
||||||
event_id = ${id}
|
reactions: cleanedReactions,
|
||||||
`
|
reactions_count: eb('reactions_count', '+', x),
|
||||||
.execute(kysely);
|
};
|
||||||
console.log(baka);
|
})
|
||||||
|
.where('event_id', '=', id)
|
||||||
|
.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue