From f60ebc2af6143a4d963c5165f38c802e37da6620 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 7 Nov 2018 00:17:24 +0100 Subject: [PATCH] Use ContentItems for NotificationEvents --- libdino/src/application.vala | 2 +- libdino/src/service/notification_events.vala | 46 ++++++++++++-------- main/src/ui/notifications.vala | 24 ++++++++-- plugins/notification-sound/src/plugin.vala | 2 +- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/libdino/src/application.vala b/libdino/src/application.vala index 7f278fa0..d307c746 100644 --- a/libdino/src/application.vala +++ b/libdino/src/application.vala @@ -37,8 +37,8 @@ public interface Dino.Application : GLib.Application { ConversationManager.start(stream_interactor, db); ChatInteraction.start(stream_interactor); FileManager.start(stream_interactor, db); - NotificationEvents.start(stream_interactor); ContentItemStore.start(stream_interactor, db); + NotificationEvents.start(stream_interactor); SearchProcessor.start(stream_interactor, db); create_actions(); diff --git a/libdino/src/service/notification_events.vala b/libdino/src/service/notification_events.vala index c0efd7de..010341e3 100644 --- a/libdino/src/service/notification_events.vala +++ b/libdino/src/service/notification_events.vala @@ -9,13 +9,13 @@ public class NotificationEvents : StreamInteractionModule, Object { public static ModuleIdentity IDENTITY = new ModuleIdentity("notification_events"); public string id { get { return IDENTITY.id; } } - public signal void notify_message(Message message, Conversation conversation); + public signal void notify_content_item(ContentItem content_item, Conversation conversation); public signal void notify_subscription_request(Conversation conversation); public signal void notify_connection_error(Account account, ConnectionManager.ConnectionError error); private StreamInteractor stream_interactor; - private HashMap> mam_potential_new = new HashMap>(Account.hash_func, Account.equals_func); + private HashMap> mam_potential_new = new HashMap>(Account.hash_func, Account.equals_func); private Gee.List synced_accounts = new ArrayList(Account.equals_func); public static void start(StreamInteractor stream_interactor) { @@ -26,16 +26,16 @@ public class NotificationEvents : StreamInteractionModule, Object { public NotificationEvents(StreamInteractor stream_interactor) { this.stream_interactor = stream_interactor; - stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(on_message_received); + stream_interactor.get_module(ContentItemStore.IDENTITY).new_item.connect(on_content_item_received); stream_interactor.get_module(PresenceManager.IDENTITY).received_subscription_request.connect(on_received_subscription_request); 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) { - Entities.Message m = mam_potential_new[account][c]; - Entities.Message last_message = stream_interactor.get_module(MessageStorage.IDENTITY).get_last_message(c); - if (m.equals(last_message) && !c.read_up_to.equals(m)) { - on_message_received(m, c); + 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); } } mam_potential_new[account].clear(); @@ -43,26 +43,38 @@ public class NotificationEvents : StreamInteractionModule, Object { stream_interactor.connection_manager.connection_error.connect((account, error) => notify_connection_error(account, error)); } - private void on_message_received(Entities.Message message, Conversation conversation) { - bool is_mam_message = Xep.MessageArchiveManagement.MessageFlag.get_flag(message.stanza) != null; - if (!synced_accounts.contains(conversation.account) && is_mam_message) { + private void on_content_item_received(ContentItem item, Conversation conversation) { + if (!synced_accounts.contains(conversation.account)) { if (!mam_potential_new.has_key(conversation.account)) { - mam_potential_new[conversation.account] = new HashMap(Conversation.hash_func, Conversation.equals_func); + mam_potential_new[conversation.account] = new HashMap(Conversation.hash_func, Conversation.equals_func); } - mam_potential_new[conversation.account][conversation] = message; + mam_potential_new[conversation.account][conversation] = item; return; } - if (!should_notify_message(message, conversation)) return; + if (!should_notify(item, conversation)) return; if (stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus()) return; - notify_message(message, conversation); + notify_content_item(item, conversation); } - private bool should_notify_message(Entities.Message message, Conversation conversation) { + private bool should_notify(ContentItem content_item, Conversation conversation) { Conversation.NotifySetting notify = conversation.get_notification_setting(stream_interactor); + 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; + if (file_transfer.direction == FileTransfer.DIRECTION_SENT) return false; + break; + } if (notify == Conversation.NotifySetting.OFF) return false; Jid? nick = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(conversation.counterpart, conversation.account); - if (notify == Conversation.NotifySetting.HIGHLIGHT && nick != null) { - return Regex.match_simple("\\b" + Regex.escape_string(nick.resourcepart) + "\\b", message.body, RegexCompileFlags.CASELESS); + 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); + } } return true; } diff --git a/main/src/ui/notifications.vala b/main/src/ui/notifications.vala index b0a4fe1b..77a290a0 100644 --- a/main/src/ui/notifications.vala +++ b/main/src/ui/notifications.vala @@ -41,20 +41,36 @@ public class Notifications : Object { } public void start() { - stream_interactor.get_module(NotificationEvents.IDENTITY).notify_message.connect(notify_message); + stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect(notify_content_item); stream_interactor.get_module(NotificationEvents.IDENTITY).notify_subscription_request.connect(notify_subscription_request); stream_interactor.get_module(NotificationEvents.IDENTITY).notify_connection_error.connect(notify_connection_error); } - private void notify_message(Entities.Message message, Conversation conversation) { + private void notify_content_item(ContentItem content_item, Conversation conversation) { if (!notifications.has_key(conversation)) { notifications[conversation] = new Notification(""); notifications[conversation].set_default_action_and_target_value("app.open-conversation", new Variant.int32(conversation.id)); } string display_name = Util.get_conversation_display_name(stream_interactor, conversation); - string text = message.body; + string text = ""; + switch (content_item.type_) { + case MessageItem.TYPE: + Message message = (content_item as MessageItem).message; + text = message.body; + break; + case FileItem.TYPE: + FileItem file_item = content_item as FileItem; + FileTransfer transfer = file_item.file_transfer; + + if (transfer.direction == Message.DIRECTION_SENT) { + text = transfer.mime_type.has_prefix("image") ? _("Image sent") : _("File sent"); + } else { + text = transfer.mime_type.has_prefix("image") ? _("Image received") : _("File received"); + } + break; + } if (stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(conversation.counterpart, conversation.account)) { - string muc_occupant = Util.get_display_name(stream_interactor, message.from, conversation.account); + string muc_occupant = Util.get_display_name(stream_interactor, content_item.jid, conversation.account); text = @"$muc_occupant: $text"; } notifications[conversation].set_title(display_name); diff --git a/plugins/notification-sound/src/plugin.vala b/plugins/notification-sound/src/plugin.vala index ba251434..081fd1c3 100644 --- a/plugins/notification-sound/src/plugin.vala +++ b/plugins/notification-sound/src/plugin.vala @@ -9,7 +9,7 @@ public class Plugin : RootInterface, Object { this.app = app; Canberra.Context.create(out sound_context); - app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_message.connect((message, conversation) => { + app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect((item, conversation) => { sound_context.play(0, Canberra.PROP_EVENT_ID, "message-new-instant", Canberra.PROP_EVENT_DESCRIPTION, "New Dino message"); }); }