Commit a8e51011 authored by Mohammed Sadiq's avatar Mohammed Sadiq
Browse files

history: Pass ChattyMessage to add a new message

instead of passing each argument separately.

This will help simplify and avoid some duplicate code.

Also, eventually we shall make use of more ChattyMessage
features in the future.
parent 1e8cdafe
Pipeline #60874 passed with stages
in 11 minutes and 8 seconds
......@@ -1856,62 +1856,6 @@ history_exists (ChattyHistory *self,
g_task_return_boolean (task, found);
}
static void
history_add_raw_message (ChattyHistory *self,
GTask *task)
{
const char *account, *who, *msg, *uid;
sqlite3_stmt *stmt;
ChattyChat *chat;
int status, time_stamp, direction;
int thread_id, sender_id;
g_assert (CHATTY_IS_HISTORY (self));
g_assert (G_IS_TASK (task));
if (!self->db) {
g_task_return_new_error (task,
G_IO_ERROR, G_IO_ERROR_FAILED,
"Error: Database not opened");
return;
}
chat = g_object_get_data (G_OBJECT (task), "chat");
thread_id = insert_or_ignore_thread (self, chat, task);
if (!thread_id)
return;
account = chatty_chat_get_username (chat);
who = g_object_get_data (G_OBJECT (task), "who");
msg = g_object_get_data (G_OBJECT (task), "message");
uid = g_object_get_data (G_OBJECT (task), "uid");
time_stamp = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "time"));
direction = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "dir"));
g_assert (account);
g_assert (uid);
sender_id = insert_or_ignore_user (self, chatty_item_get_protocols (CHATTY_ITEM (chat)), who, task);
status = sqlite3_prepare_v2 (self->db,
"INSERT INTO messages(uid,thread_id,sender_id,body,body_type,direction,time) "
"VALUES(?1,?2,?3,?4," STRING (MESSAGE_TYPE_HTML) ",?5,?6);",
-1, &stmt, NULL);
history_bind_text (stmt, 1, uid, "binding when adding message");
history_bind_int (stmt, 2, thread_id, "binding when adding message");
if (sender_id)
history_bind_int (stmt, 3, sender_id, "binding when adding message");
history_bind_text (stmt, 4, msg, "binding when adding message");
history_bind_int (stmt, 5, direction, "binding when adding message");
history_bind_int (stmt, 6, time_stamp, "binding when adding message");
status = sqlite3_step (stmt);
sqlite3_finalize (stmt);
g_task_return_boolean (task, status == SQLITE_DONE);
}
static gpointer
chatty_history_worker (gpointer user_data)
{
......@@ -2652,52 +2596,15 @@ chatty_history_chat_exists (const char *account,
* %FALSE otherwise.
*/
gboolean
chatty_history_add_message (ChattyChat *chat,
const char *who,
const char *message,
char **uid,
PurpleMessageFlags flags,
time_t time_stamp)
chatty_history_add_message (ChattyChat *chat,
ChattyMessage *message)
{
ChattyHistory *self;
g_autoptr(GTask) task = NULL;
int dir = 0;
g_return_val_if_fail (uid, FALSE);
g_return_val_if_fail (CHATTY_IS_CHAT (chat), FALSE);
self = chatty_history_get_default ();
g_return_val_if_fail (self->db, FALSE);
/* Don’t save if marked so */
if (flags & PURPLE_MESSAGE_NO_LOG)
return FALSE;
/* direction of message */
if (flags & PURPLE_MESSAGE_SYSTEM)
dir = 0;
else if (flags & PURPLE_MESSAGE_RECV)
dir = 1;
else if (flags & PURPLE_MESSAGE_SEND)
dir = -1;
if (flags & PURPLE_MESSAGE_SEND)
who = chatty_chat_get_username (chat);
if (!*uid)
*uid = g_uuid_string_random ();
task = g_task_new (NULL, NULL, NULL, NULL);
g_object_ref (task);
g_task_set_task_data (task, history_add_raw_message, NULL);
g_object_set_data_full (G_OBJECT (task), "chat", g_object_ref (chat), g_object_unref);
g_object_set_data_full (G_OBJECT (task), "who", g_strdup (who), g_free);
g_object_set_data_full (G_OBJECT (task), "message", g_strdup (message), g_free);
g_object_set_data_full (G_OBJECT (task), "uid", g_strdup (*uid), g_free);
g_object_set_data (G_OBJECT (task), "dir", GINT_TO_POINTER (dir));
g_object_set_data (G_OBJECT (task), "time", GINT_TO_POINTER (time_stamp));
g_async_queue_push (self->queue, task);
chatty_history_add_message_async (self, chat, message, finish_cb, task);
/* Wait until the task is completed */
while (!g_task_get_completed (task))
......
......@@ -79,10 +79,6 @@ gboolean chatty_history_im_exists (const char *accoun
gboolean chatty_history_chat_exists (const char *account,
const char *room);
gboolean chatty_history_add_message (ChattyChat *chat,
const char *who,
const char *message,
char **uid,
PurpleMessageFlags flags,
time_t time_stamp);
ChattyMessage *message);
G_END_DECLS
......@@ -869,22 +869,8 @@ chatty_conv_write_conversation (PurpleConversation *conv,
g_debug("Posting message id:%s flags:%d type:%d from:%s",
uuid, pcm.flags, type, pcm.who);
/*
* This is default fallback history handler. Other plugins may
* intercept “conversation-write” and suppress it if they handle
* history on their own (eg. MAM). If %PURPLE_MESSAGE_NO_LOG is
* set in @flags, it won't be saved to database.
*/
if (!(pcm.flags & PURPLE_MESSAGE_NO_LOG)) {
const char *chat_name;
chat_name = pcm.who;
if (chatty_chat_is_im (chat))
chat_name = chatty_chat_get_chat_name (chat);
chatty_history_add_message (chat, chat_name, pcm.what, &uuid, pcm.flags, pcm.when);
}
if (!uuid)
uuid = g_uuid_string_random ();
if (*message != '\0') {
......@@ -936,6 +922,18 @@ chatty_conv_write_conversation (PurpleConversation *conv,
chatty_pp_chat_append_message (CHATTY_PP_CHAT (chat), chat_message);
}
if (chat_message && pcm.who && !(flags & PURPLE_MESSAGE_SEND))
chatty_message_set_user_name (chat_message, pcm.who);
/*
* This is default fallback history handler. Other plugins may
* intercept “conversation-write” and suppress it if they handle
* history on their own (eg. MAM). If %PURPLE_MESSAGE_NO_LOG is
* set in @flags, it won't be saved to database.
*/
if (!(pcm.flags & PURPLE_MESSAGE_NO_LOG) && chat_message)
chatty_history_add_message (chat, chat_message);
chatty_chat_set_unread_count (chat, chatty_chat_get_unread_count (chat) + 1);
gtk_sorter_changed (self->chat_sorter, GTK_SORTER_ORDER_TOTAL);
}
......
......@@ -790,8 +790,14 @@ cb_chatty_mam_msg_received (PurpleConnection *pc,
who = chatty_utils_jabber_id_strip (pcm->who);
}
chatty_history_add_message (conv->ui_data, pcm->who, pcm->what,
(char**)&stanza_id, pcm->flags, pcm->when);
if (!stanza_id)
stanza_id = g_uuid_string_random ();
chat_message = chatty_message_new (NULL, NULL, pcm->what, stanza_id, pcm->when,
chatty_utils_direction_from_flag (pcm->flags), 0);
if (chat_message && pcm->who && !(flags & PURPLE_MESSAGE_SEND))
chatty_message_set_user_name (chat_message, who ? who : pcm->who);
chatty_history_add_message (conv->ui_data, chat_message);
}
// Update last timestamp for account's archive
if(mamq != NULL && mamq->to == NULL)
......
......@@ -22,10 +22,12 @@
#include "purple-init.h"
#include "chatty-settings.h"
#include "chatty-utils.h"
#include "chatty-history.h"
typedef struct Message {
ChattyChat *chat;
ChattyMessage *message;
char *account;
char *room;
char *who;
......@@ -71,6 +73,7 @@ free_message (Message *msg)
{
g_assert_true (msg);
g_clear_object (&msg->chat);
g_clear_object (&msg->message);
g_free (msg->account);
g_free (msg->who);
g_free (msg->what);
......@@ -255,12 +258,27 @@ new_message (const char *account,
time_t time_stamp,
const char *room)
{
g_autoptr(ChattyContact) contact = NULL;
ChattyChat *chat;
Message *message;
ChattyMsgDirection direction = CHATTY_DIRECTION_UNKNOWN;
if (flags & PURPLE_MESSAGE_SYSTEM)
direction = CHATTY_DIRECTION_SYSTEM;
else if (flags & PURPLE_MESSAGE_RECV)
direction = CHATTY_DIRECTION_IN;
else if (flags & PURPLE_MESSAGE_SEND)
direction = CHATTY_DIRECTION_OUT;
chat = chatty_chat_new (account, room ? room : buddy, room == NULL);
g_object_set (G_OBJECT (chat), "protocols", CHATTY_PROTOCOL_XMPP, NULL);
message = g_new (Message, 1);
contact = g_object_new (CHATTY_TYPE_CONTACT,
"protocols", CHATTY_PROTOCOL_XMPP,
NULL);
chatty_contact_set_value (contact, buddy);
message->message = chatty_message_new (CHATTY_ITEM (contact), NULL, msg_text,
uuid, time_stamp, direction, 0);
message->account = g_strdup (account);
message->who = g_strdup (buddy);
message->what = g_strdup (msg_text);
......@@ -375,9 +393,12 @@ add_message (ChattyHistory *history,
else
type = PURPLE_CONV_TYPE_IM;
if (!uuid)
uuid = g_uuid_string_random ();
uid = g_strdup (uuid);
msg = new_message (account, who, message, uid, flags, when, room);
success = chatty_history_add_message (msg->chat, who, message, &uid, flags, when);
success = chatty_history_add_message (msg->chat, msg->message);
g_assert_true (success);
g_assert_nonnull (uid);
g_ptr_array_add (test_msg_array, msg);
......@@ -735,12 +756,21 @@ test_value (sqlite3 *db,
flags = flag_for_direction (direction);
if (statement_status == SQLITE_ROW) {
g_autoptr(ChattyChat) chat = NULL;
g_autoptr(ChattyMessage) chat_message = NULL;
ChattyMsgDirection chat_direction;
gboolean success;
chat = chatty_chat_new (account, room ? room : who, room == NULL);
g_object_set (G_OBJECT (chat), "protocols", CHATTY_PROTOCOL_XMPP, NULL);
success = chatty_history_add_message (chat, who, message, &uuid, flags, time_stamp);
if (!uuid)
uuid = g_uuid_string_random ();
chat_direction = chatty_utils_direction_from_flag (flags);
chat_message = chatty_message_new (NULL, NULL, message, uuid, time_stamp, chat_direction, 0);
if (chat_direction != CHATTY_DIRECTION_OUT)
chatty_message_set_user_name (chat_message, who);
success = chatty_history_add_message (chat, chat_message);
g_assert_true (success);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment