1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-09 21:30:42 +00:00

Get regular chat history out of sql backend

This commit is contained in:
Michael Vetter 2020-04-06 12:11:08 +02:00
parent 20850eb4db
commit 5d54bb228f
3 changed files with 53 additions and 21 deletions

View File

@ -247,9 +247,14 @@ _add_to_db(ProfMessage *message, const char * const type, const Jid * const from
char *err_msg;
char *query;
gchar *date_fmt;
if (message->timestamp) {
date_fmt = g_date_time_format_iso8601(message->timestamp);
} else {
date_fmt = g_date_time_format_iso8601(g_date_time_new_now_local());
}
//gchar *date_fmt = g_date_time_format_iso8601(message->timestamp);
gchar *date_fmt = g_date_time_format(message->timestamp, "%Y/%m/%d %H:%M:%S");
if (asprintf(&query, "INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `replace_id`, `type`, `encryption`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
from_jid->barejid,
from_jid->resourcepart ? from_jid->resourcepart : "",
@ -276,3 +281,40 @@ _add_to_db(ProfMessage *message, const char * const type, const Jid * const from
}
free(query);
}
GSList*
log_database_get_previous_chat(const gchar *const login, const gchar *const recipient)
{
sqlite3_stmt *stmt = NULL;
char *query;
if (asprintf(&query, "SELECT `message`, `timestamp`, `from_jid` from `ChatLogs` WHERE `from_jid` = '%s' OR `to_jid` = '%s' ORDER BY `id` ASC LIMIT 10", recipient, recipient) == -1) {
log_error("log_database_get_previous_chat(): could not allocate memory");
return NULL;
}
int rc = sqlite3_prepare_v2(g_chatlog_database, query, -1, &stmt, NULL);
if( rc!=SQLITE_OK ) {
log_error("log_database_get_previous_chat(): unknown SQLite error");
return NULL;
}
GSList *history = NULL;
while( sqlite3_step(stmt) == SQLITE_ROW ) {
char *message = (char*)sqlite3_column_text(stmt, 0);
char *date = (char*)sqlite3_column_text(stmt, 1);
char *from = (char*)sqlite3_column_text(stmt, 2);
ProfMessage *msg = message_init();
msg->jid = jid_create(from);
msg->plain = strdup(message);
msg->timestamp = g_date_time_new_from_iso8601(date, NULL);
// TODO: later we can get more fields like 'enc'. then we can display the history like regular chats with all info the user enabled.
history = g_slist_append(history, msg);
}
sqlite3_finalize(stmt);
return history;
}

View File

@ -47,6 +47,7 @@ void log_database_add_incoming_muc_pm(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 login, const gchar *const recipient);
void log_database_close(void);
#endif // DATABASE_H

View File

@ -44,6 +44,7 @@
#include "window_list.h"
#include "xmpp/roster_list.h"
#include "log.h"
#include "database.h"
#include "config/preferences.h"
#include "ui/ui.h"
#include "ui/window.h"
@ -481,33 +482,21 @@ _chatwin_history(ProfChatWin *chatwin, const char *const contact)
{
if (!chatwin->history_shown) {
Jid *jid = jid_create(connection_get_fulljid());
GSList *history = chat_log_get_previous(jid->barejid, contact);
//GSList *history = chat_log_get_previous(jid->barejid, contact);
// TODO: jid not needed
GSList *history = log_database_get_previous_chat(jid->barejid, contact);
jid_destroy(jid);
GSList *curr = history;
int idd = 0;
int imo = 0;
int iyy = 0;
while (curr) {
char *line = curr->data;
// entry, containing the actual entries with date followed by text
if (line[2] == ':') {
char hh[3]; memcpy(hh, &line[0], 2); hh[2] = '\0'; int ihh = atoi(hh);
char mm[3]; memcpy(mm, &line[3], 2); mm[2] = '\0'; int imm = atoi(mm);
char ss[3]; memcpy(ss, &line[6], 2); ss[2] = '\0'; int iss = atoi(ss);
GDateTime *timestamp = g_date_time_new_local(iyy, imo, idd, ihh, imm, iss);
win_print_history((ProfWin*)chatwin, timestamp, curr->data+11);
g_date_time_unref(timestamp);
// header, containing the date from filename "21/10/2019:"
} else {
char dd[3]; memcpy(dd, &line[0], 2); dd[2] = '\0'; idd = atoi(dd);
char mm[3]; memcpy(mm, &line[3], 2); mm[2] = '\0'; imo = atoi(mm);
char yy[5]; memcpy(yy, &line[6], 4); yy[4] = '\0'; iyy = atoi(yy);
}
ProfMessage *msg = curr->data;
// TODO: sender is lost right now
win_print_history((ProfWin*)chatwin, msg->timestamp, msg->plain);
curr = g_slist_next(curr);
}
chatwin->history_shown = TRUE;
//TODO: message_free
g_slist_free_full(history, free);
}
}