Commit 31fc7e9c authored by Amin Bandali's avatar Amin Bandali Committed by Sébastien Blin

swarm: add support for lazy loading conversations

depends on https://review.jami.net/c/ring-lrc/+/18779

GitLab: #1268
Change-Id: Id8b65caaadc7cdfd9b40ad2dfcdb9b4e0fac887f
parent 3c788b78
......@@ -86,6 +86,7 @@ struct _ChatViewPrivate
QMetaObject::Connection composing_changed_connection;
QMetaObject::Connection interaction_removed;
QMetaObject::Connection update_interaction_connection;
QMetaObject::Connection new_messages_available_connection;
QMetaObject::Connection update_plugin_status;
QMetaObject::Connection update_add_to_conversations;
QMetaObject::Connection local_renderer_connection;
......@@ -259,6 +260,7 @@ chat_view_dispose(GObject *object)
QObject::disconnect(priv->new_interaction_connection);
QObject::disconnect(priv->composing_changed_connection);
QObject::disconnect(priv->update_interaction_connection);
QObject::disconnect(priv->new_messages_available_connection);
QObject::disconnect(priv->update_plugin_status);
QObject::disconnect(priv->interaction_removed);
QObject::disconnect(priv->update_add_to_conversations);
......@@ -704,6 +706,18 @@ reset_recorder(ChatView *self)
}
void
load_messages(lrc::api::ConversationModel& conversation_model,
const QString& convUid,
int n)
{
auto convOpt = conversation_model.getConversationForUid(convUid);
if (!convOpt)
return;
if (convOpt->get().isSwarm() && !convOpt->get().allMessagesLoaded)
conversation_model.loadConversationMessages(convOpt->get().uid, n);
}
static void
webkit_chat_container_script_dialog(GtkWidget* webview, gchar *interaction, ChatView* self)
{
......@@ -872,6 +886,11 @@ webkit_chat_container_script_dialog(GtkWidget* webview, gchar *interaction, Chat
} catch (...) {
// ignore
}
} else if (order.find("LOAD_MESSAGES:") == 0) {
auto n = stoi(order.substr(std::string("LOAD_MESSAGES:").size()));
load_messages(*(*priv->accountInfo_)->conversationModel,
priv->conversation_->uid,
n);
}
}
......@@ -1076,6 +1095,19 @@ webkit_chat_container_ready(ChatView* self)
);
webkit_chat_set_is_swarm(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), priv->conversation_->isSwarm());
auto *convModel = (*priv->accountInfo_)->conversationModel.get();
auto optConv = convModel->getConversationForUid(priv->conversation_->uid);
if (!optConv)
return;
if (optConv->get().isSwarm() && !optConv->get().allMessagesLoaded) {
convModel->loadConversationMessages(optConv->get().uid, 20);
} else {
webkit_chat_container_print_history(
WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container),
*convModel,
optConv->get().interactions);
}
display_links_toggled(self);
print_text_recording(self);
load_participants_images(self);
......@@ -1106,6 +1138,20 @@ webkit_chat_container_ready(ChatView* self)
}
});
priv->new_messages_available_connection = QObject::connect(
&*(*priv->accountInfo_)->conversationModel, &lrc::api::ConversationModel::newMessagesAvailable,
[self, priv](const QString& accountId, const QString& conversationId) {
auto *convModel = (*priv->accountInfo_)->conversationModel.get();
auto optConv = convModel->getConversationForUid(conversationId);
if (!optConv)
return;
webkit_chat_container_update_history(
WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container),
*convModel,
optConv->get().interactions,
optConv->get().allMessagesLoaded);
});
if (!priv->conversation_) return;
auto contactUri = priv->conversation_->participants.front();
try {
......
......@@ -159,8 +159,7 @@ build_view(ProfileView* view)
g_free(surface);
g_free(cr);
auto is_swarm = convOpt->get().mode != lrc::api::conversation::Mode::NON_SWARM;
gtk_label_set_text(GTK_LABEL(priv->is_swarm_label), is_swarm ? _("Yes") : _("No"));
gtk_label_set_text(GTK_LABEL(priv->is_swarm_label), convOpt->get().isSwarm() ? _("Yes") : _("No"));
gtk_window_set_title(GTK_WINDOW(view), std::string("Profile - " + alias.toStdString()).c_str());
gtk_window_set_modal(GTK_WINDOW(view), false);
......
......@@ -754,6 +754,20 @@ webkit_chat_container_print_history(WebKitChatContainer *view,
g_free(function_call);
}
void
webkit_chat_container_update_history(WebKitChatContainer *view,
lrc::api::ConversationModel& conversation_model,
lrc::api::MessagesList interactions,
bool all_loaded)
{
auto interactions_str = interactions_to_json_array_object(conversation_model, interactions).toUtf8();
gchar* function_call = g_strdup_printf("updateHistory(%s, %s)",
interactions_str.constData(),
all_loaded ? "true" : "false");
webkit_chat_container_execute_js(view, function_call);
g_free(function_call);
}
void
webkit_chat_container_set_invitation(WebKitChatContainer *view, bool show,
const std::string& contactUri, const std::string& contactId)
......
......@@ -50,6 +50,7 @@ void webkit_chat_container_print_new_interaction(WebKitChatContainer *view
void webkit_chat_container_update_interaction (WebKitChatContainer *view, lrc::api::ConversationModel& conversation_model, const QString& convUid, const QString& msgId, const lrc::api::interaction::Info& interaction);
void webkit_chat_container_remove_interaction (WebKitChatContainer *view, const QString& interactionId);
void webkit_chat_container_print_history (WebKitChatContainer *view, lrc::api::ConversationModel& conversation_model, lrc::api::MessagesList interactions);
void webkit_chat_container_update_history (WebKitChatContainer *view, lrc::api::ConversationModel& conversation_model, lrc::api::MessagesList interactions, bool all_loaded);
void webkit_chat_container_set_sender_image (WebKitChatContainer *view, const std::string& sender, const std::string& senderImage);
gboolean webkit_chat_container_is_ready (WebKitChatContainer *view);
void webkit_chat_container_set_display_links (WebKitChatContainer *view, bool display);
......
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