2017-03-02 14:37:32 +00:00
|
|
|
using Gee;
|
|
|
|
|
|
|
|
using Xmpp;
|
|
|
|
using Dino.Entities;
|
|
|
|
|
|
|
|
namespace Dino {
|
|
|
|
public class ConversationManager : StreamInteractionModule, Object {
|
2017-03-19 11:55:36 +00:00
|
|
|
public static ModuleIdentity<ConversationManager> IDENTITY = new ModuleIdentity<ConversationManager>("conversation_manager");
|
|
|
|
public string id { get { return IDENTITY.id; } }
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
public signal void conversation_activated(Conversation conversation);
|
2017-03-20 21:12:20 +00:00
|
|
|
public signal void conversation_deactivated(Conversation conversation);
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
|
|
|
private Database db;
|
|
|
|
|
|
|
|
private HashMap<Account, HashMap<Jid, Conversation>> conversations = new HashMap<Account, HashMap<Jid, Conversation>>(Account.hash_func, Account.equals_func);
|
|
|
|
|
|
|
|
public static void start(StreamInteractor stream_interactor, Database db) {
|
|
|
|
ConversationManager m = new ConversationManager(stream_interactor, db);
|
|
|
|
stream_interactor.add_module(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ConversationManager(StreamInteractor stream_interactor, Database db) {
|
|
|
|
this.db = db;
|
|
|
|
this.stream_interactor = stream_interactor;
|
|
|
|
stream_interactor.add_module(this);
|
|
|
|
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 MessageListener(stream_interactor));
|
2017-12-04 17:40:40 +00:00
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(handle_new_message);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 23:17:01 +00:00
|
|
|
public Conversation create_conversation(Jid jid, Account account, Conversation.Type? type = null) {
|
2017-03-24 21:57:05 +00:00
|
|
|
assert(conversations.has_key(account));
|
2018-01-12 20:03:09 +00:00
|
|
|
Jid store_jid = type == Conversation.Type.GROUPCHAT ? jid.bare_jid : jid;
|
|
|
|
if (conversations[account].has_key(store_jid)) {
|
|
|
|
conversations[account][store_jid].type_ = type;
|
|
|
|
return conversations[account][store_jid];
|
2017-03-24 21:57:05 +00:00
|
|
|
} else {
|
|
|
|
Conversation conversation = new Conversation(jid, account, type);
|
|
|
|
add_conversation(conversation);
|
|
|
|
conversation.persist(db);
|
|
|
|
return conversation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 23:17:01 +00:00
|
|
|
public Conversation? get_conversation_for_message(Entities.Message message) {
|
|
|
|
if (message.type_ == Entities.Message.Type.CHAT) {
|
|
|
|
return create_conversation(message.counterpart.bare_jid, message.account, Conversation.Type.CHAT);
|
|
|
|
} else if (message.type_ == Entities.Message.Type.GROUPCHAT) {
|
|
|
|
return create_conversation(message.counterpart.bare_jid, message.account, Conversation.Type.GROUPCHAT);
|
|
|
|
} else if (message.type_ == Entities.Message.Type.GROUPCHAT_PM) {
|
|
|
|
return create_conversation(message.counterpart, message.account, Conversation.Type.GROUPCHAT_PM);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Gee.List<Conversation> get_conversations_for_presence(Show show, Account account) {
|
2017-04-04 13:47:00 +00:00
|
|
|
return get_conversations(show.jid, account);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Gee.List<Conversation> get_conversations(Jid jid, Account account) {
|
2017-03-30 23:17:01 +00:00
|
|
|
Gee.List<Conversation> ret = new ArrayList<Conversation>(Conversation.equals_func);
|
2017-04-04 13:47:00 +00:00
|
|
|
Conversation? bare_conversation = get_conversation(jid, account);
|
2017-03-30 23:17:01 +00:00
|
|
|
if (bare_conversation != null) ret.add(bare_conversation);
|
2017-04-04 13:47:00 +00:00
|
|
|
Conversation? full_conversation = get_conversation(jid.bare_jid, account);
|
2017-03-30 23:17:01 +00:00
|
|
|
if (full_conversation != null) ret.add(full_conversation);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
public Conversation? get_conversation(Jid jid, Account account) {
|
|
|
|
if (conversations.has_key(account)) {
|
|
|
|
return conversations[account][jid];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-19 20:41:33 +00:00
|
|
|
public Conversation? get_conversation_by_id(int id) {
|
|
|
|
foreach (HashMap<Jid, Conversation> hm in conversations.values) {
|
|
|
|
foreach (Conversation conversation in hm.values) {
|
|
|
|
if (conversation.id == id) return conversation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
public Gee.List<Conversation> get_active_conversations(Account? account = null) {
|
2017-04-23 11:50:32 +00:00
|
|
|
Gee.List<Conversation> ret = new ArrayList<Conversation>(Conversation.equals_func);
|
2018-01-12 20:03:09 +00:00
|
|
|
foreach (Account account_ in conversations.keys) {
|
|
|
|
if (account != null && !account_.equals(account)) continue;
|
|
|
|
foreach (Conversation conversation in conversations[account_].values) {
|
2017-03-20 21:12:20 +00:00
|
|
|
if(conversation.active) ret.add(conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-18 20:38:16 +00:00
|
|
|
public void start_conversation(Conversation conversation) {
|
|
|
|
if (conversation.last_active == null) {
|
2017-08-31 16:40:58 +00:00
|
|
|
conversation.last_active = new DateTime.now_utc();
|
2017-03-24 21:57:05 +00:00
|
|
|
if (conversation.active) conversation_activated(conversation);
|
|
|
|
}
|
|
|
|
if (!conversation.active) {
|
|
|
|
conversation.active = true;
|
|
|
|
conversation_activated(conversation);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-03-20 21:12:20 +00:00
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-20 21:12:20 +00:00
|
|
|
public void close_conversation(Conversation conversation) {
|
|
|
|
conversation.active = false;
|
|
|
|
conversation_deactivated(conversation);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void on_account_added(Account account) {
|
2017-03-30 23:17:01 +00:00
|
|
|
conversations[account] = new HashMap<Jid, Conversation>(Jid.hash_func, Jid.equals_func);
|
2017-03-02 14:37:32 +00:00
|
|
|
foreach (Conversation conversation in db.get_conversations(account)) {
|
|
|
|
add_conversation(conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
private class MessageListener : Dino.MessageListener {
|
|
|
|
|
2019-02-09 12:58:16 +00:00
|
|
|
public string[] after_actions_const = new string[]{ "DEDUPLICATE", "FILTER_EMPTY" };
|
2018-01-19 21:37:02 +00:00
|
|
|
public override string action_group { get { return "MANAGER"; } }
|
|
|
|
public override string[] after_actions { get { return after_actions_const; } }
|
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
|
|
|
|
|
|
|
public MessageListener(StreamInteractor stream_interactor) {
|
|
|
|
this.stream_interactor = stream_interactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) {
|
|
|
|
conversation.last_active = message.time;
|
|
|
|
|
|
|
|
if (message.stanza != null) {
|
|
|
|
bool is_mam_message = Xep.MessageArchiveManagement.MessageFlag.get_flag(message.stanza) != null;
|
|
|
|
bool is_recent = message.local_time.compare(new DateTime.now_utc().add_hours(-24)) > 0;
|
|
|
|
if (is_mam_message && !is_recent) return false;
|
|
|
|
}
|
|
|
|
stream_interactor.get_module(ConversationManager.IDENTITY).start_conversation(conversation);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 17:40:40 +00:00
|
|
|
private void handle_new_message(Entities.Message message, Conversation conversation) {
|
2017-03-24 21:57:05 +00:00
|
|
|
conversation.last_active = message.time;
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-12-04 17:40:40 +00:00
|
|
|
if (message.stanza != null) {
|
|
|
|
bool is_mam_message = Xep.MessageArchiveManagement.MessageFlag.get_flag(message.stanza) != null;
|
|
|
|
bool is_recent = message.local_time.compare(new DateTime.now_utc().add_hours(-24)) > 0;
|
|
|
|
if (is_mam_message && !is_recent) return;
|
|
|
|
}
|
|
|
|
start_conversation(conversation);
|
2017-03-09 14:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
private void add_conversation(Conversation conversation) {
|
|
|
|
conversations[conversation.account][conversation.counterpart] = conversation;
|
|
|
|
if (conversation.active) {
|
|
|
|
conversation_activated(conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:46:40 +00:00
|
|
|
}
|