diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Matrix.cpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 11d0746..2736421 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -20,6 +20,7 @@ static const char* SERVICE_NAME = "matrix"; static const char* OTHERS_ROOM_TAG = "tld.name.others"; +static const char* 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\":3,\"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 rapidjson::Value nullValue(rapidjson::kNullType); static const rapidjson::Value& GetMember(const rapidjson::Value &obj, const char *key) { @@ -850,7 +851,7 @@ namespace QuickMedia { // Filter with account data // {"presence":{"limit":0,"types":[""]},"account_data":{"not_types":["im.vector.setting.breadcrumbs","m.push_rules","im.vector.setting.allowed_widgets","io.element.recent_emoji"]},"room":{"state":{"limit":1,"not_types":["m.room.related_groups","m.room.power_levels","m.room.join_rules","m.room.history_visibility"],"lazy_load_members":true},"timeline":{"limit":3,"lazy_load_members":true},"ephemeral":{"limit":0,"types":[""],"lazy_load_members":true},"account_data":{"limit":1,"types":["m.fully_read"],"lazy_load_members":true}}} // Filter without account data - const char *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\":3,\"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}}}"; + std::string filter = get_filter_cached(); const std::string filter_encoded = url_param_encode(filter); std::vector<CommandArg> additional_args = { @@ -2753,6 +2754,8 @@ namespace QuickMedia { Path matrix_sync_data_path = get_cache_dir().join("matrix").join("sync_data.json"); remove(matrix_sync_data_path.data.c_str()); + Path filter_cache_path = get_storage_dir().join("matrix").join("filter"); + remove(filter_cache_path.data.c_str()); if(!json_root.IsObject()) { err_msg = "Failed to parse matrix login response"; @@ -2822,6 +2825,7 @@ namespace QuickMedia { upload_limit.reset(); set_next_batch(""); invites.clear(); + filter_cached.reset(); return PluginResult::OK; } @@ -3189,6 +3193,46 @@ namespace QuickMedia { #endif } + // TODO: GET the filter to check if its valid? + std::string Matrix::get_filter_cached() { + if(filter_cached) + return filter_cached.value(); + + Path filter_path = get_storage_dir().join("matrix").join("filter"); + std::string file_content; + if(file_get_content(filter_path, file_content) == 0) { + fprintf(stderr, "Loaded filter from cache\n"); + filter_cached = file_content; + return filter_cached.value(); + } + + fprintf(stderr, "Filter not available in cache, adding to server\n"); + + std::vector<CommandArg> additional_args = { + { "-X", "POST" }, + { "-H", "Authorization: Bearer " + access_token }, + { "--data-binary", FILTER } + }; + + char url[512]; + snprintf(url, sizeof(url), "%s/_matrix/client/r0/user/%s/filter", homeserver.c_str(), user_id.c_str()); + + rapidjson::Document json_root; + DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true); + if(download_result != DownloadResult::OK || !json_root.IsObject()) return FILTER; + + const rapidjson::Value &filter_id_json = GetMember(json_root, "filter_id"); + if(!filter_id_json.IsString()) + return FILTER; + + filter_cached = filter_id_json.GetString(); + Path filter_dir = get_storage_dir().join("matrix"); + if(create_directory_recursive(filter_dir) == 0) { + file_overwrite_atomic(filter_path, filter_cached.value()); + } + return filter_cached.value(); + } + DownloadResult Matrix::download_json(rapidjson::Document &result, const std::string &url, std::vector<CommandArg> additional_args, bool use_browser_useragent, std::string *err_msg) const { if(download_to_json(url, result, std::move(additional_args), use_tor, use_browser_useragent, err_msg == nullptr) != DownloadResult::OK) { // Cant get error since we parse directly to json. TODO: Make this work somehow? |