From 97a610e915d178ff136d3c93bdb3c7c6f97666ff Mon Sep 17 00:00:00 2001 From: MarcoPolo-PasTonMolo Date: Sun, 3 Jul 2022 21:30:02 +0300 Subject: [PATCH] Fech data from mam when all history gets displayed Fetch from mam without displaying when all mam messages get received display new messages from db. Unstable, initial mam doesn't get displayed unless we start scrolling. --- src/ui/window.c | 8 +++++++- src/xmpp/iq.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/xmpp/xmpp.h | 2 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/ui/window.c b/src/ui/window.c index b6130348..4794e5c0 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -602,8 +602,14 @@ win_page_up(ProfWin* window) if (*page_start == -page_space && prefs_get_boolean(PREF_MAM) && window->type == WIN_CHAT) { ProfChatWin* chatwin = (ProfChatWin*) window; - chatwin_old_history(chatwin); + if (!chatwin_old_history(chatwin)) { + cons_show("Fetched mam"); + iq_mam_request_older(chatwin); + } else { + cons_show("Showed history"); + } } + // went past beginning, show first page if (*page_start < 0) *page_start = 0; diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index dd28b358..d51560be 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -2575,6 +2575,49 @@ _iq_free_affiliation_list(ProfAffiliationList* affiliation_list) } } +static int +_mam_buffer_commit_handler(xmpp_stanza_t* const stanza, void* const userdata) +{ + ProfChatWin* chatwin = (ProfChatWin*)userdata; + cons_show("Comitted history"); + chatwin_old_history(chatwin); + return 0; +} + +void +iq_mam_request_older(ProfChatWin* win) +{ + if (connection_supports(XMPP_FEATURE_MAM2) == FALSE) { + log_warning("Server doesn't advertise %s feature.", XMPP_FEATURE_MAM2); + cons_show_error("Server doesn't support MAM (%s).", XMPP_FEATURE_MAM2); + return; + } + + ProfMessage* first_msg = log_database_get_limits_info(win->barejid, FALSE); + char* firstid = NULL; + char* enddate = NULL; + + // If first message found + if (first_msg->timestamp) { + firstid = first_msg->stanzaid; + enddate = g_date_time_format(first_msg->timestamp, "%FT%T.%f%:z"); + } else { + return; + } + + xmpp_ctx_t* const ctx = connection_get_ctx(); + xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, win->barejid, NULL, enddate, firstid, NULL); + iq_id_handler_add(xmpp_stanza_get_id(iq), _mam_buffer_commit_handler, NULL, win); + + g_free(enddate); + message_free(first_msg); + + iq_send_stanza(iq); + xmpp_stanza_release(iq); + + return; +} + void iq_mam_request(ProfChatWin* win) { diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 2cc22e55..6b1f21df 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -64,6 +64,7 @@ #define XMPP_FEATURE_USER_AVATAR_METADATA_NOTIFY "urn:xmpp:avatar:metadata+notify" #define XMPP_FEATURE_LAST_MESSAGE_CORRECTION "urn:xmpp:message-correct:0" #define XMPP_FEATURE_MAM2 "urn:xmpp:mam:2" +#define XMPP_FEATURE_MAM2_EXTENDED "urn:xmpp:mam:2#extended" #define XMPP_FEATURE_SPAM_REPORTING "urn:xmpp:reporting:1" typedef enum { @@ -261,6 +262,7 @@ void iq_http_upload_request(HTTPUpload* upload); void iq_command_list(const char* const target); void iq_command_exec(const char* const target, const char* const command); void iq_mam_request(ProfChatWin* win); +void iq_mam_request_older(ProfChatWin* win); void iq_register_change_password(const char* const user, const char* const password); void iq_muc_register_nick(const char* const roomjid);