aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Matrix.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-04-01 17:59:36 +0200
committerdec05eba <dec05eba@protonmail.com>2021-04-01 17:59:36 +0200
commit276395f468c8ee05951401a1d352e0dec3c3a3a8 (patch)
tree3eab155a956c61268a4077322de9de9d7ca17ce0 /src/plugins/Matrix.cpp
parent521589c6026246113117a0f8ad568e0061aba46a (diff)
Matrix: add room directory for joining rooms, resize video thumbnail
Diffstat (limited to 'src/plugins/Matrix.cpp')
-rw-r--r--src/plugins/Matrix.cpp184
1 files changed, 163 insertions, 21 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index 128e610..5d942ed 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -18,7 +18,7 @@
#include <unistd.h>
#include "../../include/QuickMedia.hpp"
-// TODO: Update avatar/display name when its changed in the room/globally.
+// TODO: Use string assign with string length instead of assigning to c string (which calls strlen)
// Show images/videos inline.
// TODO: Verify if buffer of size 512 is enough for endpoints
// Remove older messages (outside screen) to save memory. Reload them when the selected body item is the top/bottom one.
@@ -72,6 +72,8 @@ static std::string extract_first_line_elipses(const std::string &str, size_t max
}
namespace QuickMedia {
+ static const sf::Vector2i thumbnail_max_size(600, 337);
+
static void remove_body_item_by_url(BodyItems &body_items, const std::string &url) {
for(auto it = body_items.begin(); it != body_items.end();) {
if((*it)->url == url)
@@ -980,6 +982,46 @@ namespace QuickMedia {
rooms_page->update();
}
+ PluginResult MatrixRoomDirectoryPage::submit(const std::string &title, const std::string&, std::vector<Tab> &result_tabs) {
+ result_tabs.push_back(Tab{create_body(), std::make_unique<MatrixServerRoomListPage>(program, matrix, title), create_search_bar("Search...", 350)});
+ return PluginResult::OK;
+ }
+
+ PluginResult MatrixServerRoomListPage::lazy_fetch(BodyItems &result_items) {
+ return matrix->get_public_rooms(server_name, search_term, next_batch, result_items, next_batch);
+ }
+
+ PluginResult MatrixServerRoomListPage::get_page(const std::string&, int page, BodyItems &result_items) {
+ while(current_page < page && !next_batch.empty()) {
+ PluginResult plugin_result = lazy_fetch(result_items);
+ if(plugin_result != PluginResult::OK) return plugin_result;
+ ++current_page;
+ }
+ return PluginResult::OK;
+ }
+
+ SearchResult MatrixServerRoomListPage::search(const std::string &str, BodyItems &result_items) {
+ next_batch.clear();
+ current_page = 0;
+ search_term = str;
+ return plugin_result_to_search_result(lazy_fetch(result_items));
+ }
+
+ PluginResult MatrixServerRoomListPage::submit(const std::string &title, const std::string &url, std::vector<Tab>&) {
+ TaskResult task_result = program->run_task_with_loading_screen([this, url]() {
+ return matrix->join_room(url) == PluginResult::OK;
+ });
+
+ if(task_result == TaskResult::TRUE) {
+ show_notification("QuickMedia", "You joined " + title, Urgency::NORMAL);
+ program->set_go_to_previous_page();
+ } else if(task_result == TaskResult::FALSE) {
+ show_notification("QuickMedia", "Failed to join " + title, Urgency::CRITICAL);
+ }
+
+ return PluginResult::OK;
+ }
+
static std::array<const char*, 7> sync_fail_error_codes = {
"M_FORBIDDEN",
"M_UNKNOWN_TOKEN",
@@ -998,18 +1040,6 @@ namespace QuickMedia {
}
}
- static void remove_ephemeral_field_in_sync_rooms_response(rapidjson::Value &rooms_json) {
- auto join_it = rooms_json.FindMember("join");
- if(join_it == rooms_json.MemberEnd() || !join_it->value.IsObject())
- return;
-
- for(auto &it : join_it->value.GetObject()) {
- if(!it.value.IsObject())
- continue;
- it.value.RemoveMember("ephemeral");
- }
- }
-
static void remove_empty_fields_in_sync_account_data_response(rapidjson::Value &account_data_json) {
for(const char *member_name : {"events"}) {
auto join_it = account_data_json.FindMember(member_name);
@@ -2416,6 +2446,7 @@ namespace QuickMedia {
std::string room_id_str(room_id.GetString(), room_id.GetStringLength());
if(set_invite(room_id_str, invite))
delegate->add_invite(room_id_str, std::move(invite));
+
break;
}
}
@@ -3168,7 +3199,7 @@ 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(filepath.c_str(), tmp_filename)) {
+ if(video_get_first_frame(filepath.c_str(), 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);
if(upload_thumbnail_result != PluginResult::OK) {
@@ -3189,7 +3220,7 @@ namespace QuickMedia {
int tmp_file = mkstemp(tmp_filename);
if(tmp_file != -1) {
std::string thumbnail_path;
- if(create_thumbnail(filepath, tmp_filename, sf::Vector2i(600, 337)))
+ if(create_thumbnail(filepath, tmp_filename, thumbnail_max_size))
thumbnail_path = tmp_filename;
else
thumbnail_path = filepath;
@@ -3306,6 +3337,10 @@ namespace QuickMedia {
return PluginResult::ERR;
}
+ const rapidjson::Value &home_server_json = GetMember(json_root, "home_server");
+ if(home_server_json.IsString())
+ this->homeserver_domain = home_server_json.GetString();
+
// Use the user-provided homeserver instead of the one the server tells us about, otherwise this wont work with a proxy
// such as pantalaimon
json_root.AddMember("homeserver", rapidjson::StringRef(homeserver.c_str()), request_data.GetAllocator());
@@ -3348,6 +3383,7 @@ namespace QuickMedia {
my_user_id.clear();
access_token.clear();
homeserver.clear();
+ homeserver_domain.clear();
upload_limit.reset();
set_next_batch("");
invites.clear();
@@ -3440,13 +3476,13 @@ namespace QuickMedia {
return PluginResult::ERR;
}
- std::string user_id = user_id_json.GetString();
- std::string access_token = access_token_json.GetString();
- std::string homeserver = homeserver_json.GetString();
+ const rapidjson::Value &home_server_json = GetMember(json_root, "home_server");
+ if(home_server_json.IsString())
+ this->homeserver_domain = home_server_json.GetString();
- this->my_user_id = std::move(user_id);
- this->access_token = std::move(access_token);
- this->homeserver = std::move(homeserver);
+ this->my_user_id = user_id_json.GetString();
+ this->access_token = access_token_json.GetString();
+ this->homeserver = homeserver_json.GetString();
return PluginResult::OK;
}
@@ -3600,6 +3636,108 @@ namespace QuickMedia {
return download_result_to_plugin_result(download_result);
}
+ PluginResult Matrix::get_public_rooms(const std::string &server, const std::string &search_term, const std::string &since, BodyItems &rooms, std::string &next_batch) {
+ rapidjson::Document filter_data(rapidjson::kObjectType);
+ if(!search_term.empty())
+ filter_data.AddMember("generic_search_term", rapidjson::StringRef(search_term.c_str()), filter_data.GetAllocator());
+
+ rapidjson::Document request_data(rapidjson::kObjectType);
+ request_data.AddMember("limit", 20, request_data.GetAllocator());
+
+ if(!search_term.empty())
+ request_data.AddMember("filter", std::move(filter_data), request_data.GetAllocator());
+
+ if(!since.empty())
+ request_data.AddMember("since", rapidjson::StringRef(since.c_str()), request_data.GetAllocator());
+
+ rapidjson::StringBuffer buffer;
+ rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+ request_data.Accept(writer);
+
+ std::vector<CommandArg> additional_args = {
+ { "-X", "POST" },
+ { "-H", "content-type: application/json" },
+ { "-H", "Authorization: Bearer " + access_token },
+ { "--data-binary", buffer.GetString() }
+ };
+
+ std::string url = homeserver + "/_matrix/client/r0/publicRooms?server=";
+ url += url_param_encode(server);
+
+ rapidjson::Document json_root;
+ DownloadResult download_result = download_json(json_root, 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 &next_batch_json = GetMember(json_root, "next_batch");
+ if(next_batch_json.IsString())
+ next_batch = next_batch_json.GetString();
+ else
+ next_batch.clear();
+
+ const rapidjson::Value &chunk_json = GetMember(json_root, "chunk");
+ if(chunk_json.IsArray()) {
+ for(const rapidjson::Value &chunk_item_json : chunk_json.GetArray()) {
+ if(!chunk_item_json.IsObject())
+ continue;
+
+ const rapidjson::Value &room_id_json = GetMember(chunk_item_json, "room_id");
+ if(!room_id_json.IsString())
+ continue;
+
+ std::string room_name;
+ const rapidjson::Value &name_json = GetMember(chunk_item_json, "name");
+ if(name_json.IsString())
+ room_name = name_json.GetString();
+ else
+ room_name = room_id_json.GetString();
+
+ auto room_body_item = BodyItem::create(std::move(room_name));
+ room_body_item->url = room_id_json.GetString();
+ std::string description;
+
+ const rapidjson::Value &topic_json = GetMember(chunk_item_json, "topic");
+ if(topic_json.IsString())
+ description = strip(topic_json.GetString());
+
+ const rapidjson::Value &canonical_alias_json = GetMember(chunk_item_json, "canonical_alias");
+ if(canonical_alias_json.IsString()) {
+ if(!description.empty())
+ description += '\n';
+ description += canonical_alias_json.GetString();
+ }
+
+ const rapidjson::Value &num_joined_members_json = GetMember(chunk_item_json, "num_joined_members");
+ if(num_joined_members_json.IsInt()) {
+ if(!description.empty())
+ description += '\n';
+ description += "👤" + std::to_string(num_joined_members_json.GetInt());
+ }
+
+ room_body_item->set_description(std::move(description));
+
+ const rapidjson::Value &avatar_url_json = GetMember(chunk_item_json, "avatar_url");
+ if(avatar_url_json.IsString()) {
+ std::string avatar_url = thumbnail_url_extract_media_id(avatar_url_json.GetString());
+ if(!avatar_url.empty())
+ avatar_url = get_thumbnail_url(homeserver, avatar_url);
+
+ if(!avatar_url.empty()) {
+ room_body_item->thumbnail_url = std::move(avatar_url);
+ room_body_item->thumbnail_size = sf::Vector2i(32 * get_ui_scale(), 32 * get_ui_scale());
+ room_body_item->thumbnail_mask_type = ThumbnailMaskType::CIRCLE;
+ }
+ }
+
+ rooms.push_back(std::move(room_body_item));
+ }
+ }
+
+ return PluginResult::OK;
+ }
+
bool Matrix::was_message_posted_by_me(void *message) {
Message *message_typed = (Message*)message;
return my_user_id == message_typed->user->user_id;
@@ -3645,6 +3783,10 @@ namespace QuickMedia {
return get_user_by_id(room, my_user_id);
}
+ const std::string& Matrix::get_homeserver_domain() const {
+ return homeserver_domain;
+ }
+
RoomData* Matrix::get_room_by_id(const std::string &id) {
std::lock_guard<std::recursive_mutex> lock(room_data_mutex);
auto room_it = room_data_by_id.find(id);