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/plugins | |
parent | 15f9c6d44e4aa53db78571e43eae96ef08f20025 (diff) |
Matrix: filter out messages with transaction id, which fixed messages sent by us in another client
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Matrix.cpp | 63 |
1 files changed, 37 insertions, 26 deletions
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); |