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
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
public signal void received_state(Conversation conversation, 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;
|
2020-02-20 15:59:34 +00:00
|
|
|
private HashMap<Conversation, HashMap<Jid, DateTime>> typing_since = new HashMap<Conversation, HashMap<Jid, DateTime>>(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);
|
2020-02-20 15:59:34 +00:00
|
|
|
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;
|
|
|
|
});
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
public Gee.List<Jid>? get_typing_jids(Conversation conversation) {
|
2018-03-07 16:24:57 +00:00
|
|
|
if (stream_interactor.connection_manager.get_state(conversation.account) != ConnectionManager.ConnectionState.CONNECTED) return null;
|
2020-05-27 22:27:06 +00:00
|
|
|
if (!typing_since.has_key(conversation) || typing_since[conversation].size == 0) return null;
|
2020-02-20 15:59:34 +00:00
|
|
|
|
|
|
|
var jids = new ArrayList<Jid>();
|
|
|
|
foreach (Jid jid in typing_since[conversation].keys) {
|
|
|
|
jids.add(jid);
|
|
|
|
}
|
|
|
|
return jids;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void on_account_added(Account account) {
|
2020-04-28 21:37:01 +00:00
|
|
|
stream_interactor.module_manager.get_module(account, Xep.ChatMarkers.Module.IDENTITY).marker_received.connect( (stream, jid, marker, id, message_stanza) => {
|
2020-05-27 22:27:06 +00:00
|
|
|
on_chat_marker_received.begin(account, jid, marker, id, message_stanza);
|
2017-03-02 14:37:32 +00:00
|
|
|
});
|
2021-04-11 13:24:21 +00:00
|
|
|
stream_interactor.module_manager.get_module(account, Xep.MessageDeliveryReceipts.Module.IDENTITY).receipt_received.connect((stream, jid, id, stanza) => {
|
|
|
|
on_receipt_received(account, jid, id, stanza);
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
private void clear_chat_state(Conversation conversation, Jid jid) {
|
2020-05-27 22:27:06 +00:00
|
|
|
if (!(typing_since.has_key(conversation) && typing_since[conversation].has_key(jid))) return;
|
2020-02-20 15:59:34 +00:00
|
|
|
|
|
|
|
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)) {
|
2020-05-27 22:27:06 +00:00
|
|
|
received_state(conversation, Xmpp.Xep.ChatStateNotifications.STATE_ACTIVE);
|
2020-05-21 20:04:23 +00:00
|
|
|
typing_since[conversation].clear();
|
2020-02-20 15:59:34 +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) {
|
2019-12-23 20:53:45 +00:00
|
|
|
// Don't show our own (other devices) typing notification
|
|
|
|
if (jid.equals_bare(account.bare_jid)) return;
|
|
|
|
|
2020-10-15 17:43:17 +00:00
|
|
|
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).approx_conversation_for_stanza(stanza.from, stanza.to, account, stanza.type_);
|
2018-03-07 16:24:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
if (!typing_since.has_key(conversation)) {
|
|
|
|
typing_since[conversation] = new HashMap<Jid, DateTime>(Jid.hash_func, Jid.equals_func);
|
2018-03-07 16:24:57 +00:00
|
|
|
}
|
2020-02-20 15:59:34 +00:00
|
|
|
if (state == Xmpp.Xep.ChatStateNotifications.STATE_COMPOSING) {
|
|
|
|
typing_since[conversation][jid] = new DateTime.now_utc();
|
|
|
|
received_state(conversation, state);
|
2018-03-07 16:24:57 +00:00
|
|
|
} else {
|
2020-02-20 15:59:34 +00:00
|
|
|
clear_chat_state(conversation, jid);
|
2018-03-07 16:24:57 +00:00
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 21:37:01 +00:00
|
|
|
private async void on_chat_marker_received(Account account, Jid jid, string marker, string stanza_id, MessageStanza message_stanza) {
|
2020-10-15 17:43:17 +00:00
|
|
|
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).approx_conversation_for_stanza(message_stanza.from, message_stanza.to, account, message_stanza.type_);
|
2020-04-28 21:37:01 +00:00
|
|
|
if (conversation == null) return;
|
|
|
|
handle_chat_marker(conversation, jid, marker, stanza_id);
|
|
|
|
}
|
2019-05-11 17:10:36 +00:00
|
|
|
|
2020-04-28 21:37:01 +00:00
|
|
|
private void handle_chat_marker(Conversation conversation, 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)
|
2020-04-28 21:37:01 +00:00
|
|
|
bool own_marker = false;
|
|
|
|
if (conversation.type_ == Conversation.Type.CHAT) {
|
|
|
|
own_marker = conversation.account.bare_jid.to_string() == jid.bare_jid.to_string();
|
|
|
|
} else {
|
|
|
|
Jid? own_muc_jid = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(jid.bare_jid, conversation.account);
|
2019-05-11 17:10:36 +00:00
|
|
|
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;
|
2020-04-28 21:37:01 +00:00
|
|
|
Entities.Message? message = null;
|
|
|
|
if (conversation.type_ == Conversation.Type.GROUPCHAT || conversation.type_ == Conversation.Type.GROUPCHAT_PM) {
|
|
|
|
message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_server_id(stanza_id, conversation);
|
2020-10-15 17:43:17 +00:00
|
|
|
// Outdated clients might use the message id. Or in MUCs that don't send server ids.
|
|
|
|
if (message == null) {
|
|
|
|
message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_stanza_id(stanza_id, conversation);
|
|
|
|
}
|
2020-04-28 21:37:01 +00:00
|
|
|
} else {
|
|
|
|
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;
|
2019-09-14 14:08:08 +00:00
|
|
|
// Don't move read marker backwards because we get old info from another client
|
2020-04-29 19:31:23 +00:00
|
|
|
if (conversation.read_up_to != null && conversation.read_up_to.local_time.compare(message.local_time) > 0) return;
|
2018-02-28 20:02:39 +00:00
|
|
|
conversation.read_up_to = message;
|
2020-04-29 19:31:23 +00:00
|
|
|
|
2022-01-03 17:33:22 +00:00
|
|
|
// TODO: This only marks messages as read, not http file transfers.
|
2022-02-14 13:55:59 +00:00
|
|
|
ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item_by_foreign(conversation, 1, message.id);
|
2022-01-03 17:33:22 +00:00
|
|
|
if (content_item == null) return;
|
2020-04-29 19:31:23 +00:00
|
|
|
ContentItem? read_up_to_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item_by_id(conversation, conversation.read_up_to_item);
|
2020-05-27 22:27:06 +00:00
|
|
|
if (read_up_to_item != null && read_up_to_item.compare(content_item) > 0) return;
|
2020-04-29 19:31:23 +00:00
|
|
|
conversation.read_up_to_item = content_item.id;
|
2018-02-28 20:02:39 +00:00
|
|
|
} else {
|
2020-04-28 21:37:01 +00:00
|
|
|
// We can't currently handle chat markers in MUCs
|
|
|
|
if (conversation.type_ == Conversation.Type.GROUPCHAT) return;
|
2020-01-27 16:32:02 +00:00
|
|
|
|
2020-04-28 21:37:01 +00:00
|
|
|
Entities.Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_stanza_id(stanza_id, conversation);
|
|
|
|
if (message != null) {
|
|
|
|
switch (marker) {
|
|
|
|
case Xep.ChatMarkers.MARKER_RECEIVED:
|
|
|
|
// If we got a received marker, mark the respective message received.
|
|
|
|
received_message_received(conversation.account, jid, message);
|
|
|
|
message.marked = Entities.Message.Marked.RECEIVED;
|
|
|
|
break;
|
|
|
|
case Xep.ChatMarkers.MARKER_DISPLAYED:
|
|
|
|
// If we got a display marker, set all messages up to that message as read (if we know they've been received).
|
|
|
|
received_message_displayed(conversation.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 {
|
|
|
|
// We might get a marker before the actual message (on catchup). Save the marker.
|
|
|
|
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) {
|
2020-04-28 21:37:01 +00:00
|
|
|
return;
|
2017-12-04 17:40:40 +00:00
|
|
|
}
|
2020-04-28 21:37:01 +00:00
|
|
|
marker_wo_message[stanza_id] = marker;
|
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)) {
|
2020-04-28 21:37:01 +00:00
|
|
|
handle_chat_marker(conversation, conversation.counterpart, marker_wo_message[message.stanza_id], message.stanza_id);
|
2017-12-04 17:40:40 +00:00
|
|
|
marker_wo_message.unset(message.stanza_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 13:24:21 +00:00
|
|
|
private void on_receipt_received(Account account, Jid jid, string id, MessageStanza stanza) {
|
|
|
|
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).approx_conversation_for_stanza(stanza.from, stanza.to, account, stanza.type_);
|
2020-04-28 21:37:01 +00:00
|
|
|
if (conversation == null) return;
|
|
|
|
handle_chat_marker(conversation, jid,Xep.ChatMarkers.MARKER_RECEIVED, id);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-04 17:40:40 +00:00
|
|
|
|
2017-11-17 15:06:54 +00:00
|
|
|
}
|