From 4c953b58827b0d3ccb1f3c052ed1c2b7242e7afa Mon Sep 17 00:00:00 2001 From: fiaxh Date: Thu, 20 Feb 2020 16:59:34 +0100 Subject: Only distinguish between typing or not (incoming) fixes #739 --- libdino/src/application.vala | 4 +- .../service/counterpart_interaction_manager.vala | 89 ++++++++++++++-------- libdino/src/service/message_processor.vala | 3 + libdino/src/service/presence_manager.vala | 3 +- 4 files changed, 65 insertions(+), 34 deletions(-) (limited to 'libdino/src') diff --git a/libdino/src/application.vala b/libdino/src/application.vala index 82d392f6..e7e02be7 100644 --- a/libdino/src/application.vala +++ b/libdino/src/application.vala @@ -31,8 +31,8 @@ public interface Application : GLib.Application { MessageProcessor.start(stream_interactor, db); MessageStorage.start(stream_interactor, db); - CounterpartInteractionManager.start(stream_interactor); PresenceManager.start(stream_interactor); + CounterpartInteractionManager.start(stream_interactor); BlockingManager.start(stream_interactor); ConversationManager.start(stream_interactor, db); MucManager.start(stream_interactor); @@ -136,4 +136,4 @@ public interface Application : GLib.Application { } } -} \ No newline at end of file +} diff --git a/libdino/src/service/counterpart_interaction_manager.vala b/libdino/src/service/counterpart_interaction_manager.vala index b994ca64..88818e95 100644 --- a/libdino/src/service/counterpart_interaction_manager.vala +++ b/libdino/src/service/counterpart_interaction_manager.vala @@ -8,13 +8,13 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { public static ModuleIdentity IDENTITY = new ModuleIdentity("counterpart_interaction_manager"); public string id { get { return IDENTITY.id; } } - public signal void received_state(Account account, Jid jid, string state); + public signal void received_state(Conversation conversation, string state); public signal void received_marker(Account account, Jid jid, Entities.Message message, Entities.Message.Marked marker); public signal void received_message_received(Account account, Jid jid, Entities.Message message); public signal void received_message_displayed(Account account, Jid jid, Entities.Message message); private StreamInteractor stream_interactor; - private HashMap> chat_states = new HashMap>(Conversation.hash_func, Conversation.equals_func); + private HashMap> typing_since = new HashMap>(Conversation.hash_func, Conversation.equals_func); private HashMap marker_wo_message = new HashMap(); public static void start(StreamInteractor stream_interactor) { @@ -25,14 +25,42 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { private CounterpartInteractionManager(StreamInteractor stream_interactor) { this.stream_interactor = stream_interactor; stream_interactor.account_added.connect(on_account_added); - stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this)); - stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(check_if_got_marker); - stream_interactor.stream_negotiated.connect(() => chat_states.clear() ); + stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect((message, conversation) => clear_chat_state(conversation, message.from)); + stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent_or_received.connect(check_if_got_marker); + stream_interactor.get_module(PresenceManager.IDENTITY).received_offline_presence.connect((jid, account) => { + foreach (Conversation conversation in stream_interactor.get_module(ConversationManager.IDENTITY).get_conversations(jid, account)) { + clear_chat_state(conversation, jid); + } + }); + stream_interactor.stream_negotiated.connect((account) => clear_all_chat_states(account) ); + + Timeout.add_seconds(60, () => { + var one_min_ago = new DateTime.now_utc().add_seconds(-1); + + foreach (Conversation conversation in typing_since.keys) { + ArrayList to_remove = new ArrayList(); + foreach (Jid jid in typing_since[conversation].keys) { + if (typing_since[conversation][jid].compare(one_min_ago) < 0) { + to_remove.add(jid); + } + } + foreach (Jid jid in to_remove) { + clear_chat_state(conversation, jid); + } + } + return true; + }); } - public HashMap? get_chat_states(Conversation conversation) { + public Gee.List? get_typing_jids(Conversation conversation) { if (stream_interactor.connection_manager.get_state(conversation.account) != ConnectionManager.ConnectionState.CONNECTED) return null; - return chat_states[conversation]; + if (!typing_since.contains(conversation) || typing_since[conversation].size == 0) return null; + + var jids = new ArrayList(); + foreach (Jid jid in typing_since[conversation].keys) { + jids.add(jid); + } + return jids; } private void on_account_added(Account account) { @@ -47,6 +75,23 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { }); } + private void clear_chat_state(Conversation conversation, Jid jid) { + if (!(typing_since.contains(conversation) && typing_since[conversation].contains(jid))) return; + + typing_since[conversation].unset(jid); + received_state(conversation, Xmpp.Xep.ChatStateNotifications.STATE_ACTIVE); + } + + private void clear_all_chat_states(Account account) { + foreach (Conversation conversation in typing_since.keys) { + if (conversation.account.equals(account)) { + foreach (Jid jid in typing_since[conversation].keys) { + clear_chat_state(conversation, jid); + } + } + } + } + private async void on_chat_state_received(Account account, Jid jid, string state, MessageStanza stanza) { // Don't show our own (other devices) typing notification if (jid.equals_bare(account.bare_jid)) return; @@ -63,15 +108,15 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { } } - if (!chat_states.has_key(conversation)) { - chat_states[conversation] = new HashMap(Jid.hash_func, Jid.equals_func); + if (!typing_since.has_key(conversation)) { + typing_since[conversation] = new HashMap(Jid.hash_func, Jid.equals_func); } - if (state == Xmpp.Xep.ChatStateNotifications.STATE_ACTIVE) { - chat_states[conversation].unset(jid); + if (state == Xmpp.Xep.ChatStateNotifications.STATE_COMPOSING) { + typing_since[conversation][jid] = new DateTime.now_utc(); + received_state(conversation, state); } else { - chat_states[conversation][jid] = state; + clear_chat_state(conversation, jid); } - received_state(account, jid, state); } private void on_chat_marker_received(Account account, Jid jid, string marker, string stanza_id) { @@ -140,24 +185,6 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object { } } - private class ReceivedMessageListener : MessageListener { - - public string[] after_actions_const = new string[]{ "DEDUPLICATE" }; - public override string action_group { get { return "STORE"; } } - public override string[] after_actions { get { return after_actions_const; } } - - private CounterpartInteractionManager outer; - - public ReceivedMessageListener(CounterpartInteractionManager outer) { - this.outer = outer; - } - - public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { - outer.on_chat_state_received.begin(conversation.account, conversation.counterpart, Xep.ChatStateNotifications.STATE_ACTIVE, stanza); - return false; - } - } - private void on_receipt_received(Account account, Jid jid, string id) { on_chat_marker_received(account, jid, Xep.ChatMarkers.MARKER_RECEIVED, id); } diff --git a/libdino/src/service/message_processor.vala b/libdino/src/service/message_processor.vala index 3959715d..7a9d3348 100644 --- a/libdino/src/service/message_processor.vala +++ b/libdino/src/service/message_processor.vala @@ -15,6 +15,7 @@ public class MessageProcessor : StreamInteractionModule, Object { public signal void build_message_stanza(Entities.Message message, Xmpp.MessageStanza message_stanza, Conversation conversation); public signal void pre_message_send(Entities.Message message, Xmpp.MessageStanza message_stanza, Conversation conversation); public signal void message_sent(Entities.Message message, Conversation conversation); + public signal void message_sent_or_received(Entities.Message message, Conversation conversation); public signal void history_synced(Account account); public MessageListenerHolder received_pipeline = new MessageListenerHolder(); @@ -306,6 +307,8 @@ public class MessageProcessor : StreamInteractionModule, Object { } else if (message.direction == Entities.Message.DIRECTION_SENT) { message_sent(message, conversation); } + + message_sent_or_received(message, conversation); } public async Entities.Message parse_message_stanza(Account account, Xmpp.MessageStanza message) { diff --git a/libdino/src/service/presence_manager.vala b/libdino/src/service/presence_manager.vala index e832687d..f494bb54 100644 --- a/libdino/src/service/presence_manager.vala +++ b/libdino/src/service/presence_manager.vala @@ -9,6 +9,7 @@ public class PresenceManager : StreamInteractionModule, Object { public string id { get { return IDENTITY.id; } } public signal void show_received(Show show, Jid jid, Account account); + public signal void received_offline_presence(Jid jid, Account account); public signal void received_subscription_request(Jid jid, Account account); public signal void received_subscription_approval(Jid jid, Account account); @@ -121,7 +122,7 @@ public class PresenceManager : StreamInteractionModule, Object { } } } - add_show(account, jid, Show.OFFLINE); + received_offline_presence(jid, account); } private void add_show(Account account, Jid jid, string s) { -- cgit v1.2.3-70-g09d2