From e47a34f3c4d9f892c002d0dce7fb43a3ca1b4f28 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 14 Dec 2022 22:26:59 +0100 Subject: Matrix: do not progress if internet is down (load qm read markers fails) --- src/plugins/Matrix.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 5355208..fae8451 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -1667,7 +1667,11 @@ namespace QuickMedia { FILE *sync_cache_file; const rapidjson::Value *next_batch_json = nullptr; - load_qm_read_markers_from_account_data(); // TODO: Remove when https://github.com/matrix-org/synapse/issues/14444 is fixed, if ever. + if(!load_qm_read_markers_from_account_data()) { // TODO: Remove when https://github.com/matrix-org/synapse/issues/14444 is fixed, if ever. + show_notification("QuickMedia", "Failed to connect to matrix homeserver. Is your internet down?", Urgency::CRITICAL); + sync_running = false; + return; + } std::ifstream sync_cache_file_stream; sync_cache_file_stream.open(matrix_cache_dir.data.c_str(), std::ifstream::in | std::ifstream::binary); @@ -5287,7 +5291,11 @@ namespace QuickMedia { rapidjson::Document request_data(rapidjson::kObjectType); { std::lock_guard lock(room_data_mutex); - load_qm_read_markers_from_account_data(); // TODO: Only do this if multiple instances of quickmedia matrix is running (on the same account) + if(!load_qm_read_markers_from_account_data()) { // TODO: Only do this if multiple instances of quickmedia matrix is running (on the same account) + fprintf(stderr, "Error: load_qm_read_markers_from_account_data failed, failed to set read marker\n"); + return PluginResult::ERR; + } + qm_read_markers_by_room_cache[room->id] = timestamp; room->read_marker_event_timestamp = timestamp; for(const auto &[key, val] : qm_read_markers_by_room_cache) { @@ -5314,7 +5322,7 @@ namespace QuickMedia { return PluginResult::OK; } - void Matrix::load_qm_read_markers_from_account_data() { + bool Matrix::load_qm_read_markers_from_account_data() { std::vector additional_args = { { "-H", "content-type: application/json" }, { "-H", "Authorization: Bearer " + access_token } @@ -5325,11 +5333,11 @@ namespace QuickMedia { DownloadResult download_result = download_json(json_root, homeserver + "/_matrix/client/r0/user/" + my_user_id + "/account_data/qm.last_read_message_timestamp", std::move(additional_args), true, &err_msg); if(download_result != DownloadResult::OK) { fprintf(stderr, "Warning: failed to get account qm.last_read_message_timestamp\n"); - return; + return false; } if(!json_root.IsObject()) - return; + return false; std::lock_guard lock(room_data_mutex); qm_read_markers_by_room_cache.clear(); @@ -5343,6 +5351,8 @@ namespace QuickMedia { rooms[room_it->second]->read_marker_event_timestamp = obj.value.GetInt64(); qm_read_markers_by_room_cache[std::move(room_id)] = obj.value.GetInt64(); } + + return true; } PluginResult Matrix::join_room(const std::string &room_id_or_name) { -- cgit v1.2.3