Handle multiple chat state notifications per bare jid

fixes #117
This commit is contained in:
fiaxh 2018-03-07 17:24:57 +01:00
parent 0968da1ff7
commit 81310dff2e
6 changed files with 119 additions and 51 deletions

View file

@ -14,7 +14,7 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object {
public signal void received_message_displayed(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 StreamInteractor stream_interactor;
private HashMap<Jid, string> chat_states = new HashMap<Jid, string>(Jid.hash_bare_func, Jid.equals_bare_func); private HashMap<Conversation, HashMap<Jid, string>> chat_states = new HashMap<Conversation, HashMap<Jid, string>>(Conversation.hash_func, Conversation.equals_func);
private HashMap<string, string> marker_wo_message = new HashMap<string, string>(); private HashMap<string, string> marker_wo_message = new HashMap<string, string>();
public static void start(StreamInteractor stream_interactor) { public static void start(StreamInteractor stream_interactor) {
@ -30,9 +30,9 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object {
stream_interactor.stream_negotiated.connect(() => chat_states.clear() ); stream_interactor.stream_negotiated.connect(() => chat_states.clear() );
} }
public string? get_chat_state(Account account, Jid jid) { public HashMap? get_chat_states(Conversation conversation) {
if (stream_interactor.connection_manager.get_state(account) != ConnectionManager.ConnectionState.CONNECTED) return null; if (stream_interactor.connection_manager.get_state(conversation.account) != ConnectionManager.ConnectionState.CONNECTED) return null;
return chat_states[jid]; return chat_states[conversation];
} }
private void on_account_added(Account account) { 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) => { stream_interactor.module_manager.get_module(account, Xep.MessageDeliveryReceipts.Module.IDENTITY).receipt_received.connect((stream, jid, id) => {
on_receipt_received(account, 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) => { stream_interactor.module_manager.get_module(account, Xep.ChatStateNotifications.Module.IDENTITY).chat_state_received.connect((stream, jid, state, stanza) => {
on_chat_state_received(account, jid, state); on_chat_state_received.begin(account, jid, state, stanza);
}); });
} }
private void on_chat_state_received(Account account, Jid jid, string state) { private async void on_chat_state_received(Account account, Jid jid, string state, MessageStanza stanza) {
chat_states[jid] = state; 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, string>(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); 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) { 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; return false;
} }
} }

View file

@ -76,7 +76,7 @@ public class MessageProcessor : StreamInteractionModule, Object {
private async void on_message_received(Account account, Xmpp.MessageStanza message_stanza) { private async void on_message_received(Account account, Xmpp.MessageStanza message_stanza) {
if (message_stanza.body == null) return; 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); Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_for_message(message);
if (conversation != null) { 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); Entities.Message new_message = new Entities.Message(message.body);
new_message.account = account; new_message.account = account;
new_message.stanza_id = message.id; new_message.stanza_id = message.id;

View file

@ -184,7 +184,7 @@ public class AvatarImage : Misc {
jid = jid_; jid = jid_;
if (occupants == null || occupants.size == 0) { if (occupants == null || occupants.size == 0) {
if (force_update || current_jids.length != 1 || !current_jids[0].equals(jid_) || gray != (allow_gray && (occupants == null || !is_self_online()))) { if (force_update || current_jids.length != 1 || !current_jids[0].equals(jid_) || gray != (allow_gray && (occupants == null || !is_self_online()))) {
set_jids(new Jid[] {jid_}, false, occupants == null || !is_self_online()); set_jids_(new Jid[] {jid_}, false, occupants == null || !is_self_online());
} }
} else if (occupants.size > 4) { } else if (occupants.size > 4) {
bool requires_update = force_update; bool requires_update = force_update;
@ -195,7 +195,7 @@ public class AvatarImage : Misc {
} }
} }
if (requires_update) { if (requires_update) {
set_jids(occupants.slice(0, 3).to_array(), true); set_jids_(occupants.slice(0, 3).to_array(), true);
} }
} else { // 1 <= occupants.size <= 4 } else { // 1 <= occupants.size <= 4
bool requires_update = force_update; bool requires_update = force_update;
@ -207,18 +207,24 @@ public class AvatarImage : Misc {
} }
} }
if (requires_update) { if (requires_update) {
set_jids(occupants.to_array(), false); set_jids_(occupants.to_array(), false);
} }
} }
} else { } else {
// Single user // Single user
this.jid = jid_; this.jid = jid_;
if (force_update || current_jids.length != 1 || !current_jids[0].equals(jid) || gray != (allow_gray && (!is_counterpart_online(jid) || !is_self_online()))) { if (force_update || current_jids.length != 1 || !current_jids[0].equals(jid) || gray != (allow_gray && (!is_counterpart_online(jid) || !is_self_online()))) {
set_jids(new Jid[] { jid }, false, !is_counterpart_online(jid) || !is_self_online()); set_jids_(new Jid[] { jid }, false, !is_counterpart_online(jid) || !is_self_online());
} }
} }
} }
public void set_jids(StreamInteractor stream_interactor, Jid[] jids, Account account, bool gray = false) {
this.stream_interactor = stream_interactor;
this.account = account;
set_jids_(jids.length > 3 ? jids[0:3] : jids, jids.length > 3, gray);
}
private void on_show_received(Show show, Jid jid, Account account) { private void on_show_received(Show show, Jid jid, Account account) {
if (!account.equals(this.account)) return; if (!account.equals(this.account)) return;
if (jid.equals_bare(this.jid)) { if (jid.equals_bare(this.jid)) {
@ -266,7 +272,7 @@ public class AvatarImage : Misc {
return stream_interactor.get_module(PresenceManager.IDENTITY).get_full_jids(counterpart, account) != null; return stream_interactor.get_module(PresenceManager.IDENTITY).get_full_jids(counterpart, account) != null;
} }
public void set_jids(Jid[] jids, bool with_plus = false, bool gray = false) { public void set_jids_(Jid[] jids, bool with_plus = false, bool gray = false) {
assert(jids.length > 0); assert(jids.length > 0);
assert(jids.length < 5); assert(jids.length < 5);
assert(!with_plus || jids.length == 3); assert(!with_plus || jids.length == 3);

View file

@ -21,7 +21,7 @@ class ChatStatePopulator : Plugins.ConversationItemPopulator, Object {
stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).received_state.connect((account, jid, state) => { 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)) { if (current_conversation != null && current_conversation.account.equals(account) && current_conversation.counterpart.equals_bare(jid)) {
update_chat_state(account, jid, state); update_chat_state(account, jid);
} }
}); });
stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect((message, conversation) => { stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect((message, conversation) => {
@ -45,70 +45,121 @@ class ChatStatePopulator : Plugins.ConversationItemPopulator, Object {
public void populate_between_widgets(Conversation conversation, DateTime from, DateTime to) { } public void populate_between_widgets(Conversation conversation, DateTime from, DateTime to) { }
private void update_chat_state(Account account, Jid jid, string? state = null) { private void update_chat_state(Account account, Jid jid) {
string? state_ = state; HashMap<Jid, string>? states = stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).get_chat_states(current_conversation);
if (state_ == null) {
state_ = stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).get_chat_state(current_conversation.account, current_conversation.counterpart); StateType? state_type = null;
} Gee.List<Jid> jids = new ArrayList<Jid>();
string? new_text = null;
if (state_ != null) { if (states != null) {
if (state_ == Xep.ChatStateNotifications.STATE_COMPOSING || state_ == Xep.ChatStateNotifications.STATE_PAUSED) { Gee.List<Jid> composing = new ArrayList<Jid>();
if (state_ == Xep.ChatStateNotifications.STATE_COMPOSING) { Gee.List<Jid> paused = new ArrayList<Jid>();
new_text = _("is typing…"); foreach (Jid j in states.keys) {
} else if (state_ == Xep.ChatStateNotifications.STATE_PAUSED) { string state = states[j];
new_text = _("has stopped typing"); 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 && new_text == null) { if (meta_item != null && state_type == null) {
item_collection.remove_item(meta_item); item_collection.remove_item(meta_item);
meta_item = null; meta_item = null;
} else if (meta_item != null && new_text != null) { } else if (meta_item != null && state_type != null) {
meta_item.set_text(new_text); meta_item.set_new(state_type, jids);
} else if (new_text != null) { } else if (state_type != null) {
meta_item = new MetaChatStateItem(stream_interactor, current_conversation, jid, new_text); meta_item = new MetaChatStateItem(stream_interactor, current_conversation, jid, state_type, jids);
item_collection.insert_item(meta_item); item_collection.insert_item(meta_item);
} }
} }
} }
public class MetaChatStateItem : Plugins.MetaConversationItem { private enum StateType {
TYPING,
PAUSED
}
private class MetaChatStateItem : Plugins.MetaConversationItem {
public override Jid? jid { get; set; } public override Jid? jid { get; set; }
public override bool dim { get; set; default=true; } public override bool dim { get; set; default=true; }
public override DateTime? sort_time { get; set; default=new DateTime.now_utc().add_years(10); } public override DateTime? sort_time { get; set; default=new DateTime.now_utc().add_years(10); }
public override bool can_merge { get; set; default=false; } public override bool can_merge { get; set; default=false; }
public override bool requires_avatar { get; set; default=true; } public override bool requires_avatar { get; set; default=false; }
public override bool requires_header { get; set; default=false; } public override bool requires_header { get; set; default=false; }
private StreamInteractor stream_interactor; private StreamInteractor stream_interactor;
private Conversation conversation; private Conversation conversation;
private string text; private StateType state_type;
private Gee.List<Jid> jids = new ArrayList<Jid>();
private Label label; private Label label;
private AvatarImage image;
public MetaChatStateItem(StreamInteractor stream_interactor, Conversation conversation, Jid jid, string text) { public MetaChatStateItem(StreamInteractor stream_interactor, Conversation conversation, Jid jid, StateType state_type, Gee.List<Jid> jids) {
this.stream_interactor = stream_interactor; this.stream_interactor = stream_interactor;
this.conversation = conversation; this.conversation = conversation;
this.jid = jid; this.jid = jid;
this.text = text; this.state_type = state_type;
this.jids = jids;
} }
public override Object? get_widget(Plugins.WidgetType widget_type) { public override Object? get_widget(Plugins.WidgetType widget_type) {
label = new Label("") { xalign=0, vexpand=true, visible=true }; label = new Label("") { xalign=0, vexpand=true, visible=true };
label.get_style_context().add_class("dim-label"); label.get_style_context().add_class("dim-label");
update_text(); image = new AvatarImage() { margin_top=2, valign=Align.START, visible=true };
return label;
Box image_content_box = new Box(Orientation.HORIZONTAL, 8) { visible=true };
image_content_box.add(image);
image_content_box.add(label);
update();
return image_content_box;
} }
public void set_text(string text) { public void set_new(StateType state_type, Gee.List<Jid> jids) {
this.text = text; this.state_type = state_type;
update_text(); this.jids = jids;
update();
} }
private void update_text() { private void update() {
string display_name = Util.get_display_name(stream_interactor, jid, conversation.account); if (image == null || label == null) return;
label.label = display_name + " " + text;
image.set_jids(stream_interactor, jids.to_array(), conversation.account, true);
Gee.List<string> display_names = new ArrayList<string>();
foreach (Jid jid in jids) {
display_names.add(Util.get_display_name(stream_interactor, jid, conversation.account));
}
string new_text = "";
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]);
} else if (jids.size == 2) {
new_text =_("%s and %s").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");
}
label.label = new_text;
} }
} }

View file

@ -22,9 +22,11 @@ public class ConversationItemSkeleton : Box {
public ConversationItemSkeleton(StreamInteractor stream_interactor, Conversation conversation, Plugins.MetaConversationItem item) { public ConversationItemSkeleton(StreamInteractor stream_interactor, Conversation conversation, Plugins.MetaConversationItem item) {
this.conversation = conversation; this.conversation = conversation;
this.stream_interactor = stream_interactor; this.stream_interactor = stream_interactor;
Box image_content_box = new Box(Orientation.HORIZONTAL, 8) { visible=true };
if (item.requires_avatar) { if (item.requires_avatar) {
image.set_jid(stream_interactor, item.jid, conversation.account); image.set_jid(stream_interactor, item.jid, conversation.account);
image_content_box.add(image);
} }
if (item.display_time != null) { if (item.display_time != null) {
default_header = new DefaultSkeletonHeader(stream_interactor, conversation, item) { visible=true }; default_header = new DefaultSkeletonHeader(stream_interactor, conversation, item) { visible=true };
@ -36,8 +38,6 @@ public class ConversationItemSkeleton : Box {
} }
add_meta_item(item); add_meta_item(item);
Box image_content_box = new Box(Orientation.HORIZONTAL, 8) { visible=true };
image_content_box.add(image);
image_content_box.add(grid); image_content_box.add(grid);
this.add(image_content_box); this.add(image_content_box);
} }

View file

@ -14,7 +14,7 @@ private const string[] STATES = { STATE_ACTIVE, STATE_INACTIVE, STATE_GONE, STAT
public class Module : XmppStreamModule { public class Module : XmppStreamModule {
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0085_chat_state_notifications"); public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0085_chat_state_notifications");
public signal void chat_state_received(XmppStream stream, Jid jid, string state); public signal void chat_state_received(XmppStream stream, Jid jid, string state, MessageStanza stanza);
private SendPipelineListener send_pipeline_listener = new SendPipelineListener(); private SendPipelineListener send_pipeline_listener = new SendPipelineListener();
@ -49,7 +49,7 @@ public class Module : XmppStreamModule {
foreach (StanzaNode node in nodes) { foreach (StanzaNode node in nodes) {
if (node.ns_uri == NS_URI && if (node.ns_uri == NS_URI &&
node.name in STATES) { node.name in STATES) {
chat_state_received(stream, message.from, node.name); chat_state_received(stream, message.from, node.name, message);
} }
} }
} }