From 818f65e1d9e21a2b0dcecf34312b217000da7c92 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 22 Nov 2020 11:46:01 +0100 Subject: Matrix: add /react --- src/plugins/Matrix.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/plugins/Matrix.cpp') diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index e5e4922..05881d5 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -2733,6 +2733,53 @@ namespace QuickMedia { return PluginResult::OK; } + PluginResult Matrix::post_reaction(RoomData *room, const std::string &body, void *relates_to, std::string &event_id_response) { + Message *relates_to_message_raw = (Message*)relates_to; + + 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)); + + rapidjson::Document relates_to_json(rapidjson::kObjectType); + relates_to_json.AddMember("event_id", rapidjson::StringRef(relates_to_message_raw->event_id.c_str()), relates_to_json.GetAllocator()); + relates_to_json.AddMember("key", rapidjson::StringRef(body.c_str()), relates_to_json.GetAllocator()); + relates_to_json.AddMember("rel_type", "m.annotation", relates_to_json.GetAllocator()); + + rapidjson::Document request_json(rapidjson::kObjectType); + request_json.AddMember("m.relates_to", std::move(relates_to_json), request_json.GetAllocator()); + + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + request_json.Accept(writer); + + std::vector additional_args = { + { "-X", "PUT" }, + { "-H", "content-type: application/json" }, + { "-H", "Authorization: Bearer " + access_token }, + { "--data-binary", buffer.GetString() } + }; + + char request_url[512]; + snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.reaction/m%ld.%.*s", homeserver.c_str(), room->id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + + rapidjson::Document json_root; + DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); + + if(!json_root.IsObject()) + return PluginResult::ERR; + + const rapidjson::Value &event_id_json = GetMember(json_root, "event_id"); + if(!event_id_json.IsString()) + return PluginResult::ERR; + + fprintf(stderr, "Matrix post reaction, response event id: %s\n", event_id_json.GetString()); + event_id_response = std::string(event_id_json.GetString(), event_id_json.GetStringLength()); + return PluginResult::OK; + } + std::shared_ptr Matrix::get_message_by_id(RoomData *room, const std::string &event_id) { std::shared_ptr existing_room_message = room->get_message_by_id(event_id); if(existing_room_message) -- cgit v1.2.3