about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
authorMarcoPolo-PasTonMolo <marcopolopastonmolo@protonmail.com>2022-07-05 00:06:04 +0300
committerMarcoPolo-PasTonMolo <marcopolopastonmolo@protonmail.com>2022-07-05 00:06:04 +0300
commit6429698f18b8b1fe3f8252402104c9701e62b620 (patch)
treefa9f8733fa8fb2d29a8aa7ccf703673c376466f7 /src/xmpp
parente9da69426527c242c72f88e74f82724c0ea20c39 (diff)
downloadprofani-tty-6429698f18b8b1fe3f8252402104c9701e62b620.tar.gz
Fix initial MAM not displaying
Did this by waiting for a batch of MAM messages to arrive before
prepending them to the buffer. Also limited the number of messages
to fetch to 10 so that the user gets more frequent updates.
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/iq.c47
-rw-r--r--src/xmpp/stanza.c30
-rw-r--r--src/xmpp/stanza.h1
3 files changed, 42 insertions, 36 deletions
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c
index d2ae7de2..098d19db 100644
--- a/src/xmpp/iq.c
+++ b/src/xmpp/iq.c
@@ -108,6 +108,7 @@ typedef struct mam_rsm_userdata
     char* barejid;
     char* start_datestr;
     char* end_datestr;
+    ProfChatWin* win;
 } MamRsmUserdata;
 
 static int _iq_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* const userdata);
@@ -2582,7 +2583,7 @@ _mam_buffer_commit_handler(xmpp_stanza_t* const stanza, void* const userdata)
     cons_show("Comitted history");
     // Remove the "Loading messages ..." message
     buffer_remove_entry(((ProfWin*)chatwin)->layout->buffer, 0);
-    chatwin_old_history(chatwin);
+    chatwin_old_history(chatwin, NULL);
     return 0;
 }
 
@@ -2630,39 +2631,32 @@ iq_mam_request(ProfChatWin* win)
     }
 
     ProfMessage* last_msg = log_database_get_limits_info(win->barejid, TRUE);
-    char* lastid = NULL;
-    char* firstid = NULL;
+    // To get last page and have flipped paging set firstid to empty string
+    char* firstid = "";
     char* startdate = NULL;
     char* enddate = NULL;
-    gboolean should_add_rsm_handler = TRUE;
 
     // If last message found
     if (last_msg->timestamp) {
-        lastid = last_msg->stanzaid;
         startdate = g_date_time_format(last_msg->timestamp, "%FT%T.%f%:z");
-
     } else {
         GDateTime* now = g_date_time_new_now_utc();
         enddate = g_date_time_format(now, "%FT%T.%f%:z");
         g_date_time_unref(now);
-        // To get last page we need to set before to empty string
-        firstid = "";
-        should_add_rsm_handler = FALSE;
     }
 
     xmpp_ctx_t* const ctx = connection_get_ctx();
 
-    xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, win->barejid, startdate, enddate, firstid, lastid);
+    xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, win->barejid, startdate, enddate, firstid, NULL);
 
-    if (should_add_rsm_handler) {
-        MamRsmUserdata* data = malloc(sizeof(MamRsmUserdata));
-        if (data) {
-            data->start_datestr = startdate;
-            data->end_datestr = enddate;
-            data->barejid = strdup(win->barejid);
+    MamRsmUserdata* data = malloc(sizeof(MamRsmUserdata));
+    if (data) {
+        data->start_datestr = startdate;
+        data->end_datestr = enddate;
+        data->barejid = strdup(win->barejid);
+        data->win = win;
 
-            iq_id_handler_add(xmpp_stanza_get_id(iq), _mam_rsm_id_handler, NULL, data);
-        }
+        iq_id_handler_add(xmpp_stanza_get_id(iq), _mam_rsm_id_handler, NULL, data);
     }
 
     message_free(last_msg);
@@ -2686,24 +2680,25 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
         xmpp_stanza_t* fin = xmpp_stanza_get_child_by_name_and_ns(stanza, STANZA_NAME_FIN, STANZA_NS_MAM2);
         if (fin) {
             gboolean is_complete = g_strcmp0(xmpp_stanza_get_attribute(fin, "complete"), "true") == 0;
+            MamRsmUserdata* data = (MamRsmUserdata*)userdata;
 
-            if (is_complete) {
+            if (is_complete || data->end_datestr) {
+                chatwin_old_history(data->win, is_complete ? NULL : data->start_datestr);
                 return 0;
             }
+            chatwin_old_history(data->win, data->start_datestr);
 
             xmpp_stanza_t* set = xmpp_stanza_get_child_by_name_and_ns(fin, STANZA_TYPE_SET, STANZA_NS_RSM);
             if (set) {
-                char* lastid = NULL;
-                xmpp_stanza_t* last = xmpp_stanza_get_child_by_name(set, STANZA_NAME_LAST);
-                lastid = xmpp_stanza_get_text(last);
+                char* firstid = NULL;
+                xmpp_stanza_t* first = xmpp_stanza_get_child_by_name(set, STANZA_NAME_FIRST);
+                firstid = xmpp_stanza_get_text(first);
 
                 // 4.3.2. send same stanza with set,max stanza
                 xmpp_ctx_t* const ctx = connection_get_ctx();
 
-                MamRsmUserdata* data = (MamRsmUserdata*)userdata;
-
-                xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, data->barejid, data->start_datestr, data->end_datestr, NULL, lastid);
-                free(lastid);
+                xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, data->barejid, data->start_datestr, data->end_datestr, firstid, NULL);
+                free(firstid);
 
                 iq_id_handler_add(xmpp_stanza_get_id(iq), _mam_rsm_id_handler, NULL, data);
 
diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c
index 86791d94..4d56ebf4 100644
--- a/src/xmpp/stanza.c
+++ b/src/xmpp/stanza.c
@@ -58,6 +58,7 @@
 #include "xmpp/connection.h"
 #include "xmpp/form.h"
 #include "xmpp/muc.h"
+#include "database.h"
 
 static void _stanza_add_unique_id(xmpp_stanza_t* stanza);
 static char* _stanza_create_sha1_hash(char* str);
@@ -2780,12 +2781,21 @@ stanza_create_mam_iq(xmpp_ctx_t* ctx, const char* const jid, const char* const s
     }
 
     // 4.3.2 set/rsm
-    xmpp_stanza_t *set;
-    if (lastid || firstid) {
-        set = xmpp_stanza_new(ctx);
-        xmpp_stanza_set_name(set, STANZA_TYPE_SET);
-        xmpp_stanza_set_ns(set, STANZA_NS_RSM);
-    }
+    xmpp_stanza_t *set, *max, *max_text;
+    set = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(set, STANZA_TYPE_SET);
+    xmpp_stanza_set_ns(set, STANZA_NS_RSM);
+
+    max = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(max, STANZA_NAME_MAX);
+
+    max_text = xmpp_stanza_new(ctx);
+    char* txt = g_strdup_printf("%d", MESSAGES_TO_RETRIEVE);
+    xmpp_stanza_set_text(max_text, txt);
+    g_free(txt);
+
+    xmpp_stanza_add_child(max, max_text);
+    xmpp_stanza_add_child(set, max);
 
     xmpp_stanza_t *after, *after_text;
     if (lastid) {
@@ -2831,10 +2841,10 @@ stanza_create_mam_iq(xmpp_ctx_t* ctx, const char* const jid, const char* const s
         xmpp_stanza_release(end_date_text);
     }
 
-    if (firstid || lastid) {
-        xmpp_stanza_add_child(query, set);
-        xmpp_stanza_release(set);
-    }
+    xmpp_stanza_add_child(query, set);
+    xmpp_stanza_release(set);
+    xmpp_stanza_release(max_text);
+    xmpp_stanza_release(max);
 
     if (lastid) {
         xmpp_stanza_release(after_text);
diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h
index 71454e9d..4d41ef87 100644
--- a/src/xmpp/stanza.h
+++ b/src/xmpp/stanza.h
@@ -118,6 +118,7 @@
 #define STANZA_NAME_FIRST            "first"
 #define STANZA_NAME_AFTER            "after"
 #define STANZA_NAME_BEFORE           "before"
+#define STANZA_NAME_MAX              "max"
 #define STANZA_NAME_USERNAME         "username"
 #define STANZA_NAME_PROPOSE          "propose"
 #define STANZA_NAME_REPORT           "report"