From dd6c860aed79c5adf0db08142bbbf7b8ec3fbe67 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 29 May 2019 16:53:34 +0200 Subject: Don't change type of a conversation --- libdino/src/service/conversation_manager.vala | 57 ++++++++++++++++++--------- 1 file changed, 39 insertions(+), 18 deletions(-) (limited to 'libdino/src/service/conversation_manager.vala') diff --git a/libdino/src/service/conversation_manager.vala b/libdino/src/service/conversation_manager.vala index 77205c57..b1e8d8a8 100644 --- a/libdino/src/service/conversation_manager.vala +++ b/libdino/src/service/conversation_manager.vala @@ -14,7 +14,7 @@ public class ConversationManager : StreamInteractionModule, Object { private StreamInteractor stream_interactor; private Database db; - private HashMap> conversations = new HashMap>(Account.hash_func, Account.equals_func); + private HashMap>> conversations = new HashMap>>(Account.hash_func, Account.equals_func); public static void start(StreamInteractor stream_interactor, Database db) { ConversationManager m = new ConversationManager(stream_interactor, db); @@ -33,15 +33,19 @@ public class ConversationManager : StreamInteractionModule, Object { public Conversation create_conversation(Jid jid, Account account, Conversation.Type? type = null) { assert(conversations.has_key(account)); Jid store_jid = type == Conversation.Type.GROUPCHAT ? jid.bare_jid : jid; - if (conversations[account].has_key(store_jid)) { - conversations[account][store_jid].type_ = type; - return conversations[account][store_jid]; - } else { - Conversation conversation = new Conversation(jid, account, type); - add_conversation(conversation); - conversation.persist(db); - return conversation; + + // Do we already have a conversation for this jid? + foreach (var conversation in conversations[account][store_jid]) { + if (conversation.type_ == type) { + return conversation; + } } + + // Create a new converation + Conversation conversation = new Conversation(jid, account, type); + add_conversation(conversation); + conversation.persist(db); + return conversation; } public Conversation? get_conversation_for_message(Entities.Message message) { @@ -68,17 +72,27 @@ public class ConversationManager : StreamInteractionModule, Object { return ret; } - public Conversation? get_conversation(Jid jid, Account account) { + public Conversation? get_conversation(Jid jid, Account account, Conversation.Type? type = null) { if (conversations.has_key(account)) { - return conversations[account][jid]; + if (conversations[account].has_key(jid)) { + foreach (var conversation in conversations[account][jid]) { + if (type == null || conversation.type_ == type) { + return conversation; + } + } + } } return null; } public Conversation? get_conversation_by_id(int id) { - foreach (HashMap hm in conversations.values) { - foreach (Conversation conversation in hm.values) { - if (conversation.id == id) return conversation; + foreach (HashMap> hm in conversations.values) { + foreach (Gee.List hm2 in hm.values) { + foreach (Conversation conversation in hm2) { + if (conversation.id == id) { + return conversation; + } + } } } return null; @@ -88,8 +102,10 @@ public class ConversationManager : StreamInteractionModule, Object { Gee.List ret = new ArrayList(Conversation.equals_func); foreach (Account account_ in conversations.keys) { if (account != null && !account_.equals(account)) continue; - foreach (Conversation conversation in conversations[account_].values) { - if(conversation.active) ret.add(conversation); + foreach (Gee.List list in conversations[account_].values) { + foreach (var conversation in list) { + if(conversation.active) ret.add(conversation); + } } } return ret; @@ -112,7 +128,7 @@ public class ConversationManager : StreamInteractionModule, Object { } private void on_account_added(Account account) { - conversations[account] = new HashMap(Jid.hash_func, Jid.equals_func); + conversations[account] = new HashMap>(Jid.hash_func, Jid.equals_func); foreach (Conversation conversation in db.get_conversations(account)) { add_conversation(conversation); } @@ -153,7 +169,12 @@ public class ConversationManager : StreamInteractionModule, Object { } private void add_conversation(Conversation conversation) { - conversations[conversation.account][conversation.counterpart] = conversation; + if (!conversations[conversation.account].has_key(conversation.counterpart)) { + conversations[conversation.account][conversation.counterpart] = new ArrayList(Conversation.equals_func); + } + + conversations[conversation.account][conversation.counterpart].add(conversation); + if (conversation.active) { conversation_activated(conversation); } -- cgit v1.2.3-54-g00ecf