diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-10-11 21:35:37 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-10-13 13:13:01 +0200 |
commit | 77ed51898157d99112be7550471ec06e32344c9e (patch) | |
tree | 0645274d0f13b4fa6940d4054f74a070611a8ef0 /src/plugins/Matrix.cpp | |
parent | da89ec98fb34757f0c46dc8cb2dd87ae78d317ce (diff) |
Refactor plugin into seperate pages
TODO: Readd 4chan login page, manganelo creators page, autocomplete
Diffstat (limited to 'src/plugins/Matrix.cpp')
-rw-r--r-- | src/plugins/Matrix.cpp | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 2107812..dec4a68 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -1,5 +1,6 @@ #include "../../plugins/Matrix.hpp" #include "../../include/Storage.hpp" +#include "../../include/StringUtils.hpp" #include <json/reader.h> #include <json/writer.h> #include <fcntl.h> @@ -18,6 +19,8 @@ // TODO: Verify if this class really is thread-safe (for example room data fields, user fields, message fields; etc that are updated in /sync) +static const char* SERVICE_NAME = "matrix"; + namespace QuickMedia { std::shared_ptr<UserInfo> RoomData::get_user_by_id(const std::string &user_id) { std::lock_guard<std::mutex> lock(room_mutex); @@ -99,10 +102,6 @@ namespace QuickMedia { return messages; } - Matrix::Matrix() : Plugin("matrix") { - - } - PluginResult Matrix::sync(RoomSyncMessages &room_messages) { std::vector<CommandArg> additional_args = { { "-H", "Authorization: Bearer " + access_token }, @@ -819,10 +818,6 @@ namespace QuickMedia { return PluginResult::OK; } - SearchResult Matrix::search(const std::string&, BodyItems&) { - return SearchResult::OK; - } - static bool generate_random_characters(char *buffer, int buffer_size) { int fd = open("/dev/urandom", O_RDONLY); if(fd == -1) { @@ -1443,7 +1438,7 @@ namespace QuickMedia { // TODO: Handle well_known field. The spec says clients SHOULD handle it if its provided - Path session_path = get_storage_dir().join(name); + Path session_path = get_storage_dir().join(SERVICE_NAME); if(create_directory_recursive(session_path) == 0) { session_path.join("session.json"); if(!save_json_to_file_atomic(session_path, json_root)) { @@ -1457,7 +1452,7 @@ namespace QuickMedia { } PluginResult Matrix::logout() { - Path session_path = get_storage_dir().join(name).join("session.json"); + Path session_path = get_storage_dir().join(SERVICE_NAME).join("session.json"); remove(session_path.data.c_str()); std::vector<CommandArg> additional_args = { @@ -1530,7 +1525,7 @@ namespace QuickMedia { } PluginResult Matrix::load_and_verify_cached_session() { - Path session_path = get_storage_dir().join(name).join("session.json"); + Path session_path = get_storage_dir().join(SERVICE_NAME).join("session.json"); std::string session_json_content; if(file_get_content(session_path, session_json_content) != 0) { fprintf(stderr, "Info: failed to read matrix session from %s. Either its missing or we failed to read the file\n", session_path.data.c_str()); @@ -1721,4 +1716,28 @@ namespace QuickMedia { std::lock_guard<std::mutex> lock(room_data_mutex); room_data_by_id.insert(std::make_pair(room->id, room)); } + + DownloadResult Matrix::download_json(Json::Value &result, const std::string &url, std::vector<CommandArg> additional_args, bool use_browser_useragent, std::string *err_msg) const { + std::string server_response; + if(download_to_string(url, server_response, std::move(additional_args), use_tor, use_browser_useragent, err_msg == nullptr) != DownloadResult::OK) { + if(err_msg) + *err_msg = server_response; + return DownloadResult::NET_ERR; + } + + if(server_response.empty()) + return DownloadResult::OK; + + Json::CharReaderBuilder json_builder; + std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader()); + std::string json_errors; + if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &result, &json_errors)) { + fprintf(stderr, "download_json error: %s\n", json_errors.c_str()); + if(err_msg) + *err_msg = std::move(json_errors); + return DownloadResult::ERR; + } + + return DownloadResult::OK; + } }
\ No newline at end of file |