diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Matrix.cpp | 297 |
1 files changed, 114 insertions, 183 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 18a31a0..0866bac 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -53,30 +53,24 @@ namespace QuickMedia { return user->read_marker_event_id; } - size_t RoomData::prepend_messages_reverse(std::vector<std::shared_ptr<Message>> new_messages) { + void RoomData::prepend_messages_reverse(std::vector<std::shared_ptr<Message>> new_messages) { std::lock_guard<std::mutex> lock(room_mutex); - size_t num_inserted = 0; for(auto it = new_messages.begin(); it != new_messages.end(); ++it) { if(message_by_event_id.find((*it)->event_id) == message_by_event_id.end()) { message_by_event_id.insert(std::make_pair((*it)->event_id, *it)); messages.insert(messages.begin(), std::move(*it)); - ++num_inserted; } } - return num_inserted; } - size_t RoomData::append_messages(std::vector<std::shared_ptr<Message>> new_messages) { + void RoomData::append_messages(std::vector<std::shared_ptr<Message>> new_messages) { std::lock_guard<std::mutex> lock(room_mutex); - size_t num_inserted = 0; for(auto it = new_messages.begin(); it != new_messages.end(); ++it) { if(message_by_event_id.find((*it)->event_id) == message_by_event_id.end()) { message_by_event_id.insert(std::make_pair((*it)->event_id, *it)); messages.push_back(std::move(*it)); - ++num_inserted; } } - return num_inserted; } std::shared_ptr<Message> RoomData::get_message_by_id(const std::string &id) { @@ -141,7 +135,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::get_joined_rooms(BodyItems &result_items) { + PluginResult Matrix::get_joined_rooms(Rooms &rooms) { std::vector<CommandArg> additional_args = { { "-H", "Authorization: Bearer " + access_token } }; @@ -162,114 +156,28 @@ namespace QuickMedia { continue; std::string room_id_str = room_id_json.GetString(); - std::string room_name; - std::string avatar_url; - auto room = get_room_by_id(room_id_str); if(!room) { room = std::make_shared<RoomData>(); room->id = room_id_json.GetString(); add_room(std::move(room)); - room_name = room_id_str; fprintf(stderr, "Missing room %s from /sync, adding in joined_rooms\n", room_id_str.c_str()); - } else { - room_name = room->name; - if(room_name.empty()) - room_name = room_id_str; - avatar_url = room->avatar_url; } - auto body_item = BodyItem::create(std::move(room_name)); - body_item->url = room_id_str; - body_item->thumbnail_url = std::move(avatar_url); - result_items.push_back(std::move(body_item)); - } - - return PluginResult::OK; - } - - static void room_messages_to_body_items(const std::shared_ptr<Message> *messages, size_t num_messages, BodyItems &result_items) { - for(size_t i = 0; i < num_messages; ++i) { - auto body_item = BodyItem::create(""); - body_item->set_author(messages[i]->user->display_name); - body_item->set_description(messages[i]->body); - body_item->set_timestamp(messages[i]->timestamp); - if(!messages[i]->thumbnail_url.empty()) - body_item->thumbnail_url = messages[i]->thumbnail_url; - else if(!messages[i]->url.empty() && messages[i]->type == MessageType::IMAGE) - body_item->thumbnail_url = messages[i]->url; - else - body_item->thumbnail_url = messages[i]->user->avatar_url; - // TODO: Show image thumbnail inline instead of url to image and showing it as the thumbnail of the body item - body_item->url = messages[i]->url; - body_item->author_color = messages[i]->user->display_name_color; - body_item->userdata = (void*)messages[i].get(); // Note: messages[i] has to be valid as long as body_item is used! - result_items.push_back(std::move(body_item)); - } - } - - // TODO: Merge common code with |get_new_room_messages| - PluginResult Matrix::get_all_synced_room_messages(const std::string &room_id, BodyItems &result_items) { - auto room = get_room_by_id(room_id); - if(!room) { - fprintf(stderr, "Error: no such room: %s\n", room_id.c_str()); - return PluginResult::ERR; + rooms.push_back(room); } - // TODO: Thread safe? - /* - if(!room->initial_fetch_finished) { - PluginResult result = get_previous_room_messages(room); - if(result == PluginResult::OK) { - room->initial_fetch_finished = true; - } else { - fprintf(stderr, "Initial sync failed for room: %s\n", room_id.c_str()); - return result; - } - } - */ - - room->acquire_room_lock(); - room_messages_to_body_items(room->get_messages_thread_unsafe().data(), room->get_messages_thread_unsafe().size(), result_items); - room->last_read_index = room->get_messages_thread_unsafe().size(); - room->release_room_lock(); return PluginResult::OK; } - PluginResult Matrix::get_new_room_messages(const std::string &room_id, BodyItems &result_items) { - auto room = get_room_by_id(room_id); - if(!room) { - fprintf(stderr, "Error: no such room: %s\n", room_id.c_str()); - return PluginResult::ERR; - } - - /* - if(!room->initial_fetch_finished) { - PluginResult result = get_previous_room_messages(room); - if(result == PluginResult::OK) { - room->initial_fetch_finished = true; - } else { - fprintf(stderr, "Initial sync failed for room: %s\n", room_id.c_str()); - return result; - } - } - */ - + PluginResult Matrix::get_all_synced_room_messages(std::shared_ptr<RoomData> room, Messages &messages) { room->acquire_room_lock(); - size_t num_new_messages = room->get_messages_thread_unsafe().size() - room->last_read_index; - room_messages_to_body_items(room->get_messages_thread_unsafe().data() + room->last_read_index, num_new_messages, result_items); - room->last_read_index = room->get_messages_thread_unsafe().size(); + messages = room->get_messages_thread_unsafe(); room->release_room_lock(); return PluginResult::OK; } - PluginResult Matrix::get_previous_room_messages(const std::string &room_id, BodyItems &result_items) { - auto room = get_room_by_id(room_id); - if(!room) { - fprintf(stderr, "Error: no such room: %s\n", room_id.c_str()); - return PluginResult::ERR; - } - + PluginResult Matrix::get_previous_room_messages(std::shared_ptr<RoomData> room, Messages &messages) { room->acquire_room_lock(); size_t num_messages_before = room->get_messages_thread_unsafe().size(); room->release_room_lock(); @@ -280,7 +188,7 @@ namespace QuickMedia { room->acquire_room_lock(); size_t num_messages_after = room->get_messages_thread_unsafe().size(); size_t num_new_messages = num_messages_after - num_messages_before; - room_messages_to_body_items(room->get_messages_thread_unsafe().data(), num_new_messages, result_items); + messages.insert(messages.end(), room->get_messages_thread_unsafe().begin(), room->get_messages_thread_unsafe().begin() + num_new_messages); room->release_room_lock(); return PluginResult::OK; } @@ -536,20 +444,12 @@ namespace QuickMedia { if(!events_json.IsArray()) return; - std::vector<std::shared_ptr<Message>> *room_sync_messages = nullptr; - if(room_messages) - room_sync_messages = &(*room_messages)[room_data]; - std::vector<std::shared_ptr<Message>> new_messages; for(const rapidjson::Value &event_item_json : events_json.GetArray()) { if(!event_item_json.IsObject()) continue; - const rapidjson::Value &type_json = GetMember(event_item_json, "type"); - if(!type_json.IsString() || strcmp(type_json.GetString(), "m.room.message") != 0) - continue; - const rapidjson::Value &sender_json = GetMember(event_item_json, "sender"); if(!sender_json.IsString()) continue; @@ -562,12 +462,8 @@ namespace QuickMedia { std::string event_id_str = event_id_json.GetString(); - const rapidjson::Value &content_json = GetMember(event_item_json, "content"); - if(!content_json.IsObject()) - continue; - - const rapidjson::Value &content_type = GetMember(content_json, "msgtype"); - if(!content_type.IsString()) + const rapidjson::Value *content_json = &GetMember(event_item_json, "content"); + if(!content_json->IsObject()) continue; auto user = room_data->get_user_by_id(sender_json_str); @@ -577,24 +473,71 @@ namespace QuickMedia { continue; } - const rapidjson::Value &body_json = GetMember(content_json, "body"); - if(!body_json.IsString()) - continue; - time_t timestamp = 0; const rapidjson::Value &origin_server_ts = GetMember(event_item_json, "origin_server_ts"); if(origin_server_ts.IsNumber()) timestamp = origin_server_ts.GetInt64(); - std::string replaces_event_id; - const rapidjson::Value &relates_to_json = GetMember(content_json, "m.relates_to"); + const rapidjson::Value &type_json = GetMember(event_item_json, "type"); + if(!type_json.IsString()) + continue; + + RelatedEventType related_event_type = RelatedEventType::NONE; + std::string related_event_id; + const rapidjson::Value &relates_to_json = GetMember(*content_json, "m.relates_to"); if(relates_to_json.IsObject()) { const rapidjson::Value &replaces_event_id_json = GetMember(relates_to_json, "event_id"); const rapidjson::Value &rel_type_json = GetMember(relates_to_json, "rel_type"); - if(replaces_event_id_json.IsString() && rel_type_json.IsString() && strcmp(rel_type_json.GetString(), "m.replace") == 0) - replaces_event_id = replaces_event_id_json.GetString(); + if(replaces_event_id_json.IsString() && rel_type_json.IsString() && strcmp(rel_type_json.GetString(), "m.replace") == 0) { + related_event_id = replaces_event_id_json.GetString(); + related_event_type = RelatedEventType::EDIT; + } else { + const rapidjson::Value &in_reply_to_json = GetMember(relates_to_json, "m.in_reply_to"); + if(in_reply_to_json.IsObject()) { + const rapidjson::Value &in_reply_to_event_id = GetMember(in_reply_to_json, "event_id"); + if(in_reply_to_event_id.IsString()) { + related_event_id = in_reply_to_event_id.GetString(); + related_event_type = RelatedEventType::REPLY; + } + } + } + } + + const rapidjson::Value &new_content_json = GetMember(*content_json, "m.new_content"); + if(new_content_json.IsObject()) + content_json = &new_content_json; + + const rapidjson::Value &content_type = GetMember(*content_json, "msgtype"); + if(!content_type.IsString() || strcmp(type_json.GetString(), "m.room.redaction") == 0) { + auto message = std::make_shared<Message>(); + message->type = MessageType::REDACTION; + message->user = user; + message->event_id = event_id_str; + message->body = "Message deleted"; + message->timestamp = timestamp; + message->related_event_type = RelatedEventType::REDACTION; + + const rapidjson::Value &reason_json = GetMember(*content_json, "reason"); + if(reason_json.IsString()) { + message->body += ", reason: "; + message->body += reason_json.GetString(); + } + + const rapidjson::Value &redacts_json = GetMember(event_item_json, "redacts"); + if(redacts_json.IsString()) + message->related_event_id = redacts_json.GetString(); + + new_messages.push_back(message); + continue; } + if(strcmp(type_json.GetString(), "m.room.message") != 0) + continue; + + const rapidjson::Value &body_json = GetMember(*content_json, "body"); + if(!body_json.IsString()) + continue; + auto message = std::make_shared<Message>(); std::string prefix; @@ -603,30 +546,30 @@ namespace QuickMedia { if(strcmp(content_type.GetString(), "m.text") == 0) { message->type = MessageType::TEXT; } else if(strcmp(content_type.GetString(), "m.image") == 0) { - const rapidjson::Value &url_json = GetMember(content_json, "url"); + const rapidjson::Value &url_json = GetMember(*content_json, "url"); if(!url_json.IsString() || strncmp(url_json.GetString(), "mxc://", 6) != 0) continue; message->url = homeserver + "/_matrix/media/r0/download/" + (url_json.GetString() + 6); - message->thumbnail_url = message_content_extract_thumbnail_url(content_json, homeserver); + message->thumbnail_url = message_content_extract_thumbnail_url(*content_json, homeserver); message->type = MessageType::IMAGE; } else if(strcmp(content_type.GetString(), "m.video") == 0) { - const rapidjson::Value &url_json = GetMember(content_json, "url"); + const rapidjson::Value &url_json = GetMember(*content_json, "url"); if(!url_json.IsString() || strncmp(url_json.GetString(), "mxc://", 6) != 0) continue; message->url = homeserver + "/_matrix/media/r0/download/" + (url_json.GetString() + 6); - message->thumbnail_url = message_content_extract_thumbnail_url(content_json, homeserver); + message->thumbnail_url = message_content_extract_thumbnail_url(*content_json, homeserver); message->type = MessageType::VIDEO; } else if(strcmp(content_type.GetString(), "m.audio") == 0) { - const rapidjson::Value &url_json = GetMember(content_json, "url"); + const rapidjson::Value &url_json = GetMember(*content_json, "url"); if(!url_json.IsString() || strncmp(url_json.GetString(), "mxc://", 6) != 0) continue; message->url = homeserver + "/_matrix/media/r0/download/" + (url_json.GetString() + 6); message->type = MessageType::AUDIO; } else if(strcmp(content_type.GetString(), "m.file") == 0) { - const rapidjson::Value &url_json = GetMember(content_json, "url"); + const rapidjson::Value &url_json = GetMember(*content_json, "url"); if(!url_json.IsString() || strncmp(url_json.GetString(), "mxc://", 6) != 0) continue; @@ -639,12 +582,12 @@ namespace QuickMedia { message->type = MessageType::TEXT; prefix = "* NOTICE * "; } else if(strcmp(content_type.GetString(), "m.location") == 0) { // TODO: show locations differently - const rapidjson::Value &geo_uri_json = GetMember(content_json, "geo_uri"); + const rapidjson::Value &geo_uri_json = GetMember(*content_json, "geo_uri"); if(geo_uri_json.IsString()) prefix = geo_uri_json.GetString() + std::string(" | "); message->type = MessageType::TEXT; - message->thumbnail_url = message_content_extract_thumbnail_url(content_json, homeserver); + message->thumbnail_url = message_content_extract_thumbnail_url(*content_json, homeserver); } else { continue; } @@ -652,22 +595,22 @@ namespace QuickMedia { message->user = user; message->event_id = event_id_str; message->body = prefix + body_json.GetString(); - message->replaces_event_id = std::move(replaces_event_id); + message->related_event_id = std::move(related_event_id); + message->related_event_type = related_event_type; // TODO: Is @room ok? shouldn't we also check if the user has permission to do @room? (only when notifications are limited to @mentions) if(has_unread_notifications && !username.empty()) message->mentions_me = message_contains_user_mention(message->body, username) || message_contains_user_mention(message->body, "@room"); message->timestamp = timestamp; new_messages.push_back(message); - if(room_sync_messages) - room_sync_messages->push_back(message); } + // TODO: Add directly to this instead when set? otherwise add to new_messages + if(room_messages) + (*room_messages)[room_data] = new_messages; + // TODO: Loop and std::move instead? doesn't insert create copies? if(message_dir == MessageDirection::BEFORE) { - size_t num_inserted_messages = room_data->prepend_messages_reverse(std::move(new_messages)); - // TODO: Is this thread-safe? - if(room_data->last_read_index != 0) - room_data->last_read_index += num_inserted_messages; + room_data->prepend_messages_reverse(std::move(new_messages)); } else if(message_dir == MessageDirection::AFTER) { room_data->append_messages(std::move(new_messages)); } @@ -860,7 +803,7 @@ namespace QuickMedia { return "m.file"; } - PluginResult Matrix::post_message(const std::string &room_id, const std::string &body, const std::optional<UploadInfo> &file_info, const std::optional<UploadInfo> &thumbnail_info) { + PluginResult Matrix::post_message(std::shared_ptr<RoomData> room, const std::string &body, const std::optional<UploadInfo> &file_info, const std::optional<UploadInfo> &thumbnail_info) { char random_characters[18]; if(!generate_random_characters(random_characters, sizeof(random_characters))) return PluginResult::ERR; @@ -940,7 +883,7 @@ namespace QuickMedia { }; char request_url[512]; - snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/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); @@ -987,9 +930,7 @@ namespace QuickMedia { std::string related_to_body; switch(message->type) { case MessageType::TEXT: { - if(!message->replaces_event_id.empty() && strncmp(message->body.c_str(), " * ", 3) == 0) - related_to_body = remove_reply_formatting(message->body.substr(3)); - else + if(message->related_event_type != RelatedEventType::NONE) related_to_body = remove_reply_formatting(message->body); break; } @@ -1005,6 +946,9 @@ namespace QuickMedia { case MessageType::FILE: related_to_body = "sent a file"; break; + case MessageType::REDACTION: + related_to_body = message->body; + break; } return related_to_body; } @@ -1027,13 +971,7 @@ namespace QuickMedia { } // TODO: Support greentext - PluginResult Matrix::post_reply(const std::string &room_id, const std::string &body, void *relates_to) { - auto room = get_room_by_id(room_id); - if(!room) { - fprintf(stderr, "Error: no such room: %s\n", room_id.c_str()); - return PluginResult::ERR; - } - + PluginResult Matrix::post_reply(std::shared_ptr<RoomData> room, const std::string &body, void *relates_to) { // TODO: Store shared_ptr<Message> instead of raw pointer... Message *relates_to_message_raw = (Message*)relates_to; std::shared_ptr<Message> relates_to_message_shared = room->get_message_by_id(relates_to_message_raw->event_id); @@ -1077,7 +1015,7 @@ namespace QuickMedia { }; char request_url[512]; - snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/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); @@ -1094,13 +1032,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::post_edit(const std::string &room_id, const std::string &body, void *relates_to) { - auto room = get_room_by_id(room_id); - if(!room) { - fprintf(stderr, "Error: no such room: %s\n", room_id.c_str()); - return PluginResult::ERR; - } - + PluginResult Matrix::post_edit(std::shared_ptr<RoomData> room, const std::string &body, void *relates_to) { Message *relates_to_message_raw = (Message*)relates_to; std::shared_ptr<Message> relates_to_message_shared = room->get_message_by_id(relates_to_message_raw->event_id); std::shared_ptr<Message> relates_to_message_original = get_edited_message_original_message(room.get(), relates_to_message_shared); @@ -1173,7 +1105,7 @@ namespace QuickMedia { }; char request_url[512]; - snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str()); + snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/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); @@ -1193,10 +1125,10 @@ namespace QuickMedia { // TODO: Right now this recursively calls /rooms/<room_id>/context/<event_id> and trusts server to not make it recursive. To make this robust, check iteration count and do not trust server. // TODO: Optimize? std::shared_ptr<Message> Matrix::get_edited_message_original_message(RoomData *room_data, std::shared_ptr<Message> message) { - if(message->replaces_event_id.empty()) + if(message->related_event_type != RelatedEventType::EDIT) return message; - auto replaced_message = room_data->get_message_by_id(message->replaces_event_id); + auto replaced_message = room_data->get_message_by_id(message->related_event_id); if(!replaced_message) { rapidjson::Document request_data(rapidjson::kObjectType); request_data.AddMember("lazy_load_members", true, request_data.GetAllocator()); @@ -1237,13 +1169,16 @@ namespace QuickMedia { if(!body_json.IsString()) return nullptr; - std::string replaces_event_id; + RelatedEventType related_event_type = RelatedEventType::NONE; + std::string related_event_id; const rapidjson::Value &relates_to_json = GetMember(content_json, "m.relates_to"); if(relates_to_json.IsObject()) { const rapidjson::Value &event_id_json = GetMember(relates_to_json, "event_id"); const rapidjson::Value &rel_type_json = GetMember(relates_to_json, "rel_type"); - if(event_id_json.IsString() && rel_type_json.IsString() && strcmp(rel_type_json.GetString(), "m.replace") == 0) - replaces_event_id = event_id_json.GetString(); + if(event_id_json.IsString() && rel_type_json.IsString() && strcmp(rel_type_json.GetString(), "m.replace") == 0) { + related_event_id = event_id_json.GetString(); + related_event_type = RelatedEventType::EDIT; + } } const rapidjson::Value &content_type = GetMember(content_json, "msgtype"); @@ -1252,7 +1187,8 @@ namespace QuickMedia { auto new_message = std::make_shared<Message>(); new_message->event_id = event_id_json.GetString(); - new_message->replaces_event_id = std::move(replaces_event_id); + new_message->related_event_id = std::move(related_event_id); + new_message->related_event_type = related_event_type; if(strcmp(content_type.GetString(), "m.text") == 0) { new_message->type = MessageType::TEXT; } else if(strcmp(content_type.GetString(), "m.image") == 0) { @@ -1284,10 +1220,10 @@ namespace QuickMedia { return filename; } - PluginResult Matrix::post_file(const std::string &room_id, const std::string &filepath, std::string &err_msg) { + PluginResult Matrix::post_file(std::shared_ptr<RoomData> room, const std::string &filepath, std::string &err_msg) { UploadInfo file_info; UploadInfo thumbnail_info; - PluginResult upload_file_result = upload_file(room_id, filepath, file_info, thumbnail_info, err_msg); + PluginResult upload_file_result = upload_file(room, filepath, file_info, thumbnail_info, err_msg); if(upload_file_result != PluginResult::OK) return upload_file_result; @@ -1297,10 +1233,10 @@ namespace QuickMedia { thumbnail_info_opt = std::move(thumbnail_info); const char *filename = file_get_filename(filepath); - return post_message(room_id, filename, file_info_opt, thumbnail_info_opt); + return post_message(room, filename, file_info_opt, thumbnail_info_opt); } - PluginResult Matrix::upload_file(const std::string &room_id, const std::string &filepath, UploadInfo &file_info, UploadInfo &thumbnail_info, std::string &err_msg) { + PluginResult Matrix::upload_file(std::shared_ptr<RoomData> room, const std::string &filepath, UploadInfo &file_info, UploadInfo &thumbnail_info, std::string &err_msg) { FileAnalyzer file_analyzer; if(!file_analyzer.load_file(filepath.c_str())) { err_msg = "Failed to load " + filepath; @@ -1337,7 +1273,7 @@ namespace QuickMedia { if(tmp_file != -1) { if(video_get_first_frame(filepath.c_str(), tmp_filename)) { UploadInfo upload_info_ignored; // Ignore because it wont be set anyways. Thumbnails dont have thumbnails. - PluginResult upload_thumbnail_result = upload_file(room_id, tmp_filename, thumbnail_info, upload_info_ignored, err_msg); + PluginResult upload_thumbnail_result = upload_file(room, tmp_filename, thumbnail_info, upload_info_ignored, err_msg); if(upload_thumbnail_result != PluginResult::OK) { close(tmp_file); remove(tmp_filename); @@ -1489,7 +1425,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::delete_message(const std::string &room_id, void *message, std::string &err_msg){ + PluginResult Matrix::delete_message(std::shared_ptr<RoomData> room, void *message, std::string &err_msg){ char random_characters[18]; if(!generate_random_characters(random_characters, sizeof(random_characters))) return PluginResult::ERR; @@ -1512,7 +1448,7 @@ namespace QuickMedia { }; 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()); + 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()); rapidjson::Document json_root; DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true, &err_msg); @@ -1595,7 +1531,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::on_start_typing(const std::string &room_id) { + PluginResult Matrix::on_start_typing(std::shared_ptr<RoomData> room) { rapidjson::Document request_data(rapidjson::kObjectType); request_data.AddMember("typing", true, request_data.GetAllocator()); request_data.AddMember("timeout", 30000, request_data.GetAllocator()); // 30 sec timeout @@ -1612,13 +1548,13 @@ namespace QuickMedia { }; std::string server_response; - if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room_id + "/typing/" + url_param_encode(user_id) , server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) + if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room->id + "/typing/" + url_param_encode(user_id) , server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) return PluginResult::NET_ERR; return PluginResult::OK; } - PluginResult Matrix::on_stop_typing(const std::string &room_id) { + PluginResult Matrix::on_stop_typing(std::shared_ptr<RoomData> room) { rapidjson::Document request_data(rapidjson::kObjectType); request_data.AddMember("typing", false, request_data.GetAllocator()); @@ -1634,13 +1570,13 @@ namespace QuickMedia { }; std::string server_response; - if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room_id + "/typing/" + url_param_encode(user_id), server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) + if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room->id + "/typing/" + url_param_encode(user_id), server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) return PluginResult::NET_ERR; return PluginResult::OK; } - PluginResult Matrix::set_read_marker(const std::string &room_id, const Message *message) { + PluginResult Matrix::set_read_marker(std::shared_ptr<RoomData> room, const Message *message) { rapidjson::Document request_data(rapidjson::kObjectType); request_data.AddMember("m.fully_read", rapidjson::StringRef(message->event_id.c_str()), request_data.GetAllocator()); request_data.AddMember("m.read", rapidjson::StringRef(message->event_id.c_str()), request_data.GetAllocator()); @@ -1658,7 +1594,7 @@ namespace QuickMedia { }; std::string server_response; - if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room_id + "/read_markers", server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) + if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room->id + "/read_markers", server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) return PluginResult::NET_ERR; return PluginResult::OK; @@ -1706,12 +1642,7 @@ namespace QuickMedia { return PluginResult::OK; } - std::shared_ptr<UserInfo> Matrix::get_me(const std::string &room_id) { - auto room = get_room_by_id(room_id); - if(!room) { - fprintf(stderr, "Error: no such room: %s\n", room_id.c_str()); - return nullptr; - } + std::shared_ptr<UserInfo> Matrix::get_me(std::shared_ptr<RoomData> room) { return room->get_user_by_id(user_id); } |