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 --- src/plugins/Matrix.cpp | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src/plugins') 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