aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-11-23 18:48:43 +0100
committerdec05eba <dec05eba@protonmail.com>2020-11-23 19:18:58 +0100
commitfd699d287de23771c17e395d79aa38287453a850 (patch)
tree36c1d2c47a2cb6426882aeeda65ad1d4851523e2 /src
parent5c0dd4a6885da32c52d4d03235507a5492919dd3 (diff)
Matrix: readd additional messages sync, remove reply/edit formatting from room description
Diffstat (limited to 'src')
-rw-r--r--src/QuickMedia.cpp78
-rw-r--r--src/plugins/Matrix.cpp33
2 files changed, 49 insertions, 62 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index 0e1c295..bbc3971 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -3037,25 +3037,6 @@ namespace QuickMedia {
sf::Text text;
};
- static std::string remove_reply_formatting(const std::string &str) {
- if(strncmp(str.c_str(), "> <@", 4) == 0) {
- size_t index = str.find("> ", 4);
- if(index != std::string::npos) {
- size_t msg_begin = str.find("\n\n", index + 2);
- if(msg_begin != std::string::npos)
- return str.substr(msg_begin + 2);
- }
- }
- return str;
- }
-
- static std::string message_get_body_remove_formatting(Message *message) {
- if(message->related_event_type == RelatedEventType::REPLY || message->related_event_type == RelatedEventType::EDIT)
- return remove_reply_formatting(message->body);
- else
- return message->body;
- }
-
static const sf::Vector2i CHAT_MESSAGE_THUMBNAIL_MAX_SIZE(600, 337);
static std::shared_ptr<BodyItem> message_to_body_item(RoomData *room, Message *message, const std::string &my_display_name, const std::string &my_user_id) {
@@ -3774,15 +3755,13 @@ namespace QuickMedia {
//tabs[MESSAGES_TAB_INDEX].body->set_page_scroll(window_size.y);
bool fetched_enough_messages = false;
- bool initial_prev_messages_fetch = true;
- auto fetch_more_previous_messages_if_needed = [this, &tabs, &current_room, &fetched_enough_messages, &previous_messages_future, &initial_prev_messages_fetch]() {
+ auto fetch_more_previous_messages_if_needed = [this, &tabs, &current_room, &fetched_enough_messages, &previous_messages_future]() {
if(!fetched_enough_messages && !previous_messages_future.valid()) {
- bool fetch_latest_messages = !matrix->is_initial_sync_finished() && initial_prev_messages_fetch;
if(!tabs[MESSAGES_TAB_INDEX].body->is_body_full_with_items()) {
- previous_messages_future = [this, &current_room, fetch_latest_messages]() {
+ previous_messages_future = [this, &current_room]() {
Messages messages;
- if(matrix->get_previous_room_messages(current_room, messages, fetch_latest_messages) != PluginResult::OK)
+ if(matrix->get_previous_room_messages(current_room, messages, true) != PluginResult::OK)
fprintf(stderr, "Failed to get previous matrix messages in room: %s\n", current_room->id.c_str());
return messages;
};
@@ -3873,6 +3852,12 @@ namespace QuickMedia {
if(tabs[MESSAGES_TAB_INDEX].body->is_selected_item_last_visible_item() && selected_tab == MESSAGES_TAB_INDEX && chat_state != ChatState::TYPING_MESSAGE)
scroll_to_end = true;
+ if(current_room->initial_prev_messages_fetch) {
+ current_room->initial_prev_messages_fetch = false;
+ if(selected_tab == MESSAGES_TAB_INDEX)
+ scroll_to_end = true;
+ }
+
BodyItem *selected_item = tabs[MESSAGES_TAB_INDEX].body->get_selected();
tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(messages_to_body_items(current_room, messages, current_room->get_user_display_name(me), me->user_id));
if(selected_item && !scroll_to_end) {
@@ -4474,9 +4459,6 @@ namespace QuickMedia {
filter_sent_messages(new_messages);
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)
- move_to_bottom = tabs[MESSAGES_TAB_INDEX].body->no_items_visible();
size_t num_new_messages = new_messages.size();
if(num_new_messages > 0) {
BodyItem *selected_item = tabs[MESSAGES_TAB_INDEX].body->get_selected();
@@ -4492,13 +4474,10 @@ namespace QuickMedia {
// TODO: Do not loop all items, only loop the new items
resolve_unreferenced_events_with_body_items(tabs[MESSAGES_TAB_INDEX].body->items.data(), tabs[MESSAGES_TAB_INDEX].body->items.size());
}
- if(initial_prev_messages_fetch) {
- initial_prev_messages_fetch = false;
- // XXX: Hack to scroll up while keeping the selected item (usually the last one) visible
- if(move_to_bottom) {
+ if(num_new_messages > 0 && current_room->initial_prev_messages_fetch) {
+ current_room->initial_prev_messages_fetch = false;
+ if(selected_tab == MESSAGES_TAB_INDEX)
tabs[MESSAGES_TAB_INDEX].body->select_last_item();
- //tabs[MESSAGES_TAB_INDEX].body->set_page_scroll(window_size.y);
- }
}
}
@@ -4567,18 +4546,6 @@ namespace QuickMedia {
window.draw(room_name_text);
}
- gradient_points[0].position.x = 0.0f;
- gradient_points[0].position.y = tab_shade_height;
-
- gradient_points[1].position.x = window_size.x;
- gradient_points[1].position.y = tab_shade_height;
-
- gradient_points[2].position.x = window_size.x;
- gradient_points[2].position.y = tab_shade_height + gradient_height;
-
- gradient_points[3].position.x = 0.0f;
- gradient_points[3].position.y = tab_shade_height + gradient_height;
-
int i = 0;
for(ChatTab &tab : tabs) {
if(i == selected_tab) {
@@ -4597,6 +4564,18 @@ namespace QuickMedia {
gradient_inc += (frame_time_ms * 0.5);
sf::Color top_color = interpolate_colors(back_color, sf::Color(175, 180, 188), progress);
+ gradient_points[0].position.x = 0.0f;
+ gradient_points[0].position.y = tab_shade_height;
+
+ gradient_points[1].position.x = window_size.x;
+ gradient_points[1].position.y = tab_shade_height;
+
+ gradient_points[2].position.x = window_size.x;
+ gradient_points[2].position.y = tab_shade_height + gradient_height;
+
+ gradient_points[3].position.x = 0.0f;
+ gradient_points[3].position.y = tab_shade_height + gradient_height;
+
gradient_points[0].color = top_color;
gradient_points[1].color = top_color;
gradient_points[2].color = back_color;
@@ -4709,7 +4688,9 @@ namespace QuickMedia {
}
}
- current_room = matrix->get_room_by_id(current_room->id);
+
+ RoomData *new_room = matrix->get_room_by_id(current_room->id);
+ assert(new_room == current_room);
if(current_room) {
//all_messages.clear();
tabs[MESSAGES_TAB_INDEX].body->clear_items();
@@ -4726,6 +4707,11 @@ namespace QuickMedia {
modify_related_messages_in_current_room(all_messages_new);
unresolved_reactions.clear();
process_reactions(all_messages_new);
+ if(current_room->initial_prev_messages_fetch) {
+ current_room->initial_prev_messages_fetch = false;
+ if(selected_tab == MESSAGES_TAB_INDEX)
+ tabs[MESSAGES_TAB_INDEX].body->select_last_item();
+ }
std::vector<std::string> pinned_events;
matrix->get_all_pinned_events(current_room, pinned_events);
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index 278ffa3..95ce1dc 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -230,7 +230,9 @@ namespace QuickMedia {
void RoomData::set_prev_batch(const std::string &new_prev_batch) {
std::lock_guard<std::recursive_mutex> lock(room_mutex);
- prev_batch = new_prev_batch;
+ // TODO: Check if this always works and if it also works for other homeservers than synapse
+ if(prev_batch.empty() || new_prev_batch < prev_batch)
+ prev_batch = new_prev_batch;
}
std::string RoomData::get_prev_batch() {
@@ -455,7 +457,7 @@ namespace QuickMedia {
if(message->type == MessageType::REACTION)
return "Reacted with: " + extract_first_line_elipses(message->body, 150);
else
- return extract_first_line_elipses(message->body, 150);
+ return extract_first_line_elipses(message_get_body_remove_formatting(message), 150);
}
void MatrixQuickMedia::update_room_description(RoomData *room, Messages &new_messages, bool is_initial_sync, bool sync_is_cache) {
@@ -1017,7 +1019,7 @@ namespace QuickMedia {
{ "-H", "Authorization: Bearer " + access_token },
{ "-m", "35" }
};
-#if 0
+
sync_additional_messages_thread = std::thread([this]() {
std::vector<CommandArg> additional_args = {
{ "-H", "Authorization: Bearer " + access_token },
@@ -1043,7 +1045,7 @@ namespace QuickMedia {
additional_messages_queue.pop_wait();
parse_sync_response(json_root, true);
});
-#endif
+
const rapidjson::Value *next_batch_json;
PluginResult result;
bool initial_sync = true;
@@ -1388,7 +1390,7 @@ namespace QuickMedia {
const rapidjson::Value &timeline_json = GetMember(it.value, "timeline");
if(timeline_json.IsObject()) {
- if(!sync_is_cache && !room->has_prev_batch()) {
+ if(is_additional_messages_sync) {
// This may be non-existent if this is the first event in the room
const rapidjson::Value &prev_batch_json = GetMember(timeline_json, "prev_batch");
if(prev_batch_json.IsString())
@@ -2338,16 +2340,8 @@ namespace QuickMedia {
PluginResult Matrix::get_previous_room_messages(RoomData *room_data, bool latest_messages, size_t &num_new_messages) {
num_new_messages = 0;
std::string from = room_data->get_prev_batch();
- 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();
- if(from.empty()) {
- fprintf(stderr, "Warning: missing next batch, using END\n");
- from = "END";
- //return PluginResult::OK;
- }
- }
+ if(from.empty())
+ from = "END";
rapidjson::Document request_data(rapidjson::kObjectType);
request_data.AddMember("lazy_load_members", true, request_data.GetAllocator());
@@ -2535,7 +2529,7 @@ namespace QuickMedia {
return PluginResult::OK;
}
- static std::string remove_reply_formatting(const std::string &str) {
+ std::string remove_reply_formatting(const std::string &str) {
if(strncmp(str.c_str(), "> <@", 4) == 0) {
size_t index = str.find("> ", 4);
if(index != std::string::npos) {
@@ -2547,6 +2541,13 @@ namespace QuickMedia {
return str;
}
+ std::string message_get_body_remove_formatting(Message *message) {
+ if(message->related_event_type == RelatedEventType::REPLY || message->related_event_type == RelatedEventType::EDIT)
+ return remove_reply_formatting(message->body);
+ else
+ return message->body;
+ }
+
static std::string block_quote(const std::string &str) {
std::string result;
for(char c : str) {