diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/QuickMedia.cpp | 91 |
1 files changed, 59 insertions, 32 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index ff1769b..1ef6025 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3267,14 +3267,26 @@ namespace QuickMedia { }; Body url_selection_body(this, loading_icon); + + std::unordered_set<std::string> fetched_messages_set; + auto filter_existing_messages = [&fetched_messages_set](Messages &messages) { + for(auto it = messages.begin(); it != messages.end();) { + auto res = fetched_messages_set.insert((*it)->event_id); + if(!res.second) + it = messages.erase(it); + else + ++it; + } + }; Messages all_messages; matrix->get_all_synced_room_messages(current_room, all_messages); - size_t num_messages_in_room = all_messages.size(); + for(auto &message : all_messages) { + fetched_messages_set.insert(message->event_id); + } tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(messages_to_body_items(all_messages, matrix->get_me(current_room).get())); modify_related_messages_in_current_room(all_messages); tabs[MESSAGES_TAB_INDEX].body->select_last_item(); - all_messages.clear(); std::vector<std::string> pinned_events; matrix->get_all_pinned_events(current_room, pinned_events); @@ -3292,14 +3304,15 @@ namespace QuickMedia { chat_input.set_editable(false); MessageQueue<std::function<void()>> post_task_queue; - std::thread post_thread([&post_task_queue]() { + auto post_thread_handler = [&post_task_queue]() { while(true) { std::optional<std::function<void()>> post_task_opt = post_task_queue.pop_wait(); if(!post_task_opt) break; post_task_opt.value()(); } - }); + }; + std::thread post_thread(post_thread_handler); chat_input.on_submit_callback = [this, &chat_input, &selected_tab, ¤t_room, &new_page, &chat_state, ¤tly_operating_on_item, &post_task_queue](std::string text) mutable { if(!current_room) @@ -3459,28 +3472,23 @@ namespace QuickMedia { tabs[MESSAGES_TAB_INDEX].body->set_page_scroll(window_size.y); - std::unordered_set<Message*> fetched_messages_set; - auto filter_existing_messages = [&fetched_messages_set](Messages &messages) { - for(auto it = messages.begin(); it != messages.end();) { - auto res = fetched_messages_set.insert(it->get()); - if(!res.second) - it = messages.erase(it); - else - ++it; + bool fetched_enough_messages = false; + bool initial_prev_messages_fetch = true; + + auto fetch_more_previous_messages_if_needed = [this, &all_messages, ¤t_room, &fetched_enough_messages, &previous_messages_future]() { + if(!fetched_enough_messages && !previous_messages_future.ready()) { + bool fetch_latest_messages = !matrix->is_initial_sync_finished(); + if(all_messages.size() < 10) { + previous_messages_future = [this, ¤t_room, fetch_latest_messages]() { + Messages messages; + if(matrix->get_previous_room_messages(current_room, messages, fetch_latest_messages) != PluginResult::OK) + fprintf(stderr, "Failed to get previous matrix messages in room: %s\n", current_room->id.c_str()); + return messages; + }; + } } }; - - bool fetch_latest_messages = !matrix->is_initial_sync_finished(); - bool initial_prev_messages_fetch = false; - if(num_messages_in_room < 10) { - initial_prev_messages_fetch = true; - previous_messages_future = [this, ¤t_room, fetch_latest_messages]() { - Messages messages; - if(matrix->get_previous_room_messages(current_room, messages, fetch_latest_messages) != PluginResult::OK) - fprintf(stderr, "Failed to get previous matrix messages in room: %s\n", current_room->id.c_str()); - return messages; - }; - } + fetch_more_previous_messages_if_needed(); sf::RectangleShape more_messages_below_rect; more_messages_below_rect.setFillColor(sf::Color(128, 50, 50)); @@ -3495,7 +3503,7 @@ namespace QuickMedia { float tab_vertical_offset = 0.0f; MessageQueue<bool> typing_state_queue; - std::thread typing_state_thread([this, ¤t_room, &typing_state_queue]() { + auto typing_state_handler = [this, ¤t_room, &typing_state_queue]() { while(true) { std::optional<bool> state_opt = typing_state_queue.pop_wait(); if(!state_opt) @@ -3507,7 +3515,8 @@ namespace QuickMedia { else matrix->on_stop_typing(current_room); } - }); + }; + std::thread typing_state_thread(typing_state_handler); sf::Clock frame_timer; @@ -3618,9 +3627,8 @@ namespace QuickMedia { return false; }; - auto cleanup_tasks = [&set_read_marker_future, &previous_messages_future, &fetch_message_future, &typing_state_queue, &typing_state_thread, &post_task_queue, &post_thread, &unreferenced_event_by_room, &tabs]() { + auto cleanup_tasks = [&set_read_marker_future, &fetch_message_future, &typing_state_queue, &typing_state_thread, &post_task_queue, &post_thread, &unreferenced_event_by_room, &tabs]() { set_read_marker_future.cancel(); - previous_messages_future.cancel(); fetch_message_future.cancel(); typing_state_queue.close(); if(typing_state_thread.joinable()) { @@ -3633,7 +3641,7 @@ namespace QuickMedia { post_thread.join(); } - unreferenced_event_by_room.clear(); + //unreferenced_event_by_room.clear(); if(!tabs.empty()) { for(auto &body_item : tabs[PINNED_TAB_INDEX].body->items) { @@ -3641,7 +3649,7 @@ namespace QuickMedia { } } - tabs.clear(); + //tabs.clear(); }; float tab_shade_height = 0.0f; @@ -3936,6 +3944,7 @@ namespace QuickMedia { break; } case PageType::CHAT_LOGIN: { + previous_messages_future.cancel(); cleanup_tasks(); new_page = PageType::CHAT; matrix->stop_sync(); @@ -4032,6 +4041,10 @@ namespace QuickMedia { sync_data.messages.clear(); matrix->get_room_sync_data(current_room, sync_data); + if(!sync_data.messages.empty()) { + all_messages.insert(all_messages.end(), sync_data.messages.begin(), sync_data.messages.end()); + filter_existing_messages(sync_data.messages); + } add_new_messages_to_current_room(sync_data.messages); modify_related_messages_in_current_room(sync_data.messages); process_pinned_events(sync_data.pinned_events); @@ -4044,6 +4057,9 @@ namespace QuickMedia { if(previous_messages_future.ready()) { Messages new_messages = previous_messages_future.get(); + all_messages.insert(all_messages.end(), new_messages.begin(), new_messages.end()); + if(new_messages.empty() || all_messages.size() >= 10) + fetched_enough_messages = true; filter_existing_messages(new_messages); fprintf(stderr, "Finished fetching older messages, num new messages: %zu\n", new_messages.size()); bool move_to_bottom = false; @@ -4071,6 +4087,7 @@ namespace QuickMedia { tabs[MESSAGES_TAB_INDEX].body->set_page_scroll(window_size.y); } } + fetch_more_previous_messages_if_needed(); } if(fetch_message_future.ready()) { @@ -4235,7 +4252,9 @@ namespace QuickMedia { }; } } + } + if(selected_tab == MESSAGES_TAB_INDEX && current_room) { window.draw(chat_input_shade); chat_input.draw(window); //chat_input.draw(window, false); window.draw(logo_sprite); @@ -4273,8 +4292,15 @@ namespace QuickMedia { current_room = matrix->get_room_by_id(current_room->id); if(current_room) { - chat_page(matrix_chat_page, current_room); - return; + std::vector<std::string> pinned_events; + matrix->get_all_pinned_events(current_room, pinned_events); + process_pinned_events(std::move(pinned_events)); + tabs[PINNED_TAB_INDEX].body->select_last_item(); + typing_state_queue.restart(); + typing_state_thread = std::thread(typing_state_handler); + post_task_queue.restart(); + post_thread = std::thread(post_thread_handler); + fetch_more_previous_messages_if_needed(); } else { go_to_previous_page = true; } @@ -4287,6 +4313,7 @@ namespace QuickMedia { } chat_page_end: + previous_messages_future.cancel(); cleanup_tasks(); } |