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
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
2020-11-20 14:21:34 +00:00
|
|
|
private NotificationProvider? notifier;
|
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);
|
2021-03-19 22:07:40 +00:00
|
|
|
|
2020-11-20 14:21:34 +00:00
|
|
|
stream_interactor.get_module(MucManager.IDENTITY).invite_received.connect(on_invite_received);
|
|
|
|
stream_interactor.get_module(MucManager.IDENTITY).voice_request_received.connect((account, room_jid, from_jid, nick) => {
|
|
|
|
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(room_jid, account, Conversation.Type.GROUPCHAT);
|
|
|
|
if (conversation == null) return;
|
|
|
|
notifier.notify_voice_request.begin(conversation, from_jid);
|
|
|
|
});
|
2021-03-19 22:07:40 +00:00
|
|
|
|
|
|
|
stream_interactor.get_module(Calls.IDENTITY).call_incoming.connect(on_call_incoming);
|
2020-11-20 14:21:34 +00:00
|
|
|
stream_interactor.connection_manager.connection_error.connect((account, error) => notifier.notify_connection_error.begin(account, error));
|
|
|
|
stream_interactor.get_module(ChatInteraction.IDENTITY).focused_in.connect((conversation) => {
|
|
|
|
notifier.retract_content_item_notifications.begin();
|
|
|
|
notifier.retract_conversation_notifications.begin(conversation);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void register_notification_provider(NotificationProvider notification_provider) {
|
|
|
|
if (notifier == null || notifier.get_priority() < notification_provider.get_priority()) {
|
|
|
|
notifier = notification_provider;
|
|
|
|
}
|
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-12-18 17:53:14 +00:00
|
|
|
ContentItem last_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_latest(conversation);
|
2019-05-21 20:04:49 +00:00
|
|
|
|
2020-11-20 14:21:34 +00:00
|
|
|
if (item.id != last_item.id) return;
|
|
|
|
if (item.id == conversation.read_up_to_item) return;
|
2018-03-02 12:13:15 +00:00
|
|
|
if (stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus()) return;
|
|
|
|
|
|
|
|
Conversation.NotifySetting notify = conversation.get_notification_setting(stream_interactor);
|
2020-11-20 14:21:34 +00:00
|
|
|
if (notify == Conversation.NotifySetting.OFF) return;
|
2020-04-14 14:28:36 +00:00
|
|
|
|
2020-11-20 14:21:34 +00:00
|
|
|
string conversation_display_name = get_conversation_display_name(stream_interactor, conversation, null);
|
|
|
|
string? participant_display_name = null;
|
|
|
|
if (conversation.type_ == Conversation.Type.GROUPCHAT) {
|
|
|
|
participant_display_name = get_participant_display_name(stream_interactor, conversation, item.jid);
|
|
|
|
}
|
2020-04-14 14:28:36 +00:00
|
|
|
|
2020-11-20 14:21:34 +00:00
|
|
|
switch (item.type_) {
|
2018-11-06 23:17:24 +00:00
|
|
|
case MessageItem.TYPE:
|
2020-11-20 14:21:34 +00:00
|
|
|
Message message = ((MessageItem) item).message;
|
|
|
|
|
|
|
|
if (message.direction == Message.DIRECTION_SENT) return;
|
|
|
|
|
|
|
|
if (notify == Conversation.NotifySetting.HIGHLIGHT) {
|
|
|
|
Jid? nick = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(conversation.counterpart, conversation.account);
|
|
|
|
if (nick == null) return;
|
|
|
|
|
|
|
|
bool highlight = Regex.match_simple("\\b" + Regex.escape_string(nick.resourcepart) + "\\b", message.body, RegexCompileFlags.CASELESS);
|
|
|
|
if (!highlight) return;
|
|
|
|
}
|
|
|
|
|
2020-12-30 19:22:14 +00:00
|
|
|
notify_content_item(item, conversation);
|
|
|
|
if (notify != Conversation.NotifySetting.OFF) {
|
|
|
|
notifier.notify_message.begin(message, conversation, conversation_display_name, participant_display_name);
|
|
|
|
}
|
2018-11-06 23:17:24 +00:00
|
|
|
break;
|
|
|
|
case FileItem.TYPE:
|
2020-11-20 14:21:34 +00:00
|
|
|
FileTransfer file_transfer = ((FileItem) item).file_transfer;
|
|
|
|
bool is_image = file_transfer.mime_type != null && file_transfer.mime_type.has_prefix("image");
|
2020-04-14 14:28:36 +00:00
|
|
|
|
2020-11-20 14:21:34 +00:00
|
|
|
// Don't notify on file transfers in a groupchat set to "mention only"
|
|
|
|
if (notify == Conversation.NotifySetting.HIGHLIGHT) return;
|
|
|
|
if (file_transfer.direction == FileTransfer.DIRECTION_SENT) return;
|
2020-04-14 14:28:36 +00:00
|
|
|
|
2020-12-30 19:22:14 +00:00
|
|
|
notify_content_item(item, conversation);
|
|
|
|
if (notify != Conversation.NotifySetting.OFF) {
|
|
|
|
notifier.notify_file.begin(file_transfer, conversation, is_image, conversation_display_name, participant_display_name);
|
|
|
|
}
|
2020-11-20 14:21:34 +00:00
|
|
|
break;
|
2021-03-19 22:07:40 +00:00
|
|
|
case CallItem.TYPE:
|
|
|
|
// handled in `on_call_incoming`
|
|
|
|
break;
|
2018-03-02 12:13:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-11-20 14:21:34 +00:00
|
|
|
notifier.notify_subscription_request.begin(conversation);
|
2018-03-02 12:13:15 +00:00
|
|
|
}
|
2020-11-20 14:21:34 +00:00
|
|
|
|
2021-03-19 22:07:40 +00:00
|
|
|
private void on_call_incoming(Call call, Conversation conversation, bool video) {
|
|
|
|
string conversation_display_name = get_conversation_display_name(stream_interactor, conversation, null);
|
|
|
|
|
|
|
|
notifier.notify_call.begin(call, conversation, video, conversation_display_name);
|
|
|
|
call.notify["state"].connect(() => {
|
|
|
|
if (call.state != Call.State.RINGING) {
|
2021-04-09 21:59:03 +00:00
|
|
|
notifier.retract_call_notification.begin(call, conversation);
|
2021-03-19 22:07:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:21:34 +00:00
|
|
|
private void on_invite_received(Account account, Jid room_jid, Jid from_jid, string? password, string? reason) {
|
|
|
|
string inviter_display_name;
|
|
|
|
if (room_jid.equals_bare(from_jid)) {
|
|
|
|
Conversation conversation = new Conversation(room_jid, account, Conversation.Type.GROUPCHAT);
|
|
|
|
inviter_display_name = get_participant_display_name(stream_interactor, conversation, from_jid);
|
|
|
|
} else {
|
|
|
|
Conversation direct_conversation = new Conversation(from_jid, account, Conversation.Type.CHAT);
|
|
|
|
inviter_display_name = get_participant_display_name(stream_interactor, direct_conversation, from_jid);
|
|
|
|
}
|
|
|
|
notifier.notify_muc_invite.begin(account, room_jid, from_jid, inviter_display_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface NotificationProvider : Object {
|
|
|
|
public abstract double get_priority();
|
|
|
|
|
|
|
|
public abstract async void notify_message(Message message, Conversation conversation, string conversation_display_name, string? participant_display_name);
|
|
|
|
public abstract async void notify_file(FileTransfer file_transfer, Conversation conversation, bool is_image, string conversation_display_name, string? participant_display_name);
|
2021-03-19 22:07:40 +00:00
|
|
|
public abstract async void notify_call(Call call, Conversation conversation, bool video, string conversation_display_name);
|
|
|
|
public abstract async void retract_call_notification(Call call, Conversation conversation);
|
2020-11-20 14:21:34 +00:00
|
|
|
public abstract async void notify_subscription_request(Conversation conversation);
|
|
|
|
public abstract async void notify_connection_error(Account account, ConnectionManager.ConnectionError error);
|
|
|
|
public abstract async void notify_muc_invite(Account account, Jid room_jid, Jid from_jid, string inviter_display_name);
|
|
|
|
public abstract async void notify_voice_request(Conversation conversation, Jid from_jid);
|
|
|
|
|
|
|
|
public abstract async void retract_content_item_notifications();
|
|
|
|
public abstract async void retract_conversation_notifications(Conversation conversation);
|
2018-03-02 12:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|