2017-04-04 13:47:00 +00:00
|
|
|
using Gee;
|
2018-07-09 22:31:39 +00:00
|
|
|
using Qlite;
|
2017-04-04 13:47:00 +00:00
|
|
|
|
|
|
|
using Dino.Entities;
|
|
|
|
|
|
|
|
namespace Dino {
|
|
|
|
|
|
|
|
public class MessageStorage : StreamInteractionModule, Object {
|
|
|
|
public static ModuleIdentity<MessageStorage> IDENTITY = new ModuleIdentity<MessageStorage>("message_cache");
|
|
|
|
public string id { get { return IDENTITY.id; } }
|
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
|
|
|
private Database db;
|
|
|
|
|
2017-08-17 22:46:40 +00:00
|
|
|
private HashMap<Conversation, Gee.TreeSet<Message>> messages = new HashMap<Conversation, Gee.TreeSet<Message>>(Conversation.hash_func, Conversation.equals_func);
|
2017-04-04 13:47:00 +00:00
|
|
|
|
|
|
|
public static void start(StreamInteractor stream_interactor, Database db) {
|
|
|
|
MessageStorage m = new MessageStorage(stream_interactor, db);
|
|
|
|
stream_interactor.add_module(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
private MessageStorage(StreamInteractor stream_interactor, Database db) {
|
|
|
|
this.stream_interactor = stream_interactor;
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add_message(Message message, Conversation conversation) {
|
|
|
|
message.persist(db);
|
|
|
|
init_conversation(conversation);
|
|
|
|
messages[conversation].add(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Gee.List<Message> get_messages(Conversation conversation, int count = 50) {
|
|
|
|
init_conversation(conversation);
|
2017-08-17 22:46:40 +00:00
|
|
|
Gee.List<Message> ret = new ArrayList<Message>(Message.equals_func);
|
2017-11-18 18:42:22 +00:00
|
|
|
BidirIterator<Message> iter = messages[conversation].bidir_iterator();
|
|
|
|
iter.last();
|
2017-12-06 17:08:20 +00:00
|
|
|
if (messages[conversation].size > 0) {
|
|
|
|
do {
|
|
|
|
ret.insert(0, iter.get());
|
|
|
|
iter.previous();
|
|
|
|
} while (iter.has_previous() && ret.size < count);
|
2017-04-04 13:47:00 +00:00
|
|
|
}
|
2017-08-17 22:46:40 +00:00
|
|
|
return ret;
|
2017-04-04 13:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Message? get_last_message(Conversation conversation) {
|
|
|
|
init_conversation(conversation);
|
|
|
|
if (messages[conversation].size > 0) {
|
2017-11-18 18:42:22 +00:00
|
|
|
return messages[conversation].last();
|
2017-04-04 13:47:00 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-06-23 09:59:21 +00:00
|
|
|
public Gee.List<Message>? get_messages_before_message(Conversation? conversation, DateTime before, int id, int count = 20) {
|
2018-06-19 16:07:00 +00:00
|
|
|
// SortedSet<Message>? before = messages[conversation].head_set(message);
|
|
|
|
// if (before != null && before.size >= count) {
|
|
|
|
// Gee.List<Message> ret = new ArrayList<Message>(Message.equals_func);
|
|
|
|
// Iterator<Message> 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 {
|
2018-06-23 09:59:21 +00:00
|
|
|
Gee.List<Message> db_messages = db.get_messages(conversation.counterpart, conversation.account, Util.get_message_type_for_conversation(conversation), count, before, null, id);
|
2017-12-06 17:08:20 +00:00
|
|
|
return db_messages;
|
2018-06-19 16:07:00 +00:00
|
|
|
// }
|
2017-04-04 13:47:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 09:59:21 +00:00
|
|
|
public Gee.List<Message>? get_messages_after_message(Conversation? conversation, DateTime after, int id, int count = 20) {
|
|
|
|
Gee.List<Message> db_messages = db.get_messages(conversation.counterpart, conversation.account, Util.get_message_type_for_conversation(conversation), count, null, after, id);
|
|
|
|
return db_messages;
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:26:39 +00:00
|
|
|
public Message? get_message_by_id(int id, Conversation conversation) {
|
|
|
|
init_conversation(conversation);
|
|
|
|
foreach (Message message in messages[conversation]) {
|
|
|
|
if (message.id == id) return message;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Message? get_message_by_stanza_id(string stanza_id, Conversation conversation) {
|
2017-04-04 13:47:00 +00:00
|
|
|
init_conversation(conversation);
|
2017-08-17 22:46:40 +00:00
|
|
|
foreach (Message message in messages[conversation]) {
|
|
|
|
if (message.stanza_id == stanza_id) return message;
|
2017-04-04 13:47:00 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-28 20:02:39 +00:00
|
|
|
public Conversation? get_conversation_for_stanza_id(Account account, string stanza_id) {
|
|
|
|
foreach (Conversation conversation in messages.keys) {
|
|
|
|
if (!conversation.account.equals(account)) continue;
|
|
|
|
foreach (Message message in messages[conversation]) {
|
|
|
|
if (message.stanza_id == stanza_id) return conversation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-04 13:47:00 +00:00
|
|
|
private void init_conversation(Conversation conversation) {
|
|
|
|
if (!messages.has_key(conversation)) {
|
2017-11-21 21:17:04 +00:00
|
|
|
messages[conversation] = new Gee.TreeSet<Message>((a, b) => {
|
|
|
|
int res = a.local_time.compare(b.local_time);
|
|
|
|
if (res == 0) {
|
|
|
|
res = a.time.compare(b.time);
|
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
res = a.id - b.id > 0 ? 1 : -1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
});
|
2018-06-23 09:59:21 +00:00
|
|
|
Gee.List<Message> db_messages = db.get_messages(conversation.counterpart, conversation.account, Util.get_message_type_for_conversation(conversation), 50, null, null, -1);
|
2017-08-17 22:46:40 +00:00
|
|
|
messages[conversation].add_all(db_messages);
|
2017-04-04 13:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:46:40 +00:00
|
|
|
}
|