aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Matrix.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-11-08 16:35:30 +0100
committerdec05eba <dec05eba@protonmail.com>2020-11-08 16:35:30 +0100
commitf8748d14ea11d6d53ad46aee2832180daf05fb77 (patch)
tree9a3e0d04042f347675a5db8f827f3ec961417ab0 /src/plugins/Matrix.cpp
parent240767f49483672466de638e93aaa8cb47f2f854 (diff)
Matrix: show last message sorted by timestamp in room description
Diffstat (limited to 'src/plugins/Matrix.cpp')
-rw-r--r--src/plugins/Matrix.cpp33
1 files changed, 25 insertions, 8 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index c1f2b3c..7c170c9 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -373,6 +373,12 @@ namespace QuickMedia {
unread_notifications.clear();
}
+ static std::shared_ptr<Message> get_last_message_by_timestamp(const Messages &messages) {
+ return *std::max_element(messages.begin(), messages.end(), [](const std::shared_ptr<Message> &message1, const std::shared_ptr<Message> &message2) {
+ return message1->timestamp < message2->timestamp;
+ });
+ }
+
void MatrixQuickMedia::update_room_description(RoomData *room, bool is_initial_sync) {
room->acquire_room_lock();
const Messages &messages = room->get_messages_thread_unsafe();
@@ -385,10 +391,25 @@ namespace QuickMedia {
read_marker_message_timestamp = read_marker_message->timestamp;
}
- // 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.
+ std::shared_ptr<Message> last_message = get_last_message_by_timestamp(messages);
+ if(!last_message) {
+ room->release_room_lock();
+ return;
+ }
+
+ auto last_message_it = last_message_by_room.find(room);
+ if(last_message_it != last_message_by_room.end()) {
+ if(last_message->timestamp > last_message_it->second->timestamp)
+ last_message_it->second = last_message;
+ else
+ last_message = last_message_it->second;
+ } else {
+ last_message_by_room[room] = last_message;
+ }
+
Message *last_unread_message = nullptr;
- if(!messages.empty() && messages.back()->timestamp > read_marker_message_timestamp)
- last_unread_message = messages.back().get();
+ if(last_message->timestamp > read_marker_message_timestamp)
+ last_unread_message = last_message.get();
BodyItem *room_body_item = static_cast<BodyItem*>(room->userdata);
assert(room_body_item);
@@ -405,11 +426,7 @@ namespace QuickMedia {
rooms_page->move_room_to_top(room);
room_tags_page->move_room_to_top(room);
} else if(is_initial_sync) {
- Message *last_message = nullptr;
- if(!messages.empty())
- last_message = messages.back().get();
- if(last_message)
- room_body_item->set_description(matrix->message_get_author_displayname(last_message) + ": " + extract_first_line_elipses(last_message->body, 150));
+ room_body_item->set_description(matrix->message_get_author_displayname(last_message.get()) + ": " + extract_first_line_elipses(last_message->body, 150));
}
room->release_room_lock();