Only distinguish between typing or not (incoming)

fixes #739
This commit is contained in:
fiaxh 2020-02-20 16:59:34 +01:00
parent ca264c42ad
commit 4c953b5882
5 changed files with 85 additions and 91 deletions

View file

@ -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);

View file

@ -8,13 +8,13 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object {
public static ModuleIdentity<CounterpartInteractionManager> IDENTITY = new ModuleIdentity<CounterpartInteractionManager>("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<Conversation, HashMap<Jid, string>> chat_states = new HashMap<Conversation, HashMap<Jid, string>>(Conversation.hash_func, Conversation.equals_func);
private HashMap<Conversation, HashMap<Jid, DateTime>> typing_since = new HashMap<Conversation, HashMap<Jid, DateTime>>(Conversation.hash_func, Conversation.equals_func);
private HashMap<string, string> marker_wo_message = new HashMap<string, string>();
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<Jid> to_remove = new ArrayList<Jid>();
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<Jid>? 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<Jid>();
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, string>(Jid.hash_func, Jid.equals_func);
if (!typing_since.has_key(conversation)) {
typing_since[conversation] = new HashMap<Jid, DateTime>(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);
}

View file

@ -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) {

View file

@ -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) {

View file

@ -19,14 +19,14 @@ class ChatStatePopulator : Plugins.ConversationItemPopulator, Plugins.Conversati
public ChatStatePopulator(StreamInteractor stream_interactor) {
this.stream_interactor = stream_interactor;
stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).received_state.connect((account, jid, state) => {
if (current_conversation != null && current_conversation.account.equals(account) && current_conversation.counterpart.equals_bare(jid)) {
update_chat_state(account, jid);
stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).received_state.connect((conversation, state) => {
if (current_conversation != null && current_conversation.equals(conversation)) {
update_chat_state();
}
});
stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect((message, conversation) => {
if (conversation.equals(current_conversation)) {
update_chat_state(conversation.account, conversation.counterpart);
update_chat_state();
}
});
}
@ -36,60 +36,32 @@ class ChatStatePopulator : Plugins.ConversationItemPopulator, Plugins.Conversati
this.item_collection = item_collection;
this.meta_item = null;
update_chat_state(conversation.account, conversation.counterpart);
update_chat_state();
}
public void close(Conversation conversation) { }
public void populate_timespan(Conversation conversation, DateTime from, DateTime to) { }
private void update_chat_state(Account account, Jid jid) {
HashMap<Jid, string>? states = stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).get_chat_states(current_conversation);
private void update_chat_state() {
Gee.List<Jid>? typing_jids = stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).get_typing_jids(current_conversation);
StateType? state_type = null;
Gee.List<Jid> jids = new ArrayList<Jid>();
if (states != null) {
Gee.List<Jid> composing = new ArrayList<Jid>();
Gee.List<Jid> paused = new ArrayList<Jid>();
foreach (Jid j in states.keys) {
string state = states[j];
if (state == Xep.ChatStateNotifications.STATE_COMPOSING) {
composing.add(j);
} else if (state == Xep.ChatStateNotifications.STATE_PAUSED) {
paused.add(j);
}
}
if (composing.size == 1 || (composing.size > 1 && current_conversation.type_ != Conversation.Type.GROUPCHAT)) {
state_type = StateType.TYPING;
jids.add(composing[0]);
} else if (paused.size >= 1 && current_conversation.type_ != Conversation.Type.GROUPCHAT) {
state_type = StateType.PAUSED;
jids.add(paused[0]);
} else if (composing.size > 1) {
state_type = StateType.TYPING;
jids = composing;
}
}
if (meta_item != null && state_type == null) {
if (meta_item != null && typing_jids == null) {
// Remove state (stoped typing)
item_collection.remove_item(meta_item);
meta_item = null;
} else if (meta_item != null && state_type != null) {
meta_item.set_new(state_type, jids);
} else if (state_type != null) {
meta_item = new MetaChatStateItem(stream_interactor, current_conversation, jid, state_type, jids);
} else if (meta_item != null && typing_jids != null) {
// Update state (other people typing in MUC)
meta_item.set_new(typing_jids);
} else if (typing_jids != null) {
// New state (started typing)
meta_item = new MetaChatStateItem(stream_interactor, current_conversation, typing_jids);
item_collection.insert_item(meta_item);
}
}
}
private enum StateType {
TYPING,
PAUSED
}
private class MetaChatStateItem : Plugins.MetaConversationItem {
public override Jid? jid { get; set; }
public override bool dim { get; set; default=true; }
public override DateTime sort_time { get; set; default=new DateTime.now_utc().add_years(10); }
@ -99,16 +71,13 @@ private class MetaChatStateItem : Plugins.MetaConversationItem {
private StreamInteractor stream_interactor;
private Conversation conversation;
private StateType state_type;
private Gee.List<Jid> jids = new ArrayList<Jid>();
private Label label;
private AvatarImage image;
public MetaChatStateItem(StreamInteractor stream_interactor, Conversation conversation, Jid jid, StateType state_type, Gee.List<Jid> jids) {
public MetaChatStateItem(StreamInteractor stream_interactor, Conversation conversation, Gee.List<Jid> jids) {
this.stream_interactor = stream_interactor;
this.conversation = conversation;
this.jid = jid;
this.state_type = state_type;
this.jids = jids;
}
@ -125,8 +94,7 @@ private class MetaChatStateItem : Plugins.MetaConversationItem {
return image_content_box;
}
public void set_new(StateType state_type, Gee.List<Jid> jids) {
this.state_type = state_type;
public void set_new(Gee.List<Jid> jids) {
this.jids = jids;
update();
}
@ -144,16 +112,11 @@ private class MetaChatStateItem : Plugins.MetaConversationItem {
if (jids.size > 3) {
new_text = _("%s, %s and %i others").printf(display_names[0], display_names[1], jids.size - 2);
} else if (jids.size == 3) {
new_text = _("%s, %s and %s").printf(display_names[0], display_names[1], display_names[2]);
new_text = _("%s, %s and %s are typing…").printf(display_names[0], display_names[1], display_names[2]);
} else if (jids.size == 2) {
new_text =_("%s and %s").printf(display_names[0], display_names[1]);
new_text =_("%s and %s are typing…").printf(display_names[0], display_names[1]);
} else {
new_text = display_names[0];
}
if (state_type == StateType.TYPING) {
new_text += " " + n("is typing…", "are typing…", jids.size);
} else {
new_text += " " + _("has stopped typing");
new_text = "%s is typing…".printf(display_names[0]);
}
label.label = new_text;