diff --git a/src/database.c b/src/database.c index ea971d4e..966f6ca4 100644 --- a/src/database.c +++ b/src/database.c @@ -246,7 +246,8 @@ log_database_get_previous_chat(const gchar* const contact_barejid, char* start_t // Flip order when querying older pages gchar* sort = !flip ? "ASC" : "DESC"; - gchar* end_date_fmt = end_time ? end_time : g_date_time_format_iso8601(g_date_time_new_now_local()); + GDateTime* now = g_date_time_new_now_local(); + gchar* end_date_fmt = end_time ? end_time : g_date_time_format_iso8601(now); gchar* start_date_fmt = start_time ? start_time : NULL; query = sqlite3_mprintf("SELECT * FROM (SELECT `message`, `timestamp`, `from_jid`, `type` from `ChatLogs` WHERE ((`from_jid` = '%q' AND `to_jid` = '%q') OR (`from_jid` = '%q' AND `to_jid` = '%q')) AND `timestamp` < '%q' AND (%Q IS NULL OR `timestamp` > %Q) ORDER BY `timestamp` DESC LIMIT %d) ORDER BY `timestamp` %s;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid, end_date_fmt, start_date_fmt, start_date_fmt, MESSAGES_TO_RETRIEVE, sort); if (!query) { @@ -254,6 +255,7 @@ log_database_get_previous_chat(const gchar* const contact_barejid, char* start_t return NULL; } + g_date_time_unref(now); g_free(end_date_fmt); jid_destroy(myjid); diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 82df1e80..a89e4626 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -102,6 +102,7 @@ chatwin_new(const char* const barejid) if (prefs_get_boolean(PREF_MAM)) { iq_mam_request(chatwin); + win_print_loading_history(window); } return chatwin; diff --git a/src/ui/window.c b/src/ui/window.c index 391aec21..e168b70c 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -610,8 +610,7 @@ win_page_up(ProfWin* window) if (first_entry && !(first_entry->theme_item == THEME_ROOMINFO && g_strcmp0(first_entry->message, loading_text) == 0)) { if (!chatwin_old_history(chatwin, NULL)) { cons_show("Fetched mam"); - buffer_prepend(window->layout->buffer, "-", 0, first_entry->time, NO_DATE, THEME_ROOMINFO, NULL, NULL, loading_text, NULL, NULL); - win_redraw(window); + win_print_loading_history(window); iq_mam_request_older(chatwin); } else { cons_show("Showed history"); @@ -1855,6 +1854,15 @@ win_redraw(ProfWin* window) } } +void +win_print_loading_history(ProfWin* window) +{ + char* loading_text = "Loading older messages ..."; + GDateTime* timestamp = buffer_size(window->layout->buffer) != 0 ? buffer_get_entry(window->layout->buffer, 0)->time : g_date_time_new_now_local(); + buffer_prepend(window->layout->buffer, "-", 0, timestamp, NO_DATE, THEME_ROOMINFO, NULL, NULL, loading_text, NULL, NULL); + win_redraw(window); +} + gboolean win_has_active_subwin(ProfWin* window) { diff --git a/src/ui/window.h b/src/ui/window.h index 174785b1..822f12b0 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -77,6 +77,7 @@ void win_print_http_transfer(ProfWin* window, const char* const message, char* u void win_newline(ProfWin* window); void win_redraw(ProfWin* window); +void win_print_loading_history(ProfWin* window); int win_roster_cols(void); int win_occpuants_cols(void); void win_sub_print(WINDOW* win, char* msg, gboolean newline, gboolean wrap, int indent); diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 098d19db..1c851cbd 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -67,6 +67,7 @@ #include "xmpp/roster.h" #include "xmpp/muc.h" #include "src/database.h" +#include "ui/window.h" #ifdef HAVE_OMEMO #include "omemo/omemo.h" @@ -2681,7 +2682,9 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata) if (fin) { gboolean is_complete = g_strcmp0(xmpp_stanza_get_attribute(fin, "complete"), "true") == 0; MamRsmUserdata* data = (MamRsmUserdata*)userdata; + ProfWin* window = (ProfWin*)data->win; + buffer_remove_entry(window->layout->buffer, 0); if (is_complete || data->end_datestr) { chatwin_old_history(data->win, is_complete ? NULL : data->start_datestr); return 0; @@ -2690,6 +2693,8 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata) xmpp_stanza_t* set = xmpp_stanza_get_child_by_name_and_ns(fin, STANZA_TYPE_SET, STANZA_NS_RSM); if (set) { + win_print_loading_history(window); + char* firstid = NULL; xmpp_stanza_t* first = xmpp_stanza_get_child_by_name(set, STANZA_NAME_FIRST); firstid = xmpp_stanza_get_text(first);