diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/QuickMedia.cpp | 22 | ||||
-rw-r--r-- | src/plugins/Matrix.cpp | 36 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index da76335..101fa59 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -5246,6 +5246,26 @@ namespace QuickMedia { return message; } + static std::string pantalaimon_image_proxy_url_to_remote_image_url(Matrix *matrix, const std::string &image_url) { + std::string remote_homeserver_url = matrix->get_remote_homeserver_url(); + if(!remote_homeserver_url.empty() && remote_homeserver_url.back() == '/') + remote_homeserver_url.pop_back(); + + std::string result_url = image_url; + + if(string_starts_with(result_url, "http://")) + result_url.erase(result_url.begin(), result_url.begin() + 7); + else if(string_starts_with(result_url, "https://")) + result_url.erase(result_url.begin(), result_url.begin() + 8); + + size_t path_index = result_url.find('/'); + if(path_index == std::string::npos) + return remote_homeserver_url; + + result_url.replace(0, path_index, remote_homeserver_url); + return result_url; + } + bool Program::chat_page(MatrixChatPage *matrix_chat_page, RoomData *current_room) { assert(current_room); assert(strcmp(plugin_name, "matrix") == 0); @@ -6627,6 +6647,8 @@ namespace QuickMedia { if(selected_item_message->type == MessageType::VIDEO) image_url = selected_item->thumbnail_url; + image_url = pantalaimon_image_proxy_url_to_remote_image_url(matrix, image_url); + std::vector<Tab> saucenao_tabs; saucenao_tabs.push_back(Tab{create_body(), std::make_unique<SaucenaoPage>(this, image_url, false), nullptr}); page_loop(saucenao_tabs); diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 144bbd8..5306349 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -3863,6 +3863,16 @@ namespace QuickMedia { if(home_server_json.IsString()) this->homeserver_domain = home_server_json.GetString(); + const rapidjson::Value &well_known_json = GetMember(json_root, "well_known"); + if(well_known_json.IsObject()) { + const rapidjson::Value &homeserver_json = GetMember(well_known_json, "m.homeserver"); + if(homeserver_json.IsObject()) { + const rapidjson::Value &base_url_json = GetMember(homeserver_json, "base_url"); + if(base_url_json.IsString()) + well_known_base_url = base_url_json.GetString(); + } + } + // Use the user-provided homeserver instead of the one the server tells us about, otherwise this wont work with a proxy // such as pantalaimon json_root.AddMember("homeserver", rapidjson::StringRef(homeserver.c_str()), request_data.GetAllocator()); @@ -3906,6 +3916,7 @@ namespace QuickMedia { access_token.clear(); homeserver.clear(); homeserver_domain.clear(); + well_known_base_url.clear(); upload_limit.reset(); return PluginResult::OK; @@ -4103,6 +4114,16 @@ namespace QuickMedia { if(home_server_json.IsString()) this->homeserver_domain = home_server_json.GetString(); + const rapidjson::Value &well_known_json = GetMember(json_root, "well_known"); + if(well_known_json.IsObject()) { + const rapidjson::Value &homeserver_json = GetMember(well_known_json, "m.homeserver"); + if(homeserver_json.IsObject()) { + const rapidjson::Value &base_url_json = GetMember(homeserver_json, "base_url"); + if(base_url_json.IsString()) + well_known_base_url = base_url_json.GetString(); + } + } + this->my_user_id = user_id_json.GetString(); this->access_token = access_token_json.GetString(); this->homeserver = homeserver_json.GetString(); @@ -4527,6 +4548,21 @@ namespace QuickMedia { return homeserver_domain; } + std::string Matrix::get_remote_homeserver_url() const { + if(!well_known_base_url.empty()) + return well_known_base_url; + + if(!homeserver_domain.empty()) { + if(string_starts_with(homeserver_domain, "http://") || string_starts_with(homeserver_domain, "https://")) + return homeserver_domain; + return "https://" + homeserver_domain; // TODO: What if domain does not use https? + } + + if(string_starts_with(homeserver, "http://") || string_starts_with(homeserver, "https://")) + return homeserver; + return "https://" + homeserver; // TODO: What if domain does not use https? + } + RoomData* Matrix::get_room_by_id(const std::string &id) { std::lock_guard<std::recursive_mutex> lock(room_data_mutex); auto room_it = room_data_by_id.find(id); |