2017-03-02 14:37:32 +00:00
|
|
|
using Gee;
|
|
|
|
|
|
|
|
using Xmpp;
|
|
|
|
using Dino.Entities;
|
|
|
|
|
|
|
|
namespace Dino {
|
|
|
|
public class CounterpartInteractionManager : StreamInteractionModule, Object {
|
2017-03-19 11:55:36 +00:00
|
|
|
public static ModuleIdentity<CounterpartInteractionManager> IDENTITY = new ModuleIdentity<CounterpartInteractionManager>("counterpart_interaction_manager");
|
|
|
|
public string id { get { return IDENTITY.id; } }
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
public signal void received_state(Account account, Jid jid, string state);
|
2018-07-16 19:26:39 +00:00
|
|
|
public signal void received_marker(Account account, Jid jid, Entities.Message message, Entities.Message.Marked marker);
|
2017-03-02 14:37:32 +00:00
|
|
|
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;
|
2018-03-07 16:24:57 +00:00
|
|
|
private HashMap<Conversation, HashMap<Jid, string>> chat_states = new HashMap<Conversation, HashMap<Jid, string>>(Conversation.hash_func, Conversation.equals_func);
|
2017-12-04 17:40:40 +00:00
|
|
|
private HashMap<string, string> marker_wo_message = new HashMap<string, string>();
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
public static void start(StreamInteractor stream_interactor) {
|
|
|
|
CounterpartInteractionManager m = new CounterpartInteractionManager(stream_interactor);
|
|
|
|
stream_interactor.add_module(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
private CounterpartInteractionManager(StreamInteractor stream_interactor) {
|
|
|
|
this.stream_interactor = stream_interactor;
|
|
|
|
stream_interactor.account_added.connect(on_account_added);
|
2018-01-19 21:37:02 +00:00
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this));
|
2017-12-04 17:40:40 +00:00
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(check_if_got_marker);
|
2017-12-17 12:15:53 +00:00
|
|
|
stream_interactor.stream_negotiated.connect(() => chat_states.clear() );
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 16:24:57 +00:00
|
|
|
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];
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void on_account_added(Account account) {
|
2017-03-10 20:45:56 +00:00
|
|
|
stream_interactor.module_manager.get_module(account, Xep.ChatMarkers.Module.IDENTITY).marker_received.connect( (stream, jid, marker, id) => {
|
2018-01-12 20:03:09 +00:00
|
|
|
on_chat_marker_received(account, jid, marker, id);
|
2017-03-02 14:37:32 +00:00
|
|
|
});
|
2017-03-10 20:45:56 +00:00
|
|
|
stream_interactor.module_manager.get_module(account, Xep.MessageDeliveryReceipts.Module.IDENTITY).receipt_received.connect((stream, jid, id) => {
|
2018-01-12 20:03:09 +00:00
|
|
|
on_receipt_received(account, jid, id);
|
2017-03-02 14:37:32 +00:00
|
|
|
});
|
2018-03-07 16:24:57 +00:00
|
|
|
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);
|
2017-03-02 14:37:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-07 16:24:57 +00:00
|
|
|
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;
|
|
|
|
|
2019-05-11 22:09:50 +00:00
|
|
|
// Don't show our own typing notification in MUCs
|
|
|
|
if (conversation.type_ == Conversation.Type.GROUPCHAT) {
|
|
|
|
Jid? own_muc_jid = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(jid.bare_jid, account);
|
|
|
|
if (own_muc_jid != null && own_muc_jid.equals(jid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 16:24:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
received_state(account, jid, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void on_chat_marker_received(Account account, Jid jid, string marker, string stanza_id) {
|
2019-05-11 17:10:36 +00:00
|
|
|
|
|
|
|
// Check if the marker comes from ourselves (own jid or our jid in a MUC)
|
2018-02-28 20:02:39 +00:00
|
|
|
bool own_marker = account.bare_jid.to_string() == jid.bare_jid.to_string();
|
2019-05-11 17:10:36 +00:00
|
|
|
if (stream_interactor.get_module(MucManager.IDENTITY).is_groupchat_occupant(jid, account)) {
|
|
|
|
Jid? own_muc_jid = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(jid.bare_jid, account);
|
|
|
|
if (own_muc_jid != null && own_muc_jid.equals(jid)) {
|
|
|
|
own_marker = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 20:02:39 +00:00
|
|
|
if (own_marker) {
|
2019-05-11 17:10:36 +00:00
|
|
|
// If we received a display marker from ourselves (other device), set the conversation read up to that message.
|
2018-03-22 13:37:25 +00:00
|
|
|
if (marker != Xep.ChatMarkers.MARKER_DISPLAYED && marker != Xep.ChatMarkers.MARKER_ACKNOWLEDGED) return;
|
2018-02-28 20:02:39 +00:00
|
|
|
Conversation? conversation = stream_interactor.get_module(MessageStorage.IDENTITY).get_conversation_for_stanza_id(account, stanza_id);
|
|
|
|
if (conversation == null) return;
|
2018-07-16 19:26:39 +00:00
|
|
|
Entities.Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_stanza_id(stanza_id, conversation);
|
2018-02-28 20:02:39 +00:00
|
|
|
if (message == null) return;
|
|
|
|
conversation.read_up_to = message;
|
|
|
|
} else {
|
2019-05-11 17:10:36 +00:00
|
|
|
// We received a marker from someone else. Search the respective message and mark it.
|
2018-02-28 20:02:39 +00:00
|
|
|
foreach (Conversation conversation in stream_interactor.get_module(ConversationManager.IDENTITY).get_conversations(jid, account)) {
|
2018-07-16 19:26:39 +00:00
|
|
|
Entities.Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_stanza_id(stanza_id, conversation);
|
2018-02-28 20:02:39 +00:00
|
|
|
if (message != null) {
|
|
|
|
switch (marker) {
|
|
|
|
case Xep.ChatMarkers.MARKER_RECEIVED:
|
2019-05-11 17:10:36 +00:00
|
|
|
// If we got a received marker, mark the respective message received.
|
2018-02-28 20:02:39 +00:00
|
|
|
received_message_received(account, jid, message);
|
|
|
|
message.marked = Entities.Message.Marked.RECEIVED;
|
|
|
|
break;
|
|
|
|
case Xep.ChatMarkers.MARKER_DISPLAYED:
|
2019-05-11 17:10:36 +00:00
|
|
|
// If we got a display marker, set all messages up to that message as read (if we know they've been received).
|
2018-02-28 20:02:39 +00:00
|
|
|
received_message_displayed(account, jid, message);
|
|
|
|
Gee.List<Entities.Message> messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages(conversation);
|
|
|
|
foreach (Entities.Message m in messages) {
|
|
|
|
if (m.equals(message)) break;
|
|
|
|
if (m.marked == Entities.Message.Marked.RECEIVED) m.marked = Entities.Message.Marked.READ;
|
|
|
|
}
|
|
|
|
message.marked = Entities.Message.Marked.READ;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-11 17:10:36 +00:00
|
|
|
// We might get a marker before the actual message (on catchup). Save the marker.
|
2018-02-28 20:02:39 +00:00
|
|
|
if (marker_wo_message.has_key(stanza_id) &&
|
2017-12-04 17:40:40 +00:00
|
|
|
marker_wo_message[stanza_id] == Xep.ChatMarkers.MARKER_DISPLAYED && marker == Xep.ChatMarkers.MARKER_RECEIVED) {
|
2018-02-28 20:02:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
marker_wo_message[stanza_id] = marker;
|
2017-12-04 17:40:40 +00:00
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 17:40:40 +00:00
|
|
|
private void check_if_got_marker(Entities.Message message, Conversation conversation) {
|
|
|
|
if (marker_wo_message.has_key(message.stanza_id)) {
|
|
|
|
on_chat_marker_received(conversation.account, conversation.counterpart, marker_wo_message[message.stanza_id], message.stanza_id);
|
|
|
|
marker_wo_message.unset(message.stanza_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
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) {
|
2018-03-07 16:24:57 +00:00
|
|
|
outer.on_chat_state_received.begin(conversation.account, conversation.counterpart, Xep.ChatStateNotifications.STATE_ACTIVE, stanza);
|
2018-01-19 21:37:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void on_receipt_received(Account account, Jid jid, string id) {
|
|
|
|
on_chat_marker_received(account, jid, Xep.ChatMarkers.MARKER_RECEIVED, id);
|
|
|
|
}
|
|
|
|
}
|
2017-12-04 17:40:40 +00:00
|
|
|
|
2017-11-17 15:06:54 +00:00
|
|
|
}
|