diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-09-22 01:42:51 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-09-22 04:12:03 +0200 |
commit | a29f310b8ad0b088860fe05a5499bccef963a503 (patch) | |
tree | 7df3abb8e3b3652bf6f7d13ac336e498c60509b9 /src/plugins | |
parent | 7bf0f7107b72af01fea83a7343ce1c32e54865e9 (diff) |
Add ctrl+i to view attached images/videos to matrix posts
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Matrix.cpp | 73 |
1 files changed, 59 insertions, 14 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index ad0dfea..8c6d07d 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -9,8 +9,12 @@ // Send read receipt to server and receive notifications in /sync and show the notifications. // Delete messages. // Edit messages. -// Show embedded images/videos. +// Show images/videos inline. // TODO: Verify if buffer of size 512 is enough for endpoints +// TODO: POST /_matrix/client/r0/rooms/{roomId}/read_markers after 5 seconds of receiving a message when the client is focused +// to mark messages as read +// When reaching top/bottom message, show older/newer messages. +// Remove older messages (outside screen) to save memory. Reload them when the selected body item is the top/bottom one. namespace QuickMedia { Matrix::Matrix() : Plugin("matrix") { @@ -179,15 +183,17 @@ namespace QuickMedia { size_t prev_user_id = -1; for(auto it = room_it->second->messages.begin() + start_index, end = room_it->second->messages.end(); it != end; ++it) { const UserInfo &user_info = room_it->second->user_info[it->user_id]; - if(it->user_id == prev_user_id) { + if(it->user_id == prev_user_id && it->url.empty()) { assert(!result_items.empty()); result_items.back()->append_description("\n"); - result_items.back()->append_description(it->msg); + result_items.back()->append_description(it->body); } else { auto body_item = std::make_unique<BodyItem>(""); body_item->author = user_info.display_name; - body_item->set_description(it->msg); + body_item->set_description(it->body); body_item->thumbnail_url = user_info.avatar_url; + // TODO: Show image thumbnail inline instead of url to image + body_item->url = it->url; result_items.push_back(std::move(body_item)); prev_user_id = it->user_id; } @@ -261,6 +267,7 @@ namespace QuickMedia { room_it->second->prev_batch = prev_batch_json.asString(); const Json::Value &events_json = timeline_json["events"]; + events_add_user_info(events_json, room_it->second.get()); events_add_messages(events_json, room_it->second.get(), MessageDirection::AFTER); events_set_room_name(events_json, room_it->second.get()); } @@ -307,7 +314,7 @@ namespace QuickMedia { UserInfo user_info; user_info.avatar_url = avatar_url_json.asString(); - if(user_info.avatar_url.size() >= 6) + if(strncmp(user_info.avatar_url.c_str(), "mxc://", 6) == 0) user_info.avatar_url.erase(user_info.avatar_url.begin(), user_info.avatar_url.begin() + 6); // TODO: What if the user hasn't selected an avatar? user_info.avatar_url = homeserver + "/_matrix/media/r0/thumbnail/" + user_info.avatar_url + "?width=32&height=32&method=crop"; @@ -342,11 +349,7 @@ namespace QuickMedia { continue; const Json::Value &content_type = content_json["msgtype"]; - if(!content_type.isString() || strcmp(content_type.asCString(), "m.text") != 0) - continue; - - const Json::Value &body_json = content_json["body"]; - if(!body_json.isString()) + if(!content_type.isString()) continue; auto user_it = room_data->user_info_by_user_id.find(sender_json_str); @@ -355,10 +358,52 @@ namespace QuickMedia { continue; } - Message message; - message.user_id = user_it->second; - message.msg = body_json.asString(); - new_messages.push_back(std::move(message)); + const Json::Value &body_json = content_json["body"]; + if(!body_json.isString()) + continue; + + if(strcmp(content_type.asCString(), "m.text") == 0) { + Message message; + message.user_id = user_it->second; + message.body = body_json.asString(); + message.type = MessageType::TEXT; + new_messages.push_back(std::move(message)); + } else if(strcmp(content_type.asCString(), "m.image") == 0) { + const Json::Value &url_json = content_json["url"]; + if(!url_json.isString() || strncmp(url_json.asCString(), "mxc://", 6) != 0) + continue; + + Message message; + message.user_id = user_it->second; + message.body = body_json.asString(); + message.url = homeserver + "/_matrix/media/r0/download/" + url_json.asString().substr(6); + message.type = MessageType::IMAGE; + new_messages.push_back(std::move(message)); + } else if(strcmp(content_type.asCString(), "m.video") == 0) { + const Json::Value &url_json = content_json["url"]; + if(!url_json.isString() || strncmp(url_json.asCString(), "mxc://", 6) != 0) + continue; + + Message message; + + const Json::Value &info_json = content_json["info"]; + if(info_json.isString()) { + const Json::Value &thumbnail_url_json = info_json["thumbnail_url"]; + if(thumbnail_url_json.isString()) { + std::string thumbnail_str = thumbnail_url_json.asString(); + if(strncmp(thumbnail_str.c_str(), "mxc://", 6) == 0) { + thumbnail_str.erase(thumbnail_str.begin(), thumbnail_str.begin() + 6); + message.thumbnail_url = homeserver + "/_matrix/media/r0/download/" + std::move(thumbnail_str); + } + } + } + + message.user_id = user_it->second; + message.body = body_json.asString(); + message.url = homeserver + "/_matrix/media/r0/download/" + url_json.asString().substr(6); + message.type = MessageType::VIDEO; + new_messages.push_back(std::move(message)); + } } if(message_dir == MessageDirection::BEFORE) { |