2018-03-02 12:13:15 +00:00
|
|
|
using Gee;
|
|
|
|
|
|
|
|
using Dino.Entities;
|
|
|
|
using Xmpp;
|
|
|
|
|
|
|
|
namespace Dino {
|
|
|
|
|
|
|
|
public class NotificationEvents : StreamInteractionModule, Object {
|
|
|
|
public static ModuleIdentity<NotificationEvents> IDENTITY = new ModuleIdentity<NotificationEvents>("notification_events");
|
|
|
|
public string id { get { return IDENTITY.id; } }
|
|
|
|
|
2018-11-06 23:17:24 +00:00
|
|
|
public signal void notify_content_item(ContentItem content_item, Conversation conversation);
|
2018-03-02 12:13:15 +00:00
|
|
|
public signal void notify_subscription_request(Conversation conversation);
|
2018-09-15 14:11:05 +00:00
|
|
|
public signal void notify_connection_error(Account account, ConnectionManager.ConnectionError error);
|
2019-08-22 14:05:28 +00:00
|
|
|
public signal void notify_muc_invite(Account account, Jid room_jid, Jid from_jid, string? password, string? reason);
|
2018-03-02 12:13:15 +00:00
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
|
|
|
|
2018-11-06 23:17:24 +00:00
|
|
|
private HashMap<Account, HashMap<Conversation, ContentItem>> mam_potential_new = new HashMap<Account, HashMap<Conversation, ContentItem>>(Account.hash_func, Account.equals_func);
|
2018-09-21 19:52:25 +00:00
|
|
|
private Gee.List<Account> synced_accounts = new ArrayList<Account>(Account.equals_func);
|
2018-02-28 20:02:39 +00:00
|
|
|
|
2018-03-02 12:13:15 +00:00
|
|
|
public static void start(StreamInteractor stream_interactor) {
|
|
|
|
NotificationEvents m = new NotificationEvents(stream_interactor);
|
|
|
|
stream_interactor.add_module(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
public NotificationEvents(StreamInteractor stream_interactor) {
|
|
|
|
this.stream_interactor = stream_interactor;
|
|
|
|
|
2018-11-06 23:17:24 +00:00
|
|
|
stream_interactor.get_module(ContentItemStore.IDENTITY).new_item.connect(on_content_item_received);
|
2018-03-02 12:13:15 +00:00
|
|
|
stream_interactor.get_module(PresenceManager.IDENTITY).received_subscription_request.connect(on_received_subscription_request);
|
2019-08-22 14:05:28 +00:00
|
|
|
stream_interactor.get_module(MucManager.IDENTITY).invite_received.connect((account, room_jid, from_jid, password, reason) => notify_muc_invite(account, room_jid, from_jid, password, reason));
|
|
|
|
stream_interactor.connection_manager.connection_error.connect((account, error) => notify_connection_error(account, error));
|
2018-02-28 20:02:39 +00:00
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).history_synced.connect((account) => {
|
|
|
|
synced_accounts.add(account);
|
|
|
|
if (!mam_potential_new.has_key(account)) return;
|
|
|
|
foreach (Conversation c in mam_potential_new[account].keys) {
|
2018-11-06 23:17:24 +00:00
|
|
|
ContentItem last_mam_item = mam_potential_new[account][c];
|
|
|
|
ContentItem last_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_latest(c);
|
|
|
|
if (last_mam_item == last_item /* && !c.read_up_to.equals(m) */) {
|
|
|
|
on_content_item_received(last_mam_item, c);
|
2018-02-28 20:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mam_potential_new[account].clear();
|
|
|
|
});
|
2018-03-02 12:13:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 23:17:24 +00:00
|
|
|
private void on_content_item_received(ContentItem item, Conversation conversation) {
|
2019-05-21 20:04:49 +00:00
|
|
|
|
|
|
|
// Don't wait for MAM sync on servers without MAM
|
|
|
|
bool mam_available = true;
|
|
|
|
XmppStream? stream = stream_interactor.get_stream(conversation.account);
|
|
|
|
if (stream != null) {
|
|
|
|
mam_available = stream.get_flag(Xep.MessageArchiveManagement.Flag.IDENTITY) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mam_available && !synced_accounts.contains(conversation.account)) {
|
2018-02-28 20:02:39 +00:00
|
|
|
if (!mam_potential_new.has_key(conversation.account)) {
|
2018-11-06 23:17:24 +00:00
|
|
|
mam_potential_new[conversation.account] = new HashMap<Conversation, ContentItem>(Conversation.hash_func, Conversation.equals_func);
|
2018-02-28 20:02:39 +00:00
|
|
|
}
|
2018-11-06 23:17:24 +00:00
|
|
|
mam_potential_new[conversation.account][conversation] = item;
|
2018-02-28 20:02:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-11-06 23:17:24 +00:00
|
|
|
if (!should_notify(item, conversation)) return;
|
2018-03-02 12:13:15 +00:00
|
|
|
if (stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus()) return;
|
2018-11-06 23:17:24 +00:00
|
|
|
notify_content_item(item, conversation);
|
2018-03-02 12:13:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 23:17:24 +00:00
|
|
|
private bool should_notify(ContentItem content_item, Conversation conversation) {
|
2018-03-02 12:13:15 +00:00
|
|
|
Conversation.NotifySetting notify = conversation.get_notification_setting(stream_interactor);
|
2018-11-06 23:17:24 +00:00
|
|
|
switch (content_item.type_) {
|
|
|
|
case MessageItem.TYPE:
|
|
|
|
Message message = (content_item as MessageItem).message;
|
|
|
|
if (message.direction == Message.DIRECTION_SENT) return false;
|
|
|
|
break;
|
|
|
|
case FileItem.TYPE:
|
|
|
|
FileTransfer file_transfer = (content_item as FileItem).file_transfer;
|
2019-05-16 06:49:44 +00:00
|
|
|
// Don't notify on file transfers in a groupchat set to "mention only"
|
|
|
|
if (notify == Conversation.NotifySetting.HIGHLIGHT) return false;
|
2018-11-06 23:17:24 +00:00
|
|
|
if (file_transfer.direction == FileTransfer.DIRECTION_SENT) return false;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-02 12:13:15 +00:00
|
|
|
if (notify == Conversation.NotifySetting.OFF) return false;
|
|
|
|
Jid? nick = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(conversation.counterpart, conversation.account);
|
2018-11-06 23:17:24 +00:00
|
|
|
if (content_item.type_ == MessageItem.TYPE) {
|
|
|
|
Entities.Message message = (content_item as MessageItem).message;
|
|
|
|
if (notify == Conversation.NotifySetting.HIGHLIGHT && nick != null) {
|
|
|
|
return Regex.match_simple("\\b" + Regex.escape_string(nick.resourcepart) + "\\b", message.body, RegexCompileFlags.CASELESS);
|
|
|
|
}
|
2018-03-02 12:13:15 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void on_received_subscription_request(Jid jid, Account account) {
|
|
|
|
Conversation conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(jid, account, Conversation.Type.CHAT);
|
|
|
|
if (stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus(conversation)) return;
|
|
|
|
|
|
|
|
notify_subscription_request(conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|