diff --git a/pkg/hicli/database/upgrades/00-latest-revision.sql b/pkg/hicli/database/upgrades/00-latest-revision.sql index f789bba..923445c 100644 --- a/pkg/hicli/database/upgrades/00-latest-revision.sql +++ b/pkg/hicli/database/upgrades/00-latest-revision.sql @@ -1,4 +1,4 @@ --- v0 -> v5 (compatible with v5+): Latest revision +-- v0 -> v6 (compatible with v5+): Latest revision CREATE TABLE account ( user_id TEXT NOT NULL PRIMARY KEY, device_id TEXT NOT NULL, @@ -158,6 +158,7 @@ CREATE TRIGGER event_insert_fill_reactions 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( @@ -179,6 +180,7 @@ CREATE TRIGGER event_redact_fill_reactions 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( diff --git a/pkg/hicli/database/upgrades/06-hacky-reaction-fix.sql b/pkg/hicli/database/upgrades/06-hacky-reaction-fix.sql new file mode 100644 index 0000000..0220ad1 --- /dev/null +++ b/pkg/hicli/database/upgrades/06-hacky-reaction-fix.sql @@ -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;