From 81310dff2e712cf7ddbce7c4717cd5a77170c79a Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 7 Mar 2018 17:24:57 +0100 Subject: Handle multiple chat state notifications per bare jid fixes #117 --- .../service/counterpart_interaction_manager.vala | 29 +++++++++++++++------- libdino/src/service/message_processor.vala | 4 +-- 2 files changed, 22 insertions(+), 11 deletions(-) (limited to 'libdino/src') diff --git a/libdino/src/service/counterpart_interaction_manager.vala b/libdino/src/service/counterpart_interaction_manager.vala index b6c1faab..1548d9fa 100644 --- a/libdino/src/service/counterpart_interaction_manager.vala +++ b/libdino/src/service/counterpart_interaction_manager.vala @@ -14,7 +14,7 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { public signal void received_message_displayed(Account account, Jid jid, Entities.Message message); private StreamInteractor stream_interactor; - private HashMap chat_states = new HashMap(Jid.hash_bare_func, Jid.equals_bare_func); + private HashMap> chat_states = new HashMap>(Conversation.hash_func, Conversation.equals_func); private HashMap marker_wo_message = new HashMap(); public static void start(StreamInteractor stream_interactor) { @@ -30,9 +30,9 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { stream_interactor.stream_negotiated.connect(() => chat_states.clear() ); } - public string? get_chat_state(Account account, Jid jid) { - if (stream_interactor.connection_manager.get_state(account) != ConnectionManager.ConnectionState.CONNECTED) return null; - return chat_states[jid]; + public HashMap? get_chat_states(Conversation conversation) { + if (stream_interactor.connection_manager.get_state(conversation.account) != ConnectionManager.ConnectionState.CONNECTED) return null; + return chat_states[conversation]; } private void on_account_added(Account account) { @@ -42,13 +42,24 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { stream_interactor.module_manager.get_module(account, Xep.MessageDeliveryReceipts.Module.IDENTITY).receipt_received.connect((stream, jid, id) => { on_receipt_received(account, jid, id); }); - stream_interactor.module_manager.get_module(account, Xep.ChatStateNotifications.Module.IDENTITY).chat_state_received.connect((stream, jid, state) => { - on_chat_state_received(account, jid, state); + stream_interactor.module_manager.get_module(account, Xep.ChatStateNotifications.Module.IDENTITY).chat_state_received.connect((stream, jid, state, stanza) => { + on_chat_state_received.begin(account, jid, state, stanza); }); } - private void on_chat_state_received(Account account, Jid jid, string state) { - chat_states[jid] = state; + private async void on_chat_state_received(Account account, Jid jid, string state, MessageStanza stanza) { + Message message = yield stream_interactor.get_module(MessageProcessor.IDENTITY).parse_message_stanza(account, stanza); + Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_for_message(message); + if (conversation == null) return; + + if (!chat_states.has_key(conversation)) { + chat_states[conversation] = new HashMap(Jid.hash_func, Jid.equals_func); + } + if (state == Xmpp.Xep.ChatStateNotifications.STATE_ACTIVE) { + chat_states[conversation].unset(jid); + } else { + chat_states[conversation][jid] = state; + } received_state(account, jid, state); } @@ -110,7 +121,7 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { } public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { - outer.on_chat_state_received(conversation.account, conversation.counterpart, Xep.ChatStateNotifications.STATE_ACTIVE); + outer.on_chat_state_received.begin(conversation.account, conversation.counterpart, Xep.ChatStateNotifications.STATE_ACTIVE, stanza); return false; } } diff --git a/libdino/src/service/message_processor.vala b/libdino/src/service/message_processor.vala index 9a20cf3f..f2f539bb 100644 --- a/libdino/src/service/message_processor.vala +++ b/libdino/src/service/message_processor.vala @@ -76,7 +76,7 @@ public class MessageProcessor : StreamInteractionModule, Object { private async void on_message_received(Account account, Xmpp.MessageStanza message_stanza) { if (message_stanza.body == null) return; - Entities.Message message = yield create_in_message(account, message_stanza); + Entities.Message message = yield parse_message_stanza(account, message_stanza); Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_for_message(message); if (conversation != null) { @@ -90,7 +90,7 @@ public class MessageProcessor : StreamInteractionModule, Object { } } - private async Entities.Message create_in_message(Account account, Xmpp.MessageStanza message) { + public async Entities.Message parse_message_stanza(Account account, Xmpp.MessageStanza message) { Entities.Message new_message = new Entities.Message(message.body); new_message.account = account; new_message.stanza_id = message.id; -- cgit v1.2.3-70-g09d2