From 2e22afbaa5c01d9aa40564a7646092455ce0d88c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 9 Feb 2021 15:29:29 +0100 Subject: Matrix: put an end to my unread messages misery --- src/plugins/Matrix.cpp | 65 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 12 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 66a942b..50a2cd4 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -23,9 +23,9 @@ static const char* SERVICE_NAME = "matrix"; static const char* OTHERS_ROOM_TAG = "tld.name.others"; // Filter without account data -static const char* INITIAL_FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"types\":[\"m.room.message\"],\"limit\":1,\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"limit\":1,\"types\":[\"m.fully_read\",\"m.tag\"],\"lazy_load_members\":true}}}"; +static const char* INITIAL_FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"types\":[\"m.room.message\"],\"limit\":1,\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"limit\":1,\"types\":[\"m.fully_read\",\"m.tag\",\"qm.last_read_message_timestamp\"],\"lazy_load_members\":true}}}"; static const char* ADDITIONAL_MESSAGES_FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"limit\":20,\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true}}}"; -static const char* CONTINUE_FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"types\":[\"m.fully_read\",\"m.tag\"],\"lazy_load_members\":true}}}"; +static const char* CONTINUE_FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"types\":[\"m.fully_read\",\"m.tag\",\"qm.last_read_message_timestamp\"],\"lazy_load_members\":true}}}"; static std::string capitalize(const std::string &str) { if(str.size() >= 1) @@ -437,6 +437,7 @@ namespace QuickMedia { for(size_t i = 0; i < body->items.size(); ++i) { if(body->items[i]->url == selected_item->url) { body->set_selected_item(i); + body->reset_prev_selected_item(); return; } } @@ -509,6 +510,9 @@ namespace QuickMedia { read_marker_message_timestamp = read_marker_message->timestamp; } + if(read_marker_message_timestamp == 0) + read_marker_message_timestamp = room->last_read_message_timestamp; + std::shared_ptr last_new_message = get_last_message_by_timestamp(new_messages); if(!last_new_message) return; @@ -532,6 +536,9 @@ namespace QuickMedia { else if(read_marker_message_timestamp == 0 && !my_user_read_marker.empty() && last_new_message->event_id.find(':') != std::string::npos && last_new_message->event_id > my_user_read_marker) last_unread_message = last_new_message.get(); + if(!last_unread_message && read_marker_message_timestamp == 0) + last_unread_message = last_new_message.get(); + //assert(room_body_item); if(!room->body_item) return; @@ -1603,18 +1610,30 @@ namespace QuickMedia { continue; const rapidjson::Value &type_json = GetMember(event_json, "type"); - if(!type_json.IsString() || strcmp(type_json.GetString(), "m.fully_read") != 0) + if(!type_json.IsString()) continue; - const rapidjson::Value &content_json = GetMember(event_json, "content"); - if(!content_json.IsObject()) - continue; + if(strcmp(type_json.GetString(), "m.fully_read") == 0) { + const rapidjson::Value &content_json = GetMember(event_json, "content"); + if(!content_json.IsObject()) + continue; - const rapidjson::Value &event_id_json = GetMember(content_json, "event_id"); - if(!event_id_json.IsString()) - continue; + const rapidjson::Value &event_id_json = GetMember(content_json, "event_id"); + if(!event_id_json.IsString()) + continue; + + room_data->set_user_read_marker(me, std::string(event_id_json.GetString(), event_id_json.GetStringLength())); + } else if(strcmp(type_json.GetString(), "qm.last_read_message_timestamp") == 0) { + const rapidjson::Value &content_json = GetMember(event_json, "content"); + if(!content_json.IsObject()) + continue; - room_data->set_user_read_marker(me, std::string(event_id_json.GetString(), event_id_json.GetStringLength())); + const rapidjson::Value ×tamp_json = GetMember(content_json, "timestamp"); + if(!timestamp_json.IsInt64()) + continue; + + room_data->last_read_message_timestamp = timestamp_json.GetInt64(); + } } } @@ -3360,7 +3379,7 @@ namespace QuickMedia { return download_result_to_plugin_result(download_result); } - PluginResult Matrix::set_read_marker(RoomData *room, const std::string &event_id) { + PluginResult Matrix::set_read_marker(RoomData *room, const std::string &event_id, int64_t event_timestamp) { rapidjson::Document request_data(rapidjson::kObjectType); request_data.AddMember("m.fully_read", rapidjson::StringRef(event_id.c_str()), request_data.GetAllocator()); request_data.AddMember("m.read", rapidjson::StringRef(event_id.c_str()), request_data.GetAllocator()); @@ -3385,7 +3404,29 @@ namespace QuickMedia { if(me) room->set_user_read_marker(me, event_id); - return PluginResult::OK; + return set_qm_last_read_message_timestamp(room, event_timestamp); + } + + PluginResult Matrix::set_qm_last_read_message_timestamp(RoomData *room, int64_t timestamp) { + rapidjson::Document request_data(rapidjson::kObjectType); + request_data.AddMember("timestamp", timestamp, request_data.GetAllocator()); + + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + request_data.Accept(writer); + + std::vector additional_args = { + { "-X", "PUT" }, + { "-H", "content-type: application/json" }, + { "-H", "Authorization: Bearer " + access_token }, + { "--data-binary", buffer.GetString() } + }; + + room->last_read_message_timestamp = timestamp; + + std::string server_response; + DownloadResult download_result = download_to_string(homeserver + "/_matrix/client/r0/user/" + my_user_id + "/rooms/" + room->id + "/account_data/qm.last_read_message_timestamp", server_response, std::move(additional_args), use_tor, true); + return download_result_to_plugin_result(download_result); } PluginResult Matrix::join_room(const std::string &room_id) { -- cgit v1.2.3