diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Matrix.cpp | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index b5948d2..13d42bb 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -3387,7 +3387,7 @@ namespace QuickMedia { "</mx-reply>" + std::move(formatted_body); } - PluginResult Matrix::post_reply(RoomData *room, const std::string &body, void *relates_to, std::string &event_id_response, const std::string &custom_transaction_id) { + PluginResult Matrix::post_reply(RoomData *room, const std::string &body, void *relates_to, std::string &event_id_response, const std::string &custom_transaction_id, const std::optional<UploadInfo> &file_info, const std::optional<UploadInfo> &thumbnail_info) { Message *relates_to_message_raw = (Message*)relates_to; std::string transaction_id = custom_transaction_id; @@ -3397,7 +3397,8 @@ namespace QuickMedia { if(transaction_id.empty()) return PluginResult::ERR; - my_events_transaction_ids.insert(transaction_id); + if(!file_info) + my_events_transaction_ids.insert(transaction_id); rapidjson::Document in_reply_to_json(rapidjson::kObjectType); in_reply_to_json.AddMember("event_id", rapidjson::StringRef(relates_to_message_raw->event_id.c_str()), in_reply_to_json.GetAllocator()); @@ -3409,12 +3410,43 @@ namespace QuickMedia { std::string formatted_message_reply_body = create_formatted_body_for_message_reply(room, relates_to_message_raw, body); rapidjson::Document request_data(rapidjson::kObjectType); - request_data.AddMember("msgtype", "m.text", request_data.GetAllocator()); // TODO: Allow image reply? element doesn't do that but we could! + request_data.AddMember("msgtype", rapidjson::StringRef(file_info ? content_type_to_message_type(file_info->content_type) : "m.text"), request_data.GetAllocator()); request_data.AddMember("body", rapidjson::StringRef(message_reply_body.c_str()), request_data.GetAllocator()); request_data.AddMember("format", "org.matrix.custom.html", request_data.GetAllocator()); request_data.AddMember("formatted_body", rapidjson::StringRef(formatted_message_reply_body.c_str()), request_data.GetAllocator()); request_data.AddMember("m.relates_to", std::move(relates_to_json), request_data.GetAllocator()); + // TODO: Add hashblur? + if(file_info) { + rapidjson::Value info_json(rapidjson::kObjectType); + info_json.AddMember("size", file_info->file_size, request_data.GetAllocator()); + info_json.AddMember("mimetype", rapidjson::StringRef(content_type_to_string(file_info->content_type)), request_data.GetAllocator()); + if(file_info->dimensions) { + info_json.AddMember("w", file_info->dimensions->width, request_data.GetAllocator()); + info_json.AddMember("h", file_info->dimensions->height, request_data.GetAllocator()); + } + if(file_info->duration_seconds) { + // TODO: Check for overflow? + info_json.AddMember("duration", (int)(file_info->duration_seconds.value() * 1000.0), request_data.GetAllocator()); + } + + if(thumbnail_info) { + rapidjson::Value thumbnail_info_json(rapidjson::kObjectType); + thumbnail_info_json.AddMember("size", thumbnail_info->file_size, request_data.GetAllocator()); + thumbnail_info_json.AddMember("mimetype", rapidjson::StringRef(content_type_to_string(thumbnail_info->content_type)), request_data.GetAllocator()); + if(thumbnail_info->dimensions) { + thumbnail_info_json.AddMember("w", thumbnail_info->dimensions->width, request_data.GetAllocator()); + thumbnail_info_json.AddMember("h", thumbnail_info->dimensions->height, request_data.GetAllocator()); + } + + info_json.AddMember("thumbnail_url", rapidjson::StringRef(thumbnail_info->content_uri.c_str()), request_data.GetAllocator()); + info_json.AddMember("thumbnail_info", std::move(thumbnail_info_json), request_data.GetAllocator()); + } + + request_data.AddMember("info", std::move(info_json), request_data.GetAllocator()); + request_data.AddMember("url", rapidjson::StringRef(file_info->content_uri.c_str()), request_data.GetAllocator()); + } + rapidjson::StringBuffer buffer; rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); request_data.Accept(writer); @@ -3714,7 +3746,10 @@ namespace QuickMedia { return filepath.c_str() + index + 1; } - PluginResult Matrix::post_file(RoomData *room, const std::string &filepath, std::string filename, std::string &event_id_response, std::string &err_msg) { + PluginResult Matrix::post_file(RoomData *room, const std::string &filepath, std::string filename, std::string &event_id_response, std::string &err_msg, void *relates_to) { + if(filename.empty()) + filename = file_get_filename(filepath); + UploadInfo file_info; UploadInfo thumbnail_info; PluginResult upload_file_result = upload_file(room, filepath, filename, file_info, thumbnail_info, err_msg); @@ -3726,10 +3761,10 @@ namespace QuickMedia { if(!thumbnail_info.content_uri.empty()) thumbnail_info_opt = std::move(thumbnail_info); - if(filename.empty()) - filename = file_get_filename(filepath); - - return post_message(room, filename, event_id_response, file_info_opt, thumbnail_info_opt); + if(relates_to) + return post_reply(room, filename, relates_to, event_id_response, "", file_info_opt, thumbnail_info_opt); + else + return post_message(room, filename, event_id_response, file_info_opt, thumbnail_info_opt); } PluginResult Matrix::upload_file(RoomData *room, const std::string &filepath, std::string filename, UploadInfo &file_info, UploadInfo &thumbnail_info, std::string &err_msg, bool upload_thumbnail) { @@ -3744,6 +3779,9 @@ namespace QuickMedia { file_info.dimensions = file_analyzer.get_dimensions(); file_info.duration_seconds = file_analyzer.get_duration_seconds(); + if(filename.empty()) + filename = file_get_filename(filepath); + int upload_limit; PluginResult config_result = get_config(&upload_limit); if(config_result != PluginResult::OK) { @@ -3766,9 +3804,12 @@ namespace QuickMedia { char tmp_filename[] = "/tmp/quickmedia_video_frame_XXXXXX"; int tmp_file = mkstemp(tmp_filename); if(tmp_file != -1) { - if(video_get_first_frame(file_analyzer, tmp_filename, thumbnail_max_size.x, thumbnail_max_size.y)) { + Path thumbnail_filename = filename; + thumbnail_filename = thumbnail_filename.filename_no_ext() + ".thumb.jpg"; // TODO: See video_get_middle_frame why this is jpg + + if(video_get_middle_frame(file_analyzer, tmp_filename, thumbnail_max_size.x, thumbnail_max_size.y)) { UploadInfo upload_info_ignored; // Ignore because it wont be set anyways. Thumbnails dont have thumbnails. - PluginResult upload_thumbnail_result = upload_file(room, tmp_filename, "", thumbnail_info, upload_info_ignored, err_msg, false); + PluginResult upload_thumbnail_result = upload_file(room, tmp_filename, thumbnail_filename.data, thumbnail_info, upload_info_ignored, err_msg, false); if(upload_thumbnail_result != PluginResult::OK) { close(tmp_file); remove(tmp_filename); @@ -3792,8 +3833,11 @@ namespace QuickMedia { else thumbnail_path = filepath; + Path thumbnail_filename = filename; + thumbnail_filename = thumbnail_filename.filename_no_ext() + ".thumb" + thumbnail_filename.ext(); + UploadInfo upload_info_ignored; // Ignore because it wont be set anyways. Thumbnails dont have thumbnails. - PluginResult upload_thumbnail_result = upload_file(room, thumbnail_path, "", thumbnail_info, upload_info_ignored, err_msg, false); + PluginResult upload_thumbnail_result = upload_file(room, thumbnail_path, thumbnail_filename.data, thumbnail_info, upload_info_ignored, err_msg, false); if(upload_thumbnail_result != PluginResult::OK) { close(tmp_file); remove(tmp_filename); @@ -3814,9 +3858,6 @@ namespace QuickMedia { { "--data-binary", "@" + filepath } }; - if(filename.empty()) - filename = file_get_filename(filepath); - std::string filename_escaped = url_param_encode(filename); char url[512]; |