aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-11-04 15:34:03 +0100
committerdec05eba <dec05eba@protonmail.com>2020-11-04 15:34:03 +0100
commite9d9e355f23f306198b0c49d9d24892514a1b2a5 (patch)
tree1bebb1db68ba10121ed7c5a614491c2cf6ec1c36
parent903d996e10401ec93a03b813cbc6ac4ace6a5dfa (diff)
Matrix: hack: fetch latest messages even if sync is not finished when going to room
-rw-r--r--plugins/Matrix.hpp4
-rw-r--r--src/QuickMedia.cpp31
-rw-r--r--src/plugins/Matrix.cpp12
3 files changed, 29 insertions, 18 deletions
diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp
index f4a3200..b7163d7 100644
--- a/plugins/Matrix.hpp
+++ b/plugins/Matrix.hpp
@@ -402,7 +402,7 @@ namespace QuickMedia {
void get_all_synced_room_messages(RoomData *room, Messages &messages);
void get_all_pinned_events(RoomData *room, std::vector<std::string> &events);
- PluginResult get_previous_room_messages(RoomData *room, Messages &messages);
+ PluginResult get_previous_room_messages(RoomData *room, Messages &messages, bool latest_messages = false);
// |url| should only be set when uploading media.
// TODO: Make api better.
@@ -450,7 +450,7 @@ namespace QuickMedia {
PluginResult parse_notifications(const rapidjson::Value &notifications_json);
PluginResult parse_sync_account_data(const rapidjson::Value &account_data_json, std::optional<std::set<std::string>> &dm_rooms);
PluginResult parse_sync_room_data(const rapidjson::Value &rooms_json);
- PluginResult get_previous_room_messages(RoomData *room_data);
+ PluginResult get_previous_room_messages(RoomData *room_data, bool latest_messages);
void events_add_user_info(const rapidjson::Value &events_json, RoomData *room_data);
std::shared_ptr<UserInfo> parse_user_info(const rapidjson::Value &json, const std::string &user_id, RoomData *room_data);
void events_add_user_read_markers(const rapidjson::Value &events_json, RoomData *room_data);
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index 1caf831..373970a 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -1365,9 +1365,6 @@ namespace QuickMedia {
}
if(matrix && !matrix->is_initial_sync_finished()) {
- load_sprite.setPosition(body_pos.x + body_size.x * 0.5f, body_pos.y + body_size.y * 0.5f);
- load_sprite.setRotation(load_sprite_timer.getElapsedTime().asSeconds() * 400.0);
- window.draw(load_sprite);
std::string err_msg;
if(matrix->did_initial_sync_fail(err_msg)) {
show_notification("QuickMedia", "Initial matrix sync failed, error: " + err_msg, Urgency::CRITICAL);
@@ -3455,12 +3452,24 @@ namespace QuickMedia {
tabs[MESSAGES_TAB_INDEX].body->set_page_scroll(window_size.y);
+ std::unordered_set<Message*> fetched_messages_set;
+ auto filter_existing_messages = [&fetched_messages_set](Messages &messages) {
+ for(auto it = messages.begin(); it != messages.end();) {
+ auto res = fetched_messages_set.insert(it->get());
+ if(!res.second)
+ it = messages.erase(it);
+ else
+ ++it;
+ }
+ };
+
+ bool fetch_latest_messages = !matrix->is_initial_sync_finished();
bool initial_prev_messages_fetch = false;
- if(num_messages_in_room < 10 && matrix->is_initial_sync_finished()) {
+ if(num_messages_in_room < 10) {
initial_prev_messages_fetch = true;
- previous_messages_future = [this, &current_room]() {
+ previous_messages_future = [this, &current_room, fetch_latest_messages]() {
Messages messages;
- if(matrix->get_previous_room_messages(current_room, messages) != PluginResult::OK)
+ if(matrix->get_previous_room_messages(current_room, messages, fetch_latest_messages) != PluginResult::OK)
fprintf(stderr, "Failed to get previous matrix messages in room: %s\n", current_room->id.c_str());
return messages;
};
@@ -3619,8 +3628,10 @@ namespace QuickMedia {
unreferenced_event_by_room.clear();
- for(auto &body_item : tabs[PINNED_TAB_INDEX].body->items) {
- delete (PinnedEventData*)body_item->userdata;
+ if(!tabs.empty()) {
+ for(auto &body_item : tabs[PINNED_TAB_INDEX].body->items) {
+ delete (PinnedEventData*)body_item->userdata;
+ }
}
tabs.clear();
@@ -4026,6 +4037,7 @@ namespace QuickMedia {
if(previous_messages_future.ready()) {
Messages new_messages = previous_messages_future.get();
+ filter_existing_messages(new_messages);
fprintf(stderr, "Finished fetching older messages, num new messages: %zu\n", new_messages.size());
bool move_to_bottom = false;
if(initial_prev_messages_fetch)
@@ -4223,9 +4235,6 @@ namespace QuickMedia {
}
if(matrix && !matrix->is_initial_sync_finished()) {
- load_sprite.setPosition(body_pos.x + body_size.x * 0.5f, body_pos.y + body_size.y * 0.5f);
- load_sprite.setRotation(load_sprite_timer.getElapsedTime().asSeconds() * 400.0);
- window.draw(load_sprite);
std::string err_msg;
if(matrix->did_initial_sync_fail(err_msg)) {
show_notification("QuickMedia", "Initial matrix sync failed, error: " + err_msg, Urgency::CRITICAL);
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index 2736421..6d0e095 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -20,7 +20,7 @@
static const char* SERVICE_NAME = "matrix";
static const char* OTHERS_ROOM_TAG = "tld.name.others";
-static const char* FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"limit\":3,\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"limit\":1,\"types\":[\"m.fully_read\",\"m.tag\"],\"lazy_load_members\":true}}}";
+static const char* FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"limit\":1,\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"limit\":1,\"types\":[\"m.fully_read\",\"m.tag\"],\"lazy_load_members\":true}}}";
static rapidjson::Value nullValue(rapidjson::kNullType);
static const rapidjson::Value& GetMember(const rapidjson::Value &obj, const char *key) {
@@ -1021,11 +1021,11 @@ namespace QuickMedia {
room->release_room_lock();
}
- PluginResult Matrix::get_previous_room_messages(RoomData *room, Messages &messages) {
+ PluginResult Matrix::get_previous_room_messages(RoomData *room, Messages &messages, bool latest_messages) {
room->acquire_room_lock();
size_t num_messages_before = room->get_messages_thread_unsafe().size();
room->release_room_lock();
- PluginResult result = get_previous_room_messages(room);
+ PluginResult result = get_previous_room_messages(room, latest_messages);
if(result != PluginResult::OK)
return result;
@@ -2150,9 +2150,11 @@ namespace QuickMedia {
}
}
- PluginResult Matrix::get_previous_room_messages(RoomData *room_data) {
+ PluginResult Matrix::get_previous_room_messages(RoomData *room_data, bool latest_messages) {
std::string from = room_data->get_prev_batch();
- if(from.empty()) {
+ if(latest_messages)
+ from = "END";
+ else if(from.empty()) {
fprintf(stderr, "Info: missing previous batch for room: %s, using /sync next batch\n", room_data->id.c_str());
// TODO: When caching /sync, remember to add lock around getting next_batch!
from = get_next_batch();