From cfef90021917df0052bb49e9e35298260e7ca38c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 4 Oct 2020 22:38:33 +0200 Subject: Matrix: remove initial delay when entering room --- plugins/Matrix.hpp | 6 ++++-- src/QuickMedia.cpp | 3 +-- src/plugins/Matrix.cpp | 34 ++++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index de7d9b8..4d7bc11 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -46,8 +46,10 @@ namespace QuickMedia { void set_user_read_marker(std::shared_ptr &user, const std::string &event_id); std::string get_user_read_marker(std::shared_ptr &user); - void prepend_messages_reverse(std::vector> new_messages); - void append_messages(std::vector> new_messages); + // Ignores duplicates, returns the number of inserted elements + size_t prepend_messages_reverse(std::vector> new_messages); + // Ignores duplicates, returns the number of inserted elements + size_t append_messages(std::vector> new_messages); std::shared_ptr get_message_by_id(const std::string &id); diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 776093d..dfd4551 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3882,9 +3882,8 @@ namespace QuickMedia { tabs[MESSAGES_TAB_INDEX].body->clear_items(); BodyItems new_items; - // TODO: Make asynchronous if(matrix->get_all_synced_room_messages(current_room_id, new_items) == PluginResult::OK) { - tabs[MESSAGES_TAB_INDEX].body->append_items(std::move(new_items)); + tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(std::move(new_items)); tabs[MESSAGES_TAB_INDEX].body->select_last_item(); } else { std::string err_msg = "Failed to get messages in room: " + current_room_id; diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 34c47f1..ca3fc28 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -42,20 +42,30 @@ namespace QuickMedia { return user->read_marker_event_id; } - void RoomData::prepend_messages_reverse(std::vector> new_messages) { + size_t RoomData::prepend_messages_reverse(std::vector> new_messages) { std::lock_guard lock(room_mutex); - for(auto &new_message : new_messages) { - message_by_event_id[new_message->event_id] = new_message; + size_t num_inserted = 0; + for(auto it = new_messages.begin(); it != new_messages.end(); ++it) { + if(message_by_event_id.find((*it)->event_id) == message_by_event_id.end()) { + message_by_event_id.insert(std::make_pair((*it)->event_id, *it)); + messages.insert(messages.begin(), std::move(*it)); + ++num_inserted; + } } - messages.insert(messages.begin(), new_messages.rbegin(), new_messages.rend()); + return num_inserted; } - void RoomData::append_messages(std::vector> new_messages) { + size_t RoomData::append_messages(std::vector> new_messages) { std::lock_guard lock(room_mutex); - for(auto &new_message : new_messages) { - message_by_event_id[new_message->event_id] = new_message; + size_t num_inserted = 0; + for(auto it = new_messages.begin(); it != new_messages.end(); ++it) { + if(message_by_event_id.find((*it)->event_id) == message_by_event_id.end()) { + message_by_event_id.insert(std::make_pair((*it)->event_id, *it)); + messages.push_back(std::move(*it)); + ++num_inserted; + } } - messages.insert(messages.end(), new_messages.begin(), new_messages.end()); + return num_inserted; } std::shared_ptr RoomData::get_message_by_id(const std::string &id) { @@ -225,6 +235,7 @@ namespace QuickMedia { } // TODO: Thread safe? + /* if(!room->initial_fetch_finished) { PluginResult result = get_previous_room_messages(room); if(result == PluginResult::OK) { @@ -234,6 +245,7 @@ namespace QuickMedia { return result; } } + */ room->acquire_room_lock(); room_messages_to_body_items(room->get_messages_thread_unsafe().data(), room->get_messages_thread_unsafe().size(), result_items); @@ -249,6 +261,7 @@ namespace QuickMedia { return PluginResult::ERR; } + /* if(!room->initial_fetch_finished) { PluginResult result = get_previous_room_messages(room); if(result == PluginResult::OK) { @@ -258,6 +271,7 @@ namespace QuickMedia { return result; } } + */ room->acquire_room_lock(); size_t num_new_messages = room->get_messages_thread_unsafe().size() - room->last_read_index; @@ -670,10 +684,10 @@ namespace QuickMedia { // TODO: Loop and std::move instead? doesn't insert create copies? if(message_dir == MessageDirection::BEFORE) { - room_data->prepend_messages_reverse(std::move(new_messages)); + size_t num_inserted_messages = room_data->prepend_messages_reverse(std::move(new_messages)); // TODO: Is this thread-safe? if(room_data->last_read_index != 0) - room_data->last_read_index += new_messages.size(); + room_data->last_read_index += num_inserted_messages; } else if(message_dir == MessageDirection::AFTER) { room_data->append_messages(std::move(new_messages)); } -- cgit v1.2.3