aboutsummaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-10-22 21:56:33 +0200
committerdec05eba <dec05eba@protonmail.com>2020-10-22 21:56:33 +0200
commit97564d40636aafb251644f61a0b990e392afd7a4 (patch)
treed6cb19849445d4d2683024072f1966c7bb17c619 /src/plugins
parent42ef59ef17cb0a56c6ac1d8f220db7ca461c5411 (diff)
Matrix: add pinned messages tab
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/Matrix.cpp66
1 files changed, 57 insertions, 9 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index b6f6f76..9a2f70b 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -75,6 +75,11 @@ namespace QuickMedia {
}
}
+ void RoomData::append_pinned_events(std::vector<std::string> new_pinned_events) {
+ std::lock_guard<std::mutex> lock(room_mutex);
+ pinned_events.insert(pinned_events.end(), new_pinned_events.begin(), new_pinned_events.end());
+ }
+
std::shared_ptr<Message> RoomData::get_message_by_id(const std::string &id) {
std::lock_guard<std::mutex> lock(room_mutex);
auto message_it = message_by_event_id.find(id);
@@ -106,7 +111,11 @@ namespace QuickMedia {
return messages;
}
- PluginResult Matrix::sync(RoomSyncMessages &room_messages) {
+ const std::vector<std::string>& RoomData::get_pinned_events_unsafe() const {
+ return pinned_events;
+ }
+
+ PluginResult Matrix::sync(RoomSyncData &room_sync_data) {
std::vector<CommandArg> additional_args = {
{ "-H", "Authorization: Bearer " + access_token },
{ "-m", "35" }
@@ -122,7 +131,7 @@ namespace QuickMedia {
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);
- PluginResult result = sync_response_to_body_items(json_root, room_messages);
+ PluginResult result = sync_response_to_body_items(json_root, room_sync_data);
if(result != PluginResult::OK)
return result;
@@ -148,11 +157,16 @@ namespace QuickMedia {
room_list_read_index += num_new_rooms;
}
- PluginResult Matrix::get_all_synced_room_messages(RoomData *room, Messages &messages) {
+ void Matrix::get_all_synced_room_messages(RoomData *room, Messages &messages) {
room->acquire_room_lock();
messages = room->get_messages_thread_unsafe();
room->release_room_lock();
- return PluginResult::OK;
+ }
+
+ void Matrix::get_all_pinned_events(RoomData *room, std::vector<std::string> &events) {
+ room->acquire_room_lock();
+ events = room->get_pinned_events_unsafe();
+ room->release_room_lock();
}
PluginResult Matrix::get_previous_room_messages(RoomData *room, Messages &messages) {
@@ -171,7 +185,7 @@ namespace QuickMedia {
return PluginResult::OK;
}
- PluginResult Matrix::sync_response_to_body_items(const rapidjson::Document &root, RoomSyncMessages &room_messages) {
+ PluginResult Matrix::sync_response_to_body_items(const rapidjson::Document &root, RoomSyncData &room_sync_data) {
if(!root.IsObject())
return PluginResult::ERR;
@@ -206,6 +220,7 @@ namespace QuickMedia {
const rapidjson::Value &events_json = GetMember(state_json, "events");
events_add_user_info(events_json, room);
events_set_room_name(events_json, room);
+ events_add_pinned_events(events_json, room, room_sync_data);
}
const rapidjson::Value &ephemeral_json = GetMember(it.value, "ephemeral");
@@ -236,7 +251,7 @@ namespace QuickMedia {
const rapidjson::Value &events_json = GetMember(ephemeral_json, "events");
events_add_user_read_markers(events_json, room);
}
- events_add_messages(events_json, room, MessageDirection::AFTER, &room_messages, has_unread_notifications);
+ events_add_messages(events_json, room, MessageDirection::AFTER, &room_sync_data, has_unread_notifications);
} else {
if(ephemeral_json.IsObject()) {
const rapidjson::Value &events_json = GetMember(ephemeral_json, "events");
@@ -454,7 +469,7 @@ namespace QuickMedia {
return false;
}
- void Matrix::events_add_messages(const rapidjson::Value &events_json, RoomData *room_data, MessageDirection message_dir, RoomSyncMessages *room_messages, bool has_unread_notifications) {
+ void Matrix::events_add_messages(const rapidjson::Value &events_json, RoomData *room_data, MessageDirection message_dir, RoomSyncData *room_sync_data, bool has_unread_notifications) {
if(!events_json.IsArray())
return;
@@ -471,8 +486,8 @@ namespace QuickMedia {
return;
// TODO: Add directly to this instead when set? otherwise add to new_messages
- if(room_messages)
- (*room_messages)[room_data] = new_messages;
+ if(room_sync_data)
+ (*room_sync_data)[room_data].messages = new_messages;
// TODO: Loop and std::move instead? doesn't insert create copies?
if(message_dir == MessageDirection::BEFORE) {
@@ -753,6 +768,39 @@ namespace QuickMedia {
}
}
+ void Matrix::events_add_pinned_events(const rapidjson::Value &events_json, RoomData *room_data, RoomSyncData &room_sync_data) {
+ if(!events_json.IsArray())
+ return;
+
+ std::vector<std::string> pinned_events;
+ 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.pinned_events") != 0)
+ continue;
+
+ const rapidjson::Value &content_json = GetMember(event_item_json, "content");
+ if(!content_json.IsObject())
+ continue;
+
+ const rapidjson::Value &pinned_json = GetMember(content_json, "pinned");
+ if(!pinned_json.IsArray())
+ continue;
+
+ for(const rapidjson::Value &pinned_item_json : pinned_json.GetArray()) {
+ if(!pinned_item_json.IsString())
+ continue;
+
+ pinned_events.push_back(std::string(pinned_item_json.GetString(), pinned_item_json.GetStringLength()));
+ }
+ }
+
+ room_sync_data[room_data].pinned_events = pinned_events;
+ room_data->append_pinned_events(std::move(pinned_events));
+ }
+
PluginResult Matrix::get_previous_room_messages(RoomData *room_data) {
std::string from = room_data->prev_batch;
if(from.empty()) {