aboutsummaryrefslogtreecommitdiff
path: root/src/QuickMedia.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/QuickMedia.cpp')
-rw-r--r--src/QuickMedia.cpp289
1 files changed, 170 insertions, 119 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index cec81c6..249f38f 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -2807,8 +2807,13 @@ namespace QuickMedia {
while (current_page == PageType::CHAT_LOGIN) {
while (window.pollEvent(event)) {
- base_event_handler(event, PageType::EXIT, body.get(), nullptr, false, false);
- if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus) {
+ if(event.type == sf::Event::Resized) {
+ window_size.x = event.size.width;
+ window_size.y = event.size.height;
+ sf::FloatRect visible_area(0, 0, window_size.x, window_size.y);
+ window.setView(sf::View(visible_area));
+ redraw = true;
+ } else if(event.type == sf::Event::GainedFocus) {
redraw = true;
} else if(event.type == sf::Event::TextEntered) {
inputs[focused_input]->onTextEntered(event.text.unicode);
@@ -2867,6 +2872,29 @@ namespace QuickMedia {
}
}
+ static BodyItems messages_to_body_items(const Messages &messages) {
+ BodyItems result_items(messages.size());
+ for(size_t i = 0; i < messages.size(); ++i) {
+ auto &message = messages[i];
+ auto body_item = BodyItem::create("");
+ body_item->set_author(message->user->display_name);
+ body_item->set_description(message->body);
+ body_item->set_timestamp(message->timestamp);
+ if(!message->thumbnail_url.empty())
+ body_item->thumbnail_url = message->thumbnail_url;
+ else if(!message->url.empty() && message->type == MessageType::IMAGE)
+ body_item->thumbnail_url = message->url;
+ else
+ body_item->thumbnail_url = message->user->avatar_url;
+ // TODO: Show image thumbnail inline instead of url to image and showing it as the thumbnail of the body item
+ body_item->url = message->url;
+ body_item->author_color = message->user->display_name_color;
+ body_item->userdata = (void*)message.get(); // Note: message has to be valid as long as body_item is used!
+ result_items[i] = std::move(body_item);
+ }
+ return result_items;
+ }
+
void Program::chat_page() {
assert(strcmp(plugin_name, "matrix") == 0);
@@ -2910,13 +2938,13 @@ namespace QuickMedia {
time_t last_read_message_timestamp;
};
- std::unordered_map<std::string, RoomBodyData> body_items_by_room_id;
- std::string current_room_id;
+ std::unordered_map<std::shared_ptr<RoomData>, RoomBodyData> body_items_by_room;
+ std::shared_ptr<RoomData> current_room;
RoomBodyData *current_room_body_data = nullptr;
bool is_window_focused = window.hasFocus();
- auto process_new_room_messages = [this, &body_items_by_room_id, &current_room_id, &is_window_focused](RoomSyncMessages &room_sync_messages, bool only_show_mentions) mutable {
+ auto process_new_room_messages = [this, &body_items_by_room, &current_room, &is_window_focused](RoomSyncMessages &room_sync_messages, bool only_show_mentions) mutable {
for(auto &[room, messages] : room_sync_messages) {
bool was_mentioned = false;
for(auto &message : messages) {
@@ -2924,20 +2952,20 @@ namespace QuickMedia {
was_mentioned = true;
message->mentions_me = false;
// TODO: What if the message or username begins with "-"? also make the notification image be the avatar of the user
- if(!is_window_focused || room->id != current_room_id)
+ if(!is_window_focused || room != current_room)
show_notification("QuickMedia matrix - " + matrix->message_get_author_displayname(message.get()) + " (" + room->name + ")", message->body);
}
}
- auto room_body_item_it = body_items_by_room_id.find(room->id);
- if(room_body_item_it == body_items_by_room_id.end())
+ auto room_body_item_it = body_items_by_room.find(room);
+ if(room_body_item_it == body_items_by_room.end())
continue;
- // TODO: this wont always because we dont display all types of messages from server, such as "joined", "left", "kicked", "banned", "changed avatar", "changed display name", etc.
+ // TODO: this wont always work because we dont display all types of messages from server, such as "joined", "left", "kicked", "banned", "changed avatar", "changed display name", etc.
// TODO: Update local marker when another client with our user sets read marker, in that case our read marker (room->get_user_read_marker) will be updated.
bool unread_messages_previous_session = false;
if(!messages.empty()) {
- std::shared_ptr<UserInfo> me = matrix->get_me(room->id);
+ std::shared_ptr<UserInfo> me = matrix->get_me(room);
if(me && room->get_user_read_marker(me) != messages.back()->event_id)
unread_messages_previous_session = true;
}
@@ -2983,7 +3011,10 @@ namespace QuickMedia {
chat_input.draw_background = false;
chat_input.set_editable(false);
- chat_input.on_submit_callback = [this, &chat_input, &tabs, &selected_tab, &current_room_id, &new_page, &chat_state, &currently_operating_on_item](const std::string &text) mutable {
+ chat_input.on_submit_callback = [this, &chat_input, &tabs, &selected_tab, &current_room, &new_page, &chat_state, &currently_operating_on_item](const std::string &text) mutable {
+ if(!current_room)
+ return false;
+
if(tabs[selected_tab].type == ChatTabType::MESSAGES) {
if(text.empty())
return false;
@@ -3010,7 +3041,7 @@ namespace QuickMedia {
if(chat_state == ChatState::TYPING_MESSAGE) {
// TODO: Make asynchronous
- if(matrix->post_message(current_room_id, text, std::nullopt, std::nullopt) == PluginResult::OK) {
+ if(matrix->post_message(current_room, text, std::nullopt, std::nullopt) == PluginResult::OK) {
chat_input.set_editable(false);
chat_state = ChatState::NAVIGATING;
return true;
@@ -3020,7 +3051,7 @@ namespace QuickMedia {
}
} else if(chat_state == ChatState::REPLYING) {
// TODO: Make asynchronous
- if(matrix->post_reply(current_room_id, text, currently_operating_on_item->userdata) == PluginResult::OK) {
+ if(matrix->post_reply(current_room, text, currently_operating_on_item->userdata) == PluginResult::OK) {
chat_input.set_editable(false);
chat_state = ChatState::NAVIGATING;
currently_operating_on_item = nullptr;
@@ -3031,7 +3062,7 @@ namespace QuickMedia {
}
} else if(chat_state == ChatState::EDITING) {
// TODO: Make asynchronous
- if(matrix->post_edit(current_room_id, text, currently_operating_on_item->userdata) == PluginResult::OK) {
+ if(matrix->post_edit(current_room, text, currently_operating_on_item->userdata) == PluginResult::OK) {
chat_input.set_editable(false);
chat_state = ChatState::NAVIGATING;
currently_operating_on_item = nullptr;
@@ -3046,20 +3077,18 @@ namespace QuickMedia {
};
struct SyncFutureResult {
- BodyItems body_items;
- BodyItems rooms_body_items;
+ Rooms rooms;
RoomSyncMessages room_sync_messages;
};
std::future<SyncFutureResult> sync_future;
bool sync_running = false;
- std::string sync_future_room_id;
sf::Clock sync_timer;
sf::Int32 sync_min_time_ms = 0; // Sync immediately the first time
- std::future<BodyItems> previous_messages_future;
+ std::future<Messages> previous_messages_future;
bool fetching_previous_messages_running = false;
- std::string previous_messages_future_room_id;
+ std::shared_ptr<RoomData> previous_messages_future_room;
const float tab_spacer_height = 0.0f;
sf::Vector2f body_pos;
@@ -3099,11 +3128,11 @@ namespace QuickMedia {
auto room_avatar_thumbnail_data = std::make_shared<ThumbnailData>();
AsyncImageLoader async_image_loader;
- auto typing_async_func = [this](bool new_state, std::string room_id) {
+ auto typing_async_func = [this](bool new_state, std::shared_ptr<RoomData> room) {
if(new_state) {
- matrix->on_start_typing(room_id);
+ matrix->on_start_typing(room);
} else {
- matrix->on_stop_typing(room_id);
+ matrix->on_stop_typing(room);
}
};
std::vector<std::future<void>> typing_futures;
@@ -3187,6 +3216,57 @@ namespace QuickMedia {
return result;
};
+ auto add_new_messages_to_current_room = [&tabs](Messages &messages) {
+ int num_items = tabs[MESSAGES_TAB_INDEX].body->items.size();
+ bool scroll_to_end = (num_items == 0 || (num_items > 0 && tabs[MESSAGES_TAB_INDEX].body->get_selected_item() == num_items - 1));
+
+ BodyItem *selected_item = tabs[MESSAGES_TAB_INDEX].body->get_selected();
+ tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(messages_to_body_items(messages));
+ if(selected_item && !scroll_to_end) {
+ int selected_item_index = tabs[MESSAGES_TAB_INDEX].body->get_index_by_body_item(selected_item);
+ if(selected_item_index != -1)
+ tabs[MESSAGES_TAB_INDEX].body->set_selected_item(selected_item_index);
+ } else if(scroll_to_end) {
+ tabs[MESSAGES_TAB_INDEX].body->select_last_item();
+ }
+ };
+
+ auto add_new_rooms = [&tabs, &body_items_by_room, &current_room, &current_room_body_data, &room_name_text](Rooms &rooms) {
+ if(rooms.empty())
+ return;
+
+ for(size_t i = 0; i < rooms.size(); ++i) {
+ auto &room = rooms[i];
+ std::string room_name = room->name;
+ if(room_name.empty())
+ room_name = room->id;
+
+ auto body_item = BodyItem::create(std::move(room_name));
+ body_item->thumbnail_url = room->avatar_url;
+ body_item->userdata = room.get(); // Note: this has to be valid as long as the room list is valid!
+ tabs[ROOMS_TAB_INDEX].body->items.push_back(body_item);
+ body_items_by_room[room] = { body_item, true, 0 };
+ }
+
+ if(current_room)
+ return;
+
+ current_room = rooms[0];
+ auto room_body_item_it = body_items_by_room.find(current_room);
+ if(room_body_item_it != body_items_by_room.end()) {
+ current_room_body_data = &room_body_item_it->second;
+ room_name_text.setString(current_room_body_data->body_item->get_title());
+ }
+ };
+
+ auto get_room_by_ptr = [&body_items_by_room](RoomData *room_ptr) -> std::shared_ptr<RoomData> {
+ for(auto &[room, body_data] : body_items_by_room) {
+ if(room.get() == room_ptr)
+ return room;
+ }
+ return nullptr;
+ };
+
while (current_page == PageType::CHAT) {
sf::Int32 frame_time_ms = frame_timer.restart().asMilliseconds();
while (window.pollEvent(event)) {
@@ -3218,15 +3298,15 @@ namespace QuickMedia {
hit_top = false;
break;
}
- if(hit_top && !fetching_previous_messages_running && tabs[selected_tab].type == ChatTabType::MESSAGES) {
+ if(hit_top && !fetching_previous_messages_running && tabs[selected_tab].type == ChatTabType::MESSAGES && current_room) {
gradient_inc = 0;
fetching_previous_messages_running = true;
- previous_messages_future_room_id = current_room_id;
- previous_messages_future = std::async(std::launch::async, [this, &previous_messages_future_room_id]() {
- BodyItems result_items;
- if(matrix->get_previous_room_messages(previous_messages_future_room_id, result_items) != PluginResult::OK)
- fprintf(stderr, "Failed to get previous matrix messages in room: %s\n", previous_messages_future_room_id.c_str());
- return result_items;
+ previous_messages_future_room = current_room;
+ previous_messages_future = std::async(std::launch::async, [this, &previous_messages_future_room]() {
+ Messages messages;
+ if(matrix->get_previous_room_messages(previous_messages_future_room, messages) != PluginResult::OK)
+ fprintf(stderr, "Failed to get previous matrix messages in room: %s\n", previous_messages_future_room->id.c_str());
+ return messages;
});
}
} else if(event.key.code == sf::Keyboard::Down || event.key.code == sf::Keyboard::J) {
@@ -3242,20 +3322,20 @@ namespace QuickMedia {
selected_tab = std::max(0, selected_tab - 1);
read_marker_timer.restart();
redraw = true;
- if(typing) {
+ if(typing && current_room) {
fprintf(stderr, "Stopped typing\n");
typing = false;
- typing_futures.push_back(std::async(typing_async_func, false, current_room_id));
+ typing_futures.push_back(std::async(typing_async_func, false, current_room));
}
} else if((event.key.code == sf::Keyboard::Right || event.key.code == sf::Keyboard::L) && synced) {
tabs[selected_tab].body->clear_cache();
selected_tab = std::min((int)tabs.size() - 1, selected_tab + 1);
read_marker_timer.restart();
redraw = true;
- if(typing) {
+ if(typing && current_room) {
fprintf(stderr, "Stopped typing\n");
typing = false;
- typing_futures.push_back(std::async(typing_async_func, false, current_room_id));
+ typing_futures.push_back(std::async(typing_async_func, false, current_room));
}
}
@@ -3326,7 +3406,7 @@ namespace QuickMedia {
continue;
launch_url(selected_item->get_title());
}
- } else if(event.type == sf::Event::KeyReleased && chat_state == ChatState::NAVIGATING && tabs[selected_tab].type == ChatTabType::MESSAGES) {
+ } else if(event.type == sf::Event::KeyReleased && chat_state == ChatState::NAVIGATING && tabs[selected_tab].type == ChatTabType::MESSAGES && current_room) {
if(event.key.code == sf::Keyboard::U) {
new_page = PageType::FILE_MANAGER;
chat_input.set_editable(false);
@@ -3341,7 +3421,7 @@ namespace QuickMedia {
// TODO: Make asynchronous.
// TODO: Upload multiple files.
std::string err_msg;
- if(matrix->post_file(current_room_id, sf::Clipboard::getString(), err_msg) != PluginResult::OK) {
+ if(matrix->post_file(current_room, sf::Clipboard::getString(), err_msg) != PluginResult::OK) {
std::string desc = "Failed to upload media to room, error: " + err_msg;
show_notification("QuickMedia", desc.c_str(), Urgency::CRITICAL);
}
@@ -3382,13 +3462,13 @@ namespace QuickMedia {
show_notification("QuickMedia", "No message selected for editing");
}
}
-
+
if(event.key.code == sf::Keyboard::D) {
BodyItem *selected = tabs[selected_tab].body->get_selected();
if(selected) {
// TODO: Make asynchronous
std::string err_msg;
- if(matrix->delete_message(current_room_id, selected->userdata, err_msg) != PluginResult::OK) {
+ if(matrix->delete_message(current_room, selected->userdata, err_msg) != PluginResult::OK) {
// TODO: Show inline notification
show_notification("QuickMedia", "Failed to delete message, reason: " + err_msg, Urgency::CRITICAL);
}
@@ -3405,9 +3485,9 @@ namespace QuickMedia {
// TODO: Also show typing event when ctrl+v pasting?
if(event.text.unicode != 13) { // Return key
start_typing_timer.restart();
- if(!typing) {
+ if(!typing && current_room) {
fprintf(stderr, "Started typing\n");
- typing_futures.push_back(std::async(typing_async_func, true, current_room_id));
+ typing_futures.push_back(std::async(typing_async_func, true, current_room));
}
typing = true;
}
@@ -3416,10 +3496,10 @@ namespace QuickMedia {
chat_input.set_text("");
chat_state = ChatState::NAVIGATING;
currently_operating_on_item = nullptr;
- if(typing) {
+ if(typing && current_room) {
fprintf(stderr, "Stopped typing\n");
typing = false;
- typing_futures.push_back(std::async(typing_async_func, false, current_room_id));
+ typing_futures.push_back(std::async(typing_async_func, false, current_room));
}
}
//chat_input.on_event(event);
@@ -3428,21 +3508,23 @@ namespace QuickMedia {
BodyItem *selected_item = tabs[selected_tab].body->get_selected();
if(selected_item) {
tabs[selected_tab].body->clear_cache();
- current_room_id = selected_item->url;
+
+ current_room = get_room_by_ptr((RoomData*)selected_item->userdata);
+ assert(current_room);
selected_tab = MESSAGES_TAB_INDEX;
tabs[MESSAGES_TAB_INDEX].body->clear_items();
- BodyItems new_items;
- if(matrix->get_all_synced_room_messages(current_room_id, new_items) == PluginResult::OK) {
- tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(std::move(new_items));
+ Messages new_messages;
+ if(matrix->get_all_synced_room_messages(current_room, new_messages) == PluginResult::OK) {
+ tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(messages_to_body_items(new_messages));
tabs[MESSAGES_TAB_INDEX].body->select_last_item();
} else {
- std::string err_msg = "Failed to get messages in room: " + current_room_id;
+ std::string err_msg = "Failed to get messages in room: " + current_room->id;
show_notification("QuickMedia", err_msg, Urgency::CRITICAL);
}
- auto room_body_item_it = body_items_by_room_id.find(current_room_id);
- if(room_body_item_it != body_items_by_room_id.end()) {
+ auto room_body_item_it = body_items_by_room.find(current_room);
+ if(room_body_item_it != body_items_by_room.end()) {
current_room_body_data = &room_body_item_it->second;
room_name_text.setString(current_room_body_data->body_item->get_title());
room_avatar_thumbnail_data = std::make_shared<ThumbnailData>();
@@ -3458,28 +3540,30 @@ namespace QuickMedia {
case PageType::FILE_MANAGER: {
new_page = PageType::CHAT;
- auto file_manager_page = std::make_unique<FileManagerPage>(this);
- file_manager_page->set_current_directory(get_home_dir().data);
- auto file_manager_body = create_body();
- file_manager_page->get_files_in_directory(file_manager_body->items);
- std::vector<Tab> tabs;
- tabs.push_back(Tab{std::move(file_manager_body), std::move(file_manager_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)});
-
- selected_files.clear();
- page_loop(std::move(tabs));
-
- if(selected_files.empty()) {
- fprintf(stderr, "No files selected!\n");
- } else {
- // TODO: Make asynchronous.
- // TODO: Upload multiple files.
- std::string err_msg;
- if(matrix->post_file(current_room_id, selected_files[0], err_msg) != PluginResult::OK) {
- std::string desc = "Failed to upload media to room, error: " + err_msg;
- show_notification("QuickMedia", desc.c_str(), Urgency::CRITICAL);
+ if(current_room) {
+ auto file_manager_page = std::make_unique<FileManagerPage>(this);
+ file_manager_page->set_current_directory(get_home_dir().data);
+ auto file_manager_body = create_body();
+ file_manager_page->get_files_in_directory(file_manager_body->items);
+ std::vector<Tab> tabs;
+ tabs.push_back(Tab{std::move(file_manager_body), std::move(file_manager_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)});
+
+ selected_files.clear();
+ page_loop(std::move(tabs));
+
+ if(selected_files.empty()) {
+ fprintf(stderr, "No files selected!\n");
+ } else {
+ // TODO: Make asynchronous.
+ // TODO: Upload multiple files.
+ std::string err_msg;
+ if(matrix->post_file(current_room, selected_files[0], err_msg) != PluginResult::OK) {
+ std::string desc = "Failed to upload media to room, error: " + err_msg;
+ show_notification("QuickMedia", desc.c_str(), Urgency::CRITICAL);
+ }
}
+ redraw = true;
}
- redraw = true;
break;
}
case PageType::CHAT_LOGIN: {
@@ -3502,10 +3586,10 @@ namespace QuickMedia {
break;
}
- if(typing && start_typing_timer.getElapsedTime().asSeconds() >= typing_timeout_seconds) {
+ if(typing && start_typing_timer.getElapsedTime().asSeconds() >= typing_timeout_seconds && current_room) {
fprintf(stderr, "Stopped typing\n");
typing = false;
- typing_futures.push_back(std::async(typing_async_func, false, current_room_id));
+ typing_futures.push_back(std::async(typing_async_func, false, current_room));
}
for(auto it = typing_futures.begin(); it != typing_futures.end(); ) {
@@ -3589,26 +3673,18 @@ namespace QuickMedia {
sync_min_time_ms = 50;
sync_running = true;
sync_timer.restart();
- sync_future_room_id = current_room_id;
- sync_future = std::async(std::launch::async, [this, &sync_future_room_id, synced]() {
+ sync_future = std::async(std::launch::async, [this, synced]() {
SyncFutureResult result;
if(matrix->sync(result.room_sync_messages) == PluginResult::OK) {
fprintf(stderr, "Synced matrix\n");
if(!synced) {
- if(matrix->get_joined_rooms(result.rooms_body_items) != PluginResult::OK) {
+ if(matrix->get_joined_rooms(result.rooms) != PluginResult::OK) {
show_notification("QuickMedia", "Failed to get a list of joined rooms", Urgency::CRITICAL);
current_page = PageType::EXIT;
return result;
}
}
-
- if(sync_future_room_id.empty() && !result.rooms_body_items.empty())
- sync_future_room_id = result.rooms_body_items[0]->url;
-
- if(matrix->get_new_room_messages(sync_future_room_id, result.body_items) != PluginResult::OK) {
- fprintf(stderr, "Failed to get new matrix messages in room: %s\n", sync_future_room_id.c_str());
- }
} else {
fprintf(stderr, "Failed to sync matrix\n");
}
@@ -3619,40 +3695,14 @@ namespace QuickMedia {
if(sync_future.valid() && sync_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
SyncFutureResult sync_result = sync_future.get();
- // Ignore finished sync if it happened in another room. When we navigate back to the room we will get the messages again
- if(sync_future_room_id == current_room_id || !synced) {
- int num_items = tabs[MESSAGES_TAB_INDEX].body->items.size();
- bool scroll_to_end = (num_items == 0 || (num_items > 0 && tabs[MESSAGES_TAB_INDEX].body->get_selected_item() == num_items - 1));
-
- BodyItem *selected_item = tabs[MESSAGES_TAB_INDEX].body->get_selected();
- tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(std::move(sync_result.body_items));
- if(selected_item && !scroll_to_end) {
- int selected_item_index = tabs[MESSAGES_TAB_INDEX].body->get_index_by_body_item(selected_item);
- if(selected_item_index != -1)
- tabs[MESSAGES_TAB_INDEX].body->set_selected_item(selected_item_index);
- } else if(scroll_to_end) {
- tabs[MESSAGES_TAB_INDEX].body->select_last_item();
- }
- }
- // Initial sync
- if(!synced) {
- tabs[ROOMS_TAB_INDEX].body->items = std::move(sync_result.rooms_body_items);
+ add_new_rooms(sync_result.rooms);
- for(auto body_item : tabs[ROOMS_TAB_INDEX].body->items) {
- body_items_by_room_id[body_item->url] = { body_item, true, 0 };
- }
+ auto room_messages_it = sync_result.room_sync_messages.find(current_room);
+ if(room_messages_it != sync_result.room_sync_messages.end())
+ add_new_messages_to_current_room(room_messages_it->second);
- // The room id should be saved in a file when changing viewed room.
- if(!tabs[ROOMS_TAB_INDEX].body->items.empty())
- current_room_id = tabs[ROOMS_TAB_INDEX].body->items[0]->url;
-
- auto room_body_item_it = body_items_by_room_id.find(current_room_id);
- if(room_body_item_it != body_items_by_room_id.end()) {
- current_room_body_data = &room_body_item_it->second;
- room_name_text.setString(current_room_body_data->body_item->get_title());
- }
- }
+ //modify_related_messages()
process_new_room_messages(sync_result.room_sync_messages, !synced);
sync_running = false;
@@ -3666,13 +3716,13 @@ namespace QuickMedia {
}
if(fetching_previous_messages_running && previous_messages_future.valid() && previous_messages_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
- BodyItems new_body_items = previous_messages_future.get();
- fprintf(stderr, "Finished fetching older messages, num new messages: %zu\n", new_body_items.size());
+ Messages new_messages = previous_messages_future.get();
+ fprintf(stderr, "Finished fetching older messages, num new messages: %zu\n", new_messages.size());
// Ignore finished fetch of messages if it happened in another room. When we navigate back to the room we will get the messages again
- size_t num_new_messages = new_body_items.size();
- if(previous_messages_future_room_id == current_room_id && num_new_messages > 0) {
+ size_t num_new_messages = new_messages.size();
+ if(previous_messages_future_room == current_room && num_new_messages > 0) {
BodyItem *selected_item = tabs[MESSAGES_TAB_INDEX].body->get_selected();
- tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(std::move(new_body_items));
+ tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(messages_to_body_items(new_messages));
if(selected_item) {
int selected_item_index = tabs[MESSAGES_TAB_INDEX].body->get_index_by_body_item(selected_item);
if(selected_item_index != -1)
@@ -3799,7 +3849,7 @@ namespace QuickMedia {
window.draw(loading_text);
}
- if(tabs[selected_tab].type == ChatTabType::MESSAGES) {
+ if(tabs[selected_tab].type == ChatTabType::MESSAGES && current_room) {
BodyItem *last_visible_item = tabs[selected_tab].body->get_last_fully_visible_item();
if(is_window_focused && chat_state != ChatState::URL_SELECTION && current_room_body_data && last_visible_item && !setting_read_marker && read_marker_timer.getElapsedTime().asMilliseconds() >= read_marker_timeout_ms) {
Message *message = (Message*)last_visible_item->userdata;
@@ -3809,8 +3859,9 @@ namespace QuickMedia {
current_room_body_data->last_read_message_timestamp = message->timestamp;
// TODO: What if the message is no longer valid?
setting_read_marker = true;
- set_read_marker_future = std::async(std::launch::async, [this, current_room_id, message]() mutable {
- if(matrix->set_read_marker(current_room_id, message) != PluginResult::OK) {
+ std::shared_ptr<RoomData> room = current_room;
+ set_read_marker_future = std::async(std::launch::async, [this, room, message]() mutable {
+ if(matrix->set_read_marker(room, message) != PluginResult::OK) {
fprintf(stderr, "Warning: failed to set read marker to %s\n", message->event_id.c_str());
}
});