diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-11-22 12:35:29 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-11-22 12:35:29 +0100 |
commit | b126ce050a325b49bcc0ae1e153ac1f166db0a73 (patch) | |
tree | d19a4c4bb7067443ad3a027d58626094bc1a1880 /src | |
parent | 15f9c6d44e4aa53db78571e43eae96ef08f20025 (diff) |
Matrix: filter out messages with transaction id, which fixed messages sent by us in another client
Diffstat (limited to 'src')
-rw-r--r-- | src/QuickMedia.cpp | 14 | ||||
-rw-r--r-- | src/plugins/Matrix.cpp | 63 |
2 files changed, 39 insertions, 38 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 0f4c695..0b91876 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3431,15 +3431,6 @@ namespace QuickMedia { }; std::thread post_thread(post_thread_handler); - auto filter_my_messages = [&me](Messages &messages) { - for(auto it = messages.begin(); it != messages.end();) { - if((*it)->user == me && ((*it)->type == MessageType::TEXT || (*it)->type == MessageType::REACTION)) - it = messages.erase(it); - else - ++it; - } - }; - auto filter_sent_messages = [&sent_messages](Messages &messages) { for(auto it = messages.begin(); it != messages.end();) { if(sent_messages.find((*it)->event_id) != sent_messages.end()) @@ -3861,13 +3852,13 @@ namespace QuickMedia { } }; - auto add_new_messages_to_current_room = [&me, &tabs, &selected_tab, ¤t_room](Messages &messages) { + auto add_new_messages_to_current_room = [&me, &tabs, &selected_tab, ¤t_room, &chat_state](Messages &messages) { if(messages.empty()) return; int num_items = tabs[MESSAGES_TAB_INDEX].body->items.size(); bool scroll_to_end = num_items == 0; - if(tabs[MESSAGES_TAB_INDEX].body->is_selected_item_last_visible_item() && selected_tab == MESSAGES_TAB_INDEX) + if(tabs[MESSAGES_TAB_INDEX].body->is_selected_item_last_visible_item() && selected_tab == MESSAGES_TAB_INDEX && chat_state != ChatState::TYPING_MESSAGE) scroll_to_end = true; BodyItem *selected_item = tabs[MESSAGES_TAB_INDEX].body->get_selected(); @@ -4456,7 +4447,6 @@ namespace QuickMedia { 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_my_messages(sync_data.messages); filter_existing_messages(sync_data.messages); } add_new_messages_to_current_room(sync_data.messages); diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 05881d5..eeef477 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -1750,6 +1750,13 @@ namespace QuickMedia { if(!type_json.IsString()) return nullptr; + const rapidjson::Value &unsigned_json = GetMember(event_item_json, "unsigned"); + if(unsigned_json.IsObject()) { + const rapidjson::Value &transaction_id_json = GetMember(unsigned_json, "transaction_id"); + if(transaction_id_json.IsString() && my_events_transaction_ids.find(transaction_id_json.GetString()) != my_events_transaction_ids.end()) + return nullptr; + } + RelatedEventType related_event_type = RelatedEventType::NONE; std::string related_event_id; const rapidjson::Value &relates_to_json = GetMember(*content_json, "m.relates_to"); @@ -1823,7 +1830,6 @@ namespace QuickMedia { reason_str = std::string(reason_json.GetString(), reason_json.GetStringLength()); const rapidjson::Value &membership_json = GetMember(*content_json, "membership"); if(strcmp(membership_json.GetString(), "join") == 0) { - const rapidjson::Value &unsigned_json = GetMember(event_item_json, "unsigned"); if(unsigned_json.IsObject()) { const rapidjson::Value &prev_sender = GetMember(unsigned_json, "prev_sender"); const rapidjson::Value &prev_content_json = GetMember(unsigned_json, "prev_content"); @@ -2399,6 +2405,15 @@ namespace QuickMedia { return result.str(); } + static std::string create_transaction_id() { + char random_characters[18]; + if(!generate_random_characters(random_characters, sizeof(random_characters))) + return ""; + + std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters)); + return "m." + std::to_string(time(NULL)) + random_readable_chars; + } + static const char* content_type_to_message_type(ContentType content_type) { if(is_content_type_video(content_type)) return "m.video"; @@ -2411,11 +2426,10 @@ namespace QuickMedia { } PluginResult Matrix::post_message(RoomData *room, const std::string &body, std::string &event_id_response, const std::optional<UploadInfo> &file_info, const std::optional<UploadInfo> &thumbnail_info, const std::string &msgtype) { - char random_characters[18]; - if(!generate_random_characters(random_characters, sizeof(random_characters))) + std::string transaction_id = create_transaction_id(); + if(transaction_id.empty()) return PluginResult::ERR; - - std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters)); + my_events_transaction_ids.insert(transaction_id); std::string formatted_body; bool contains_formatted_text = false; @@ -2493,7 +2507,7 @@ namespace QuickMedia { }; char request_url[512]; - snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room->id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s", homeserver.c_str(), room->id.c_str(), transaction_id.c_str()); rapidjson::Document json_root; DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true); @@ -2596,11 +2610,10 @@ namespace QuickMedia { // TODO: Store shared_ptr<Message> instead of raw pointer... Message *relates_to_message_raw = (Message*)relates_to; - char random_characters[18]; - if(!generate_random_characters(random_characters, sizeof(random_characters))) + std::string transaction_id = create_transaction_id(); + if(transaction_id.empty()) return PluginResult::ERR; - - std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters)); + my_events_transaction_ids.insert(transaction_id); rapidjson::Document in_reply_to_json(rapidjson::kObjectType); in_reply_to_json.AddMember("event_id", rapidjson::StringRef(relates_to_message_raw->event_id.c_str()), in_reply_to_json.GetAllocator()); @@ -2630,7 +2643,7 @@ namespace QuickMedia { }; char request_url[512]; - snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room->id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s", homeserver.c_str(), room->id.c_str(), transaction_id.c_str()); rapidjson::Document json_root; DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true); @@ -2651,11 +2664,10 @@ namespace QuickMedia { PluginResult Matrix::post_edit(RoomData *room, const std::string &body, void *relates_to, std::string &event_id_response) { Message *relates_to_message_raw = (Message*)relates_to; - char random_characters[18]; - if(!generate_random_characters(random_characters, sizeof(random_characters))) + std::string transaction_id = create_transaction_id(); + if(transaction_id.empty()) return PluginResult::ERR; - - std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters)); + my_events_transaction_ids.insert(transaction_id); std::string formatted_body; bool contains_formatted_text = false; @@ -2715,7 +2727,7 @@ namespace QuickMedia { }; char request_url[512]; - snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room->id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s", homeserver.c_str(), room->id.c_str(), transaction_id.c_str()); rapidjson::Document json_root; DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true); @@ -2736,11 +2748,10 @@ namespace QuickMedia { PluginResult Matrix::post_reaction(RoomData *room, const std::string &body, void *relates_to, std::string &event_id_response) { Message *relates_to_message_raw = (Message*)relates_to; - char random_characters[18]; - if(!generate_random_characters(random_characters, sizeof(random_characters))) + std::string transaction_id = create_transaction_id(); + if(transaction_id.empty()) return PluginResult::ERR; - - std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters)); + my_events_transaction_ids.insert(transaction_id); rapidjson::Document relates_to_json(rapidjson::kObjectType); relates_to_json.AddMember("event_id", rapidjson::StringRef(relates_to_message_raw->event_id.c_str()), relates_to_json.GetAllocator()); @@ -2762,7 +2773,7 @@ namespace QuickMedia { }; char request_url[512]; - snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.reaction/m%ld.%.*s", homeserver.c_str(), room->id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.reaction/%s", homeserver.c_str(), room->id.c_str(), transaction_id.c_str()); rapidjson::Document json_root; DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true); @@ -3096,16 +3107,16 @@ namespace QuickMedia { set_next_batch(""); invites.clear(); filter_cached.reset(); + my_events_transaction_ids.clear(); return PluginResult::OK; } PluginResult Matrix::delete_message(RoomData *room, void *message, std::string &err_msg){ - char random_characters[18]; - if(!generate_random_characters(random_characters, sizeof(random_characters))) + std::string transaction_id = create_transaction_id(); + if(transaction_id.empty()) return PluginResult::ERR; - - std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters)); + my_events_transaction_ids.insert(transaction_id); Message *message_typed = (Message*)message; @@ -3123,7 +3134,7 @@ namespace QuickMedia { }; char url[512]; - snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/redact/%s/m%ld.%.*s", homeserver.c_str(), room->id.c_str(), message_typed->event_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/redact/%s/%s", homeserver.c_str(), room->id.c_str(), message_typed->event_id.c_str(), transaction_id.c_str()); rapidjson::Document json_root; DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true, &err_msg); |