From c567392a7a82040999e2a96b39084c900385ad57 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 10 Dec 2022 22:08:05 +0100 Subject: Matrix: show loading icon properly for sync, show notifications for first sync --- plugins/Matrix.hpp | 2 +- src/plugins/Matrix.cpp | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index e0e789e..0a2092c 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -778,7 +778,7 @@ namespace QuickMedia { bool set_invite(const std::string &room_id, Invite invite); // Returns true if an invite for |room_id| exists bool remove_invite(const std::string &room_id); - void set_next_batch(std::string new_next_batch); + void set_next_batch(std::string new_next_batch, bool set_initial_sync); std::string get_next_batch(); void set_next_notifications_token(std::string new_next_token); std::string get_next_notifications_token(); diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 63027ab..230232a 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -1684,7 +1684,7 @@ namespace QuickMedia { next_batch_json = &GetMember(doc, "next_batch"); if(next_batch_json->IsString()) { - set_next_batch(next_batch_json->GetString()); + set_next_batch(next_batch_json->GetString(), false); //fprintf(stderr, "Matrix: next batch: %s\n", next_batch.c_str()); } } @@ -1719,29 +1719,19 @@ namespace QuickMedia { bool initial_sync = next_batch.empty(); bool first_sync = true; - notification_thread = std::thread([this]() { - get_previous_notifications([this](const MatrixNotification ¬ification) { - if(notification.read) - return; - - MatrixDelegate *delegate = this->delegate; - ui_thread_tasks.push([delegate, notification] { - delegate->add_unread_notification(std::move(notification)); - }); - }); - finished_fetching_notifications = true; - }); - std::string filter_encoded; if(initial_sync) filter_encoded = url_param_encode(INITIAL_FILTER); else filter_encoded = url_param_encode(CONTINUE_FILTER); + initial_sync = true; while(sync_running) { char url[2048]; if(next_batch.empty()) snprintf(url, sizeof(url), "%s/_matrix/client/r0/sync?filter=%s&timeout=0", homeserver.c_str(), filter_encoded.c_str()); + else if(first_sync) + snprintf(url, sizeof(url), "%s/_matrix/client/r0/sync?filter=%s&timeout=0&since=%s", homeserver.c_str(), filter_encoded.c_str(), next_batch.c_str()); else snprintf(url, sizeof(url), "%s/_matrix/client/r0/sync?filter=%s&timeout=30000&since=%s", homeserver.c_str(), filter_encoded.c_str(), next_batch.c_str()); @@ -1781,7 +1771,7 @@ namespace QuickMedia { next_batch_json = &GetMember(json_root, "next_batch"); if(next_batch_json->IsString()) { - set_next_batch(next_batch_json->GetString()); + set_next_batch(next_batch_json->GetString(), true); fprintf(stderr, "Matrix: next batch: %s\n", next_batch.c_str()); } else { //set_next_batch("Invalid"); @@ -1792,7 +1782,22 @@ namespace QuickMedia { if(first_sync) { first_sync = false; - filter_encoded = url_param_encode(CONTINUE_FILTER); + initial_sync = false; + filter_encoded = url_param_encode(CONTINUE_FILTER); // TODO: limit messages in this continue filter? + + // TODO: This ignores new rooms that are not part of the previous sync message. Fix this. + notification_thread = std::thread([this]() { + get_previous_notifications([this](const MatrixNotification ¬ification) { + if(notification.read) + return; + + MatrixDelegate *delegate = this->delegate; + ui_thread_tasks.push([delegate, notification] { + delegate->add_unread_notification(std::move(notification)); + }); + }); + finished_fetching_notifications = true; + }); } #if 0 @@ -1812,7 +1817,6 @@ namespace QuickMedia { // and fetch previous messages etc themselves. if(!matrix_instance_already_running) { sync_cache_file = fopen(matrix_cache_dir.data.c_str(), initial_sync ? "wb" : "ab"); - initial_sync = false; if(sync_cache_file) { if(json_root.IsObject()) { rapidjson::StringBuffer buffer; @@ -1857,11 +1861,12 @@ namespace QuickMedia { delegate = nullptr; sync_failed = false; sync_fail_reason.clear(); - set_next_batch(""); + set_next_batch("", false); next_notifications_token.clear(); invites.clear(); filter_cached.reset(); finished_fetching_notifications = false; + initial_sync_finished = false; custom_emoji_by_key.clear(); silenced_invites.clear(); qm_read_markers_by_room_cache.clear(); @@ -5766,10 +5771,11 @@ namespace QuickMedia { return false; } - void Matrix::set_next_batch(std::string new_next_batch) { + void Matrix::set_next_batch(std::string new_next_batch, bool set_initial_sync) { std::lock_guard lock(next_batch_mutex); next_batch = std::move(new_next_batch); - initial_sync_finished = !next_batch.empty(); + if(set_initial_sync) + initial_sync_finished = !next_batch.empty(); } std::string Matrix::get_next_batch() { -- cgit v1.2.3