hicli/database: add temporary hacky fix for reaction aggregations

The aggregations will be redone properly later
This commit is contained in:
Tulir Asokan 2024-10-22 16:36:49 +03:00
parent 0e328c44d3
commit 014c8c07a8
2 changed files with 49 additions and 1 deletions

View file

@ -1,4 +1,4 @@
-- v0 -> v5 (compatible with v5+): Latest revision -- v0 -> v6 (compatible with v5+): Latest revision
CREATE TABLE account ( CREATE TABLE account (
user_id TEXT NOT NULL PRIMARY KEY, user_id TEXT NOT NULL PRIMARY KEY,
device_id TEXT NOT NULL, device_id TEXT NOT NULL,
@ -158,6 +158,7 @@ CREATE TRIGGER event_insert_fill_reactions
AND NEW.relation_type = 'm.annotation' AND NEW.relation_type = 'm.annotation'
AND NEW.redacted_by IS NULL AND NEW.redacted_by IS NULL
AND typeof(NEW.content ->> '$."m.relates_to".key') = 'text' AND typeof(NEW.content ->> '$."m.relates_to".key') = 'text'
AND NEW.content ->> '$."m.relates_to".key' NOT LIKE '%"%'
BEGIN BEGIN
UPDATE event UPDATE event
SET reactions=json_set( SET reactions=json_set(
@ -179,6 +180,7 @@ CREATE TRIGGER event_redact_fill_reactions
AND NEW.redacted_by IS NOT NULL AND NEW.redacted_by IS NOT NULL
AND OLD.redacted_by IS NULL AND OLD.redacted_by IS NULL
AND typeof(NEW.content ->> '$."m.relates_to".key') = 'text' AND typeof(NEW.content ->> '$."m.relates_to".key') = 'text'
AND NEW.content ->> '$."m.relates_to".key' NOT LIKE '%"%'
BEGIN BEGIN
UPDATE event UPDATE event
SET reactions=json_set( SET reactions=json_set(

View file

@ -0,0 +1,46 @@
-- v6 (compatible with v5+): Add hack for reaction aggregation
DROP TRIGGER event_insert_fill_reactions;
DROP TRIGGER event_redact_fill_reactions;
CREATE TRIGGER event_insert_fill_reactions
AFTER INSERT
ON event
WHEN NEW.type = 'm.reaction'
AND NEW.relation_type = 'm.annotation'
AND NEW.redacted_by IS NULL
AND typeof(NEW.content ->> '$."m.relates_to".key') = 'text'
AND NEW.content ->> '$."m.relates_to".key' NOT LIKE '%"%'
BEGIN
UPDATE event
SET reactions=json_set(
reactions,
'$.' || json_quote(NEW.content ->> '$."m.relates_to".key'),
coalesce(
reactions ->> ('$.' || json_quote(NEW.content ->> '$."m.relates_to".key')),
0
) + 1)
WHERE event_id = NEW.relates_to
AND reactions IS NOT NULL;
END;
CREATE TRIGGER event_redact_fill_reactions
AFTER UPDATE
ON event
WHEN NEW.type = 'm.reaction'
AND NEW.relation_type = 'm.annotation'
AND NEW.redacted_by IS NOT NULL
AND OLD.redacted_by IS NULL
AND typeof(NEW.content ->> '$."m.relates_to".key') = 'text'
AND NEW.content ->> '$."m.relates_to".key' NOT LIKE '%"%'
BEGIN
UPDATE event
SET reactions=json_set(
reactions,
'$.' || json_quote(NEW.content ->> '$."m.relates_to".key'),
coalesce(
reactions ->> ('$.' || json_quote(NEW.content ->> '$."m.relates_to".key')),
0
) - 1)
WHERE event_id = NEW.relates_to
AND reactions IS NOT NULL;
END;