aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Matrix.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-10-01 07:34:47 +0200
committerdec05eba <dec05eba@protonmail.com>2020-10-01 07:34:47 +0200
commitae02ed04389117d100070c47f3ae69abb9080231 (patch)
treedadda40c41a219c56d3bebcc6b26f7bc2ee64fc1 /src/plugins/Matrix.cpp
parent211982f321830691038cb79698fb55dcd015c9fc (diff)
Matrix: add message delete with ctrl+d
Diffstat (limited to 'src/plugins/Matrix.cpp')
-rw-r--r--src/plugins/Matrix.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index b93164e..b5eb617 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -1255,6 +1255,69 @@ namespace QuickMedia {
return PluginResult::OK;
}
+ PluginResult Matrix::delete_message(const std::string &room_id, void *message, std::string &err_msg){
+ char random_characters[18];
+ if(!generate_random_characters(random_characters, sizeof(random_characters)))
+ return PluginResult::ERR;
+
+ std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters));
+
+ Message *message_typed = (Message*)message;
+
+ // request_data could contains "reason", maybe it should be added sometime in the future?
+ Json::Value request_data(Json::objectValue);
+ Json::StreamWriterBuilder builder;
+ builder["commentStyle"] = "None";
+ builder["indentation"] = "";
+
+ std::vector<CommandArg> additional_args = {
+ { "-X", "PUT" },
+ { "-H", "content-type: application/json" },
+ { "-H", "Authorization: Bearer " + access_token },
+ { "--data-binary", Json::writeString(builder, std::move(request_data)) }
+ };
+
+ 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());
+ fprintf(stderr, "load initial room data, url: |%s|\n", url);
+
+ std::string server_response;
+ if(download_to_string(url, server_response, std::move(additional_args), use_tor, true, false) != DownloadResult::OK) {
+ err_msg = std::move(server_response);
+ return PluginResult::NET_ERR;
+ }
+
+ if(server_response.empty())
+ return PluginResult::ERR;
+
+ Json::Value json_root;
+ 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()], &json_root, &json_errors)) {
+ err_msg = "Matrix delete message response parse error: " + json_errors;
+ return PluginResult::ERR;
+ }
+
+ if(!json_root.isObject()) {
+ err_msg = "Failed to parse matrix login response";
+ return PluginResult::ERR;
+ }
+
+ const Json::Value &error = json_root["error"];
+ if(error.isString()) {
+ err_msg = error.asString();
+ return PluginResult::ERR;
+ }
+
+ const Json::Value &event_id_json = json_root["event_id"];
+ if(!event_id_json.isString())
+ return PluginResult::ERR;
+
+ fprintf(stderr, "Matrix delete message, response event id: %s\n", event_id_json.asCString());
+ return PluginResult::OK;
+ }
+
PluginResult Matrix::load_and_verify_cached_session() {
Path session_path = get_storage_dir().join(name).join("session.json");
std::string session_json_content;