From de1ee3986ef7a6f4256fc0f0d64bae6140309696 Mon Sep 17 00:00:00 2001 From: LDA Date: Thu, 27 Jun 2024 01:44:00 +0200 Subject: [PATCH] [ADD] Start cleaning up more Yeah. Let's clean up a bit more. --- src/Parsee/Data.c | 54 +++++++++++++++++++++++++++++------------------ src/XMPPThread.c | 1 - 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/Parsee/Data.c b/src/Parsee/Data.c index eca3725..b698e3f 100644 --- a/src/Parsee/Data.c +++ b/src/Parsee/Data.c @@ -66,8 +66,8 @@ ParseeCleanup(void *datp) for (i = 0; i < ArraySize(chats); i++) { DbRef *ref; - HashMap *json, *stanzas; - char *stanza; + HashMap *json, *stanzas, *events, *ids; + char *stanza, *event, *id; JsonValue *val; Array *to_delete; size_t j; @@ -75,27 +75,36 @@ ParseeCleanup(void *datp) chat = ArrayGet(chats, i); ref = DbLock(data->db, 2, "chats", chat); json = DbJson(ref); - stanzas = JsonValueAsObject(HashMapGet(json, "stanzas")); - to_delete = ArrayCreate(); - while (HashMapIterate(stanzas, &stanza, (void **) &val)) - { - HashMap *obj = JsonValueAsObject(val); - uint64_t age = JsonValueAsInteger(HashMapGet(obj, "age")); - uint64_t dur = ts - age; - if ((dur > (30 MINUTES))) - { - ArrayAdd(to_delete, StrDuplicate(stanza)); - } - } +#define CleanupField(field, timeout) do \ + { \ + field##s = JsonValueAsObject(HashMapGet(json, #field"s")); \ + to_delete = ArrayCreate(); \ + while (HashMapIterate(field##s, &field, (void **) &val)) \ + { \ + HashMap *obj = JsonValueAsObject(val); \ + uint64_t age = JsonValueAsInteger(HashMapGet(obj, "age")); \ + uint64_t dur = ts - age; \ + \ + if ((dur > (timeout))) \ + { \ + ArrayAdd(to_delete, StrDuplicate(field)); \ + } \ + } \ + \ + for (j = 0; j < ArraySize(to_delete); j++) \ + { \ + field = ArrayGet(to_delete, j); \ + JsonValueFree(HashMapDelete(field##s, field)); \ + Free(field); \ + } \ + ArrayFree(to_delete); \ + } \ + while (0) - for (j = 0; j < ArraySize(to_delete); j++) - { - stanza = ArrayGet(to_delete, j); - JsonValueFree(HashMapDelete(stanzas, stanza)); - Free(stanza); - } - ArrayFree(to_delete); + CleanupField(stanza, 5 MINUTES); + CleanupField(event, 5 MINUTES); + CleanupField(id, 5 MINUTES); DbUnlock(data->db, ref); } @@ -385,6 +394,7 @@ ParseePushStanza(ParseeData *data, char *chat_id, char *stanza_id, char *id, cha HashMapSet(obj, "stanza", JsonValueString(stanza_id)); HashMapSet(obj, "origin", JsonValueString(id)); HashMapSet(obj, "sender", JsonValueString(sender)); + HashMapSet(obj, "age", JsonValueInteger(age)); JsonValueFree(HashMapSet(events, ev, JsonValueObject(obj))); if (new_events) { @@ -402,6 +412,8 @@ ParseePushStanza(ParseeData *data, char *chat_id, char *stanza_id, char *id, cha obj = HashMapCreate(); HashMapSet(obj, "stanza", JsonValueString(stanza_id)); HashMapSet(obj, "event", JsonValueString(ev)); + HashMapSet(obj, "sender", JsonValueString(sender)); + HashMapSet(obj, "age", JsonValueInteger(age)); JsonValueFree(HashMapSet(ids, id, JsonValueObject(obj))); if (new_ids) { diff --git a/src/XMPPThread.c b/src/XMPPThread.c index 37c7375..8515aa8 100644 --- a/src/XMPPThread.c +++ b/src/XMPPThread.c @@ -115,7 +115,6 @@ MessageStanza(ParseeData *args, XMLElement *stanza) else if (replaced) { event_id = ParseeEventFromID(args, chat_id, replaced); - Log(LOG_WARNING, "RETRACTION REQUEST TO %s->%s", replaced, event_id); Free(ASSend( args->config, mroom_id, encoded, "m.room.message", MatrixCreateReplace(event_id, data->data)