1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Get messages from history when scrolling up.

This commit is contained in:
MarcoPolo-PasTonMolo 2022-07-03 21:29:36 +03:00
parent f0202a2fe0
commit ea83165a35
9 changed files with 106 additions and 9 deletions

View File

@ -232,8 +232,9 @@ log_database_get_limits_info(const gchar* const contact_barejid, gboolean is_las
return msg;
}
// Query previous chat
GSList*
log_database_get_previous_chat(const gchar* const contact_barejid)
log_database_get_previous_chat(const gchar* const contact_barejid, GDateTime* end_time, gboolean flip)
{
sqlite3_stmt* stmt = NULL;
gchar* query;
@ -242,12 +243,16 @@ log_database_get_previous_chat(const gchar* const contact_barejid)
if (!myjid)
return 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') ORDER BY `timestamp` DESC LIMIT 10) ORDER BY `timestamp` ASC;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid);
// Flip order when querying older pages
char* sort = !flip ? "ASC" : "DESC";
gchar* date_fmt = g_date_time_format_iso8601(end_time ? end_time : g_date_time_new_now_local());
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' ORDER BY `timestamp` DESC LIMIT 10) ORDER BY `timestamp` %s;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid, date_fmt, sort);
if (!query) {
log_error("log_database_get_previous_chat(): SQL query. could not allocate memory");
return NULL;
}
g_free(date_fmt);
jid_destroy(myjid);
int rc = sqlite3_prepare_v2(g_chatlog_database, query, -1, &stmt, NULL);

View File

@ -45,7 +45,7 @@ void log_database_add_incoming(ProfMessage* message);
void log_database_add_outgoing_chat(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc);
void log_database_add_outgoing_muc(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc);
void log_database_add_outgoing_muc_pm(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc);
GSList* log_database_get_previous_chat(const gchar* const contact_barejid);
GSList* log_database_get_previous_chat(const gchar* const contact_barejid, GDateTime* end_time, gboolean flip);
ProfMessage* log_database_get_limits_info(const gchar* const contact_barejid, gboolean is_last);
void log_database_close(void);

View File

@ -110,6 +110,34 @@ buffer_append(ProfBuff buffer, const char* show_char, int pad_indent, GDateTime*
buffer->entries = g_slist_append(buffer->entries, e);
}
void
buffer_prepend(ProfBuff buffer, const char* show_char, int pad_indent, GDateTime* time, int flags, theme_item_t theme_item, const char* const display_from, const char* const from_jid, const char* const message, DeliveryReceipt* receipt, const char* const id)
{
ProfBuffEntry* e = malloc(sizeof(struct prof_buff_entry_t));
e->show_char = strdup(show_char);
e->pad_indent = pad_indent;
e->flags = flags;
e->theme_item = theme_item;
e->time = g_date_time_ref(time);
e->display_from = display_from ? strdup(display_from) : NULL;
e->from_jid = from_jid ? strdup(from_jid) : NULL;
e->message = strdup(message);
e->receipt = receipt;
if (id) {
e->id = strdup(id);
} else {
e->id = NULL;
}
if (g_slist_length(buffer->entries) == BUFF_SIZE) {
GSList* last = g_slist_last(buffer->entries);
_free_entry(last->data);
buffer->entries = g_slist_delete_link(buffer->entries, last);
}
buffer->entries = g_slist_prepend(buffer->entries, e);
}
void
buffer_remove_entry_by_id(ProfBuff buffer, const char* const id)
{

View File

@ -70,6 +70,7 @@ typedef struct prof_buff_t* ProfBuff;
ProfBuff buffer_create();
void buffer_free(ProfBuff buffer);
void buffer_append(ProfBuff buffer, const char* show_char, int pad_indent, GDateTime* time, int flags, theme_item_t theme_item, const char* const display_from, const char* const barejid, const char* const message, DeliveryReceipt* receipt, const char* const id);
void buffer_prepend(ProfBuff buffer, const char* show_char, int pad_indent, GDateTime* time, int flags, theme_item_t theme_item, const char* const display_from, const char* const barejid, const char* const message, DeliveryReceipt* receipt, const char* const id);
void buffer_remove_entry_by_id(ProfBuff buffer, const char* const id);
int buffer_size(ProfBuff buffer);
ProfBuffEntry* buffer_get_entry(ProfBuff buffer, int entry);

View File

@ -66,7 +66,7 @@ chatwin_new(const char* const barejid)
ProfWin* window = wins_new_chat(barejid);
ProfChatWin* chatwin = (ProfChatWin*)window;
if (!prefs_get_boolean(PREF_MAM) && prefs_get_boolean(PREF_CHLOG) && prefs_get_boolean(PREF_HISTORY)) {
if (prefs_get_boolean(PREF_MAM) || (prefs_get_boolean(PREF_CHLOG) && prefs_get_boolean(PREF_HISTORY))) {
_chatwin_history(chatwin, barejid);
}
@ -518,7 +518,7 @@ static void
_chatwin_history(ProfChatWin* chatwin, const char* const contact_barejid)
{
if (!chatwin->history_shown) {
GSList* history = log_database_get_previous_chat(contact_barejid);
GSList* history = log_database_get_previous_chat(contact_barejid, NULL, FALSE);
GSList* curr = history;
while (curr) {
@ -537,6 +537,32 @@ _chatwin_history(ProfChatWin* chatwin, const char* const contact_barejid)
}
}
gboolean
chatwin_old_history(ProfChatWin* chatwin)
{
// TODO: not correct location but check whether notifications get screwed
GDateTime* time = buffer_size(((ProfWin*)chatwin)->layout->buffer) == 0 ? NULL : buffer_get_entry(((ProfWin*)chatwin)->layout->buffer, 0)->time;
GSList* history = log_database_get_previous_chat(chatwin->barejid, time, TRUE);
gboolean has_items = g_slist_length(history) != 0;
GSList* curr = history;
while (curr) {
ProfMessage* msg = curr->data;
char* msg_plain = msg->plain;
msg->plain = plugins_pre_chat_message_display(msg->from_jid->barejid, msg->from_jid->resourcepart, msg->plain);
// This is dirty workaround for memory leak. We reassign msg->plain above so have to free previous object
// TODO: Make a better solution, for example, pass msg object to the function and it will replace msg->plain properly if needed.
free(msg_plain);
win_print_old_history((ProfWin*)chatwin, msg);
curr = g_slist_next(curr);
}
g_slist_free_full(history, (GDestroyNotify)message_free);
win_redraw((ProfWin*)chatwin);
return has_items;
}
static void
_chatwin_set_last_message(ProfChatWin* chatwin, const char* const id, const char* const message)
{

View File

@ -145,6 +145,7 @@ void chatwin_set_incoming_char(ProfChatWin* chatwin, const char* const ch);
void chatwin_unset_incoming_char(ProfChatWin* chatwin);
void chatwin_set_outgoing_char(ProfChatWin* chatwin, const char* const ch);
void chatwin_unset_outgoing_char(ProfChatWin* chatwin);
gboolean chatwin_old_history(ProfChatWin* chatwin);
// MUC window
ProfMucWin* mucwin_new(const char* const barejid);

View File

@ -600,6 +600,10 @@ win_page_up(ProfWin* window)
*page_start -= page_space;
if (*page_start == -page_space && prefs_get_boolean(PREF_MAM) && window->type == WIN_CHAT) {
ProfChatWin* chatwin = (ProfChatWin*) window;
chatwin_old_history(chatwin);
}
// went past beginning, show first page
if (*page_start < 0)
*page_start = 0;
@ -1289,6 +1293,34 @@ win_print_history(ProfWin* window, const ProfMessage* const message)
g_date_time_unref(message->timestamp);
}
void
win_print_old_history(ProfWin* window, const ProfMessage* const message)
{
g_date_time_ref(message->timestamp);
char* display_name;
int flags = 0;
const char* jid = connection_get_fulljid();
Jid* jidp = jid_create(jid);
if (g_strcmp0(jidp->barejid, message->from_jid->barejid) == 0) {
display_name = strdup("me");
} else {
display_name = roster_get_msg_display_name(message->from_jid->barejid, message->from_jid->resourcepart);
flags = NO_ME;
}
jid_destroy(jidp);
buffer_prepend(window->layout->buffer, "-", 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, NULL, message->plain, NULL, NULL);
_win_print_internal(window, "-", 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, message->plain, NULL);
free(display_name);
inp_nonblocking(TRUE);
g_date_time_unref(message->timestamp);
}
void
win_print(ProfWin* window, theme_item_t theme_item, const char* show_char, const char* const message, ...)
{
@ -1504,9 +1536,10 @@ _win_printf(ProfWin* window, const char* show_char, int pad_indent, GDateTime* t
GString* fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);
buffer_append(window->layout->buffer, show_char, pad_indent, timestamp, flags, theme_item, display_from, from_jid, fmt_msg->str, NULL, message_id);
_win_print_internal(window, show_char, pad_indent, timestamp, flags, theme_item, display_from, fmt_msg->str, NULL);
if (buffer_size(window->layout->buffer) == 0 || g_date_time_compare(buffer_get_entry(window->layout->buffer, 0)->time, timestamp) != 1) {
buffer_append(window->layout->buffer, show_char, pad_indent, timestamp, flags, theme_item, display_from, from_jid, fmt_msg->str, NULL, message_id);
_win_print_internal(window, show_char, pad_indent, timestamp, flags, theme_item, display_from, fmt_msg->str, NULL);
}
inp_nonblocking(TRUE);
g_date_time_unref(timestamp);

View File

@ -71,6 +71,7 @@ void win_print_outgoing_with_receipt(ProfWin* window, const char* show_char, con
void win_println_incoming_muc_msg(ProfWin* window, char* show_char, int flags, const ProfMessage* const message);
void win_print_outgoing_muc_msg(ProfWin* window, char* show_char, const char* const me, const char* const id, const char* const replace_id, const char* const message);
void win_print_history(ProfWin* window, const ProfMessage* const message);
void win_print_old_history(ProfWin* window, const ProfMessage* const message);
void win_print_http_transfer(ProfWin* window, const char* const message, char* url);

View File

@ -66,6 +66,7 @@
#include "xmpp/roster_list.h"
#include "xmpp/roster.h"
#include "xmpp/muc.h"
#include "src/database.h"
#ifdef HAVE_OMEMO
#include "omemo/omemo.h"
@ -105,7 +106,8 @@ typedef struct command_config_data_t
typedef struct mam_rsm_userdata
{
char* barejid;
char* datestr;
char* start_datestr;
char* end_datestr;
} MamRsmUserdata;
static int _iq_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* const userdata);