From f279ead4d75b2b01aaadbffddc3d60ecfc5b4e6f Mon Sep 17 00:00:00 2001 From: LDA Date: Sat, 28 Sep 2024 12:31:12 +0200 Subject: [PATCH] [ADD/WIP] Start processing event relations --- Schema/Relation.json | 19 +++++++++++++++++++ src/Room/V1/Send.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Schema/Relation.json diff --git a/Schema/Relation.json b/Schema/Relation.json new file mode 100644 index 0000000..d12ff78 --- /dev/null +++ b/Schema/Relation.json @@ -0,0 +1,19 @@ +{ + "guard": "TELODENDRIA_SCHEMA_RELATION_H", + "header": "Schema/Relation.h", + "types": { + "Relation": { + "type": "struct", + "fields": { + "event_id": { + "type": "string", + "required": true + }, + "rel_type": { + "type": "string", + "required": true + } + } + } + } +} diff --git a/src/Room/V1/Send.c b/src/Room/V1/Send.c index dfa8893..5557885 100644 --- a/src/Room/V1/Send.c +++ b/src/Room/V1/Send.c @@ -1,5 +1,6 @@ #include "Room/internal.h" +#include #include #include @@ -312,6 +313,9 @@ RoomAddEventV1(Room *room, PduV1 pdu, PduV1Status status) * notified about. */ if (status == PDUV1_STATUS_ACCEPTED) { + HashMap *relates_to = + JsonValueAsObject(HashMapGet(pdu.content, "m.relates_to")); + Relation rel = { 0 }; State *state; char *type, *state_key, *event_id; @@ -319,6 +323,34 @@ RoomAddEventV1(Room *room, PduV1 pdu, PduV1Status status) /* If we have a membership change, then add it to the * proper table. */ + if (RelationFromJson(relates_to, &rel, NULL)) + { + DbRef *relate = DbLock( + room->db, + 4, "rooms", room->id, "events", rel.event_id + ); + HashMap *relObj = DbJson(relate); + if (relObj) + { + Array *relList = JsonValueAsArray(HashMapGet( + relObj, "relations" + )); + if (!relList) + { + relList = ArrayCreate(); + JsonValueFree(HashMapSet( + relObj, "relations", JsonValueArray(relList) + )); + } + + /* TODO: We may want to treat the relation as 'special' + * if the specification asks us to bundle it. */ + ArrayAdd(relList, JsonValueString(pdu.event_id)); + } + + RelationFree(&rel); + DbUnlock(room->db, relate); + } if (StrEquals(pdu.type, "m.room.member")) { CommonID *id = UserIdParse(pdu.state_key, NULL);