diff --git a/libdino/src/service/message_storage.vala b/libdino/src/service/message_storage.vala index f4691971..27d9504d 100644 --- a/libdino/src/service/message_storage.vala +++ b/libdino/src/service/message_storage.vala @@ -34,9 +34,11 @@ public class MessageStorage : StreamInteractionModule, Object { Gee.List ret = new ArrayList(Message.equals_func); BidirIterator iter = messages[conversation].bidir_iterator(); iter.last(); - while (iter.valid && ret.size < count) { - iter.previous(); - ret.insert(0, iter.get()); + if (messages[conversation].size > 0) { + do { + ret.insert(0, iter.get()); + iter.previous(); + } while (iter.has_previous() && ret.size < count); } return ret; } @@ -49,9 +51,23 @@ public class MessageStorage : StreamInteractionModule, Object { return null; } - public Gee.List? get_messages_before(Conversation? conversation, DateTime before, int count = 20) { - Gee.List db_messages = db.get_messages(conversation.counterpart, conversation.account, Util.get_message_type_for_conversation(conversation), count, before); - return db_messages; + public Gee.List? get_messages_before_message(Conversation? conversation, Message message, int count = 20) { + SortedSet? before = messages[conversation].head_set(message); + if (before != null && before.size >= count) { + Gee.List ret = new ArrayList(Message.equals_func); + Iterator iter = before.iterator(); + iter.next(); + for (int from_index = before.size - count; iter.has_next() && from_index > 0; from_index--) iter.next(); + while(iter.has_next()) { + Message m = iter.get(); + ret.add(m); + iter.next(); + } + return ret; + } else { + Gee.List db_messages = db.get_messages(conversation.counterpart, conversation.account, Util.get_message_type_for_conversation(conversation), count, message.local_time); + return db_messages; + } } public Message? get_message_by_id(string stanza_id, Conversation conversation) { diff --git a/main/src/ui/conversation_summary/conversation_view.vala b/main/src/ui/conversation_summary/conversation_view.vala index 37412335..7924da57 100644 --- a/main/src/ui/conversation_summary/conversation_view.vala +++ b/main/src/ui/conversation_summary/conversation_view.vala @@ -74,7 +74,8 @@ public class ConversationView : Box, Plugins.ConversationItemCollection { populator.init(conversation, this, Plugins.WidgetType.GTK); } message_item_populator.init(conversation, this); - message_item_populator.populate_number(conversation, new DateTime.now_utc(), 50); + message_item_populator.populate_latest(conversation, 40); + Idle.add(() => { on_value_notify(); return false; }); stack.set_visible_child_name("main"); } @@ -217,7 +218,7 @@ public class ConversationView : Box, Plugins.ConversationItemCollection { private void load_earlier_messages() { was_value = scrolled.vadjustment.value; if (!reloading_mutex.trylock()) return; - if (meta_items.size > 0) message_item_populator.populate_number(conversation, meta_items.first().sort_time, 20); + if (meta_items.size > 0) message_item_populator.populate_before(conversation, meta_items.first(), 20); } private static int sort_meta_items(Plugins.MetaConversationItem a, Plugins.MetaConversationItem b) { diff --git a/main/src/ui/conversation_summary/message_populator.vala b/main/src/ui/conversation_summary/message_populator.vala index df44a14b..b342306b 100644 --- a/main/src/ui/conversation_summary/message_populator.vala +++ b/main/src/ui/conversation_summary/message_populator.vala @@ -10,6 +10,7 @@ public class MessagePopulator : Object { private StreamInteractor? stream_interactor; private Conversation? current_conversation; private Plugins.ConversationItemCollection? item_collection; + private HashMap meta_message = new HashMap(); public MessagePopulator(StreamInteractor stream_interactor) { this.stream_interactor = stream_interactor; @@ -30,8 +31,17 @@ public class MessagePopulator : Object { public void close(Conversation conversation) { } - public void populate_number(Conversation conversation, DateTime from, int n) { - Gee.List? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages_before(conversation, from, n); + public void populate_latest(Conversation conversation, int n) { + Gee.List? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages(conversation, n); + if (messages != null) { + foreach (Entities.Message message in messages) { + handle_message(message, conversation); + } + } + } + + public void populate_before(Conversation conversation, Plugins.MetaConversationItem item, int n) { + Gee.List? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages_before_message(conversation, meta_message[item], n); if (messages != null) { foreach (Entities.Message message in messages) { handle_message(message, conversation); @@ -53,6 +63,7 @@ public class MessagePopulator : Object { } Plugins.MetaConversationItem? meta_item = best_provider.get_item(message, conversation); if (meta_item == null) return; + meta_message[meta_item] = message; meta_item.mark = message.marked; WeakRef weak_meta_item = WeakRef(meta_item);