From 9134914f6065c0b248dd1b4317dbf9b16f5f52b5 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 19 Nov 2020 12:23:06 +0100 Subject: Matrix: fix duplicate tag items, cache events we dont have permission to view as well --- include/DownloadUtils.hpp | 6 +++++- src/DownloadUtils.cpp | 6 ++++-- src/plugins/Matrix.cpp | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/include/DownloadUtils.hpp b/include/DownloadUtils.hpp index e1e71df..3eff519 100644 --- a/include/DownloadUtils.hpp +++ b/include/DownloadUtils.hpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace QuickMedia { @@ -16,8 +17,11 @@ namespace QuickMedia { std::string value; }; + // Return true the return DownloadResult::OK for the download, which also saves the result in cache if |download_to_string_cache| is used + using DownloadErrorHandler = std::function; + DownloadResult download_to_string(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent = false, bool fail_on_error = true); - DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent = false); + DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent = false, DownloadErrorHandler error_handler = nullptr); DownloadResult download_to_file(const std::string &url, const std::string &destination_filepath, const std::vector &additional_args, bool use_tor, bool use_browser_useragent = false); DownloadResult download_to_json(const std::string &url, rapidjson::Document &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent = false, bool fail_on_error = true); } \ No newline at end of file diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index cbdded6..2a3f9d3 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -52,7 +52,7 @@ namespace QuickMedia { return DownloadResult::OK; } - DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent) { + DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent, DownloadErrorHandler error_handler) { Path media_dir = get_cache_dir().join("media"); Path media_file_path = Path(media_dir).join(base64_url::encode(url)); if(get_file_type(media_file_path) == FileType::REGULAR) { @@ -64,7 +64,9 @@ namespace QuickMedia { return DownloadResult::ERR; } } else { - DownloadResult download_result = download_to_string(url, result, additional_args, use_tor, use_browser_useragent); + DownloadResult download_result = download_to_string(url, result, additional_args, use_tor, use_browser_useragent, error_handler ? false : true); + if(!error_handler(result)) + download_result = DownloadResult::ERR; if(download_result == DownloadResult::OK) { Path media_file_path_tmp(media_file_path.data + ".tmp"); if(create_directory_recursive(media_dir) == 0 && file_overwrite(media_file_path_tmp, result) == 0) { diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index f6a91e7..f17da30 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -715,7 +715,15 @@ namespace QuickMedia { } for(auto &room_body_item : it.second) { - tag_data->room_body_items.push_back(room_body_item); + bool already_exists = false; + for(auto &body_it : tag_data->room_body_items) { + if(body_it->userdata == room_body_item->userdata) { + already_exists = true; + break; + } + } + if(!already_exists) + tag_data->room_body_items.push_back(room_body_item); } } add_room_body_items_by_tags.clear(); @@ -2744,7 +2752,22 @@ namespace QuickMedia { snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/event/%s", homeserver.c_str(), room->id.c_str(), event_id.c_str()); #endif std::string response; - DownloadResult download_result = download_to_string_cache(url, response, std::move(additional_args), use_tor, true); + DownloadResult download_result = download_to_string_cache(url, response, std::move(additional_args), use_tor, true, [](std::string &response) { + rapidjson::Document json_root; + rapidjson::ParseResult parse_result = json_root.Parse(response.c_str(), response.size()); + if(parse_result.IsError() || !json_root.IsObject()) + return false; + + const rapidjson::Value &errcode_json = GetMember(json_root, "errcode"); + if(errcode_json.IsString()) { + if(strcmp(errcode_json.GetString(), "M_FORBIDDEN") == 0) + return true; + else + return false; + } + + return true; + }); if(download_result != DownloadResult::OK) return nullptr; rapidjson::Document json_root; @@ -2761,6 +2784,13 @@ namespace QuickMedia { return nullptr; } + const rapidjson::Value &errcode_json = GetMember(json_root, "errcode"); + if(errcode_json.IsString() && strcmp(errcode_json.GetString(), "M_FORBIDDEN") == 0) { + fprintf(stderr, "You donm't have permission to access event %s\n", event_id.c_str()); + room->fetched_messages_by_event_id.insert(std::make_pair(event_id, nullptr)); + return nullptr; + } + const rapidjson::Value &error_json = GetMember(json_root, "error"); if(error_json.IsString()) { fprintf(stderr, "Matrix::get_message_by_id, error: %s\n", error_json.GetString()); -- cgit v1.2.3