[polari/wip/fmuellner/nickserv: 17/28] telepathyClient: Take over highlight notifications from ChatView



commit 6371acb1b0e937fc35c6a8c329f02e7c86af1439
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Jul 30 23:27:28 2016 +0200

    telepathyClient: Take over highlight notifications from ChatView
    
    There is no need to tie notifications to a particular UI element, and
    should we in fact ever support multiple windows or none, we'd run into
    issues that are easily avoided by separating notifications from widgets.
    
    The telepathy client looks like a reasonable place for monitoring
    channels for highlights.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769656

 src/chatView.js        |   24 ----------------------
 src/telepathyClient.js |   51 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 24 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index 65af13c..33e9f42 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -685,16 +685,11 @@ const ChatView = new Lang.Class({
             }));
     },
 
-    _getNotificationID: function(id) {
-        return 'pending-message-%s-%d'.format(this._room.id, id);
-    },
-
     _pendingMessageRemoved: function(channel, message) {
         let [id, valid] = message.get_pending_message_id();
         if (!valid || !this._pending.has(id))
             return;
         this._removePendingMark(id);
-        this._app.withdraw_notification(this._getNotificationID(id));
     },
 
     _removePendingMark: function(id) {
@@ -1194,8 +1189,6 @@ const ChatView = new Lang.Class({
 
     _insertTpMessage: function(tpMessage) {
         let message = this._createMessage(tpMessage);
-        let shouldHighlight = this._room.should_highlight_message(message.nick,
-                                                                  message.text);
 
         this._ensureNewLine();
 
@@ -1203,23 +1196,6 @@ const ChatView = new Lang.Class({
         this._insertMessage(iter, message, this._state);
         this._trackContact(tpMessage.sender);
 
-        if (shouldHighlight &&
-            !(this._toplevelFocus && this._active)) {
-            let summary = '%s %s'.format(this._room.display_name, message.nick);
-            let notification = new Gio.Notification();
-            notification.set_title(summary);
-            notification.set_body(message.text);
-
-            let account = this._room.account;
-            let param = GLib.Variant.new('(ssu)',
-                                         [ account.get_object_path(),
-                                           this._room.channel_name,
-                                           Utils.getTpEventTime() ]);
-            notification.set_default_action_and_target('app.join-room', param);
-            this._app.send_notification(this._getNotificationID(message.pendingId),
-                                        notification);
-        }
-
         if (message.pendingId == undefined /* outgoing */ ||
             (this._app.isRoomFocused(this._room) && this._pending.size == 0))
             this._channel.ack_message_async(tpMessage, null);
diff --git a/src/telepathyClient.js b/src/telepathyClient.js
index 13d49ce..ca63e94 100644
--- a/src/telepathyClient.js
+++ b/src/telepathyClient.js
@@ -364,6 +364,11 @@ const TelepathyClient = new Lang.Class({
                       return;
                 }
 
+                channel.connect('message-received',
+                                Lang.bind(this, this._onMessageReceived));
+                channel.connect('pending-message-removed',
+                                Lang.bind(this, this._onPendingMessageRemoved));
+
                 this._roomManager.ensureRoomForChannel(channel, 0);
             }));
     },
@@ -387,5 +392,51 @@ const TelepathyClient = new Lang.Class({
                 this._roomManager.ensureRoomForChannel(channel, userTime);
                 //channel.join_async('', null);
             }));
+    },
+
+    _getPendingNotificationID: function(room, id) {
+        return 'pending-message-%s-%d'.format(room.id, id);
+    },
+
+    _createNotification: function(room, summary, body) {
+        let notification = new Gio.Notification();
+        notification.set_title(summary);
+        notification.set_body(body);
+
+        let param = GLib.Variant.new('(ssu)',
+                                     [ room.account.object_path,
+                                       room.channel_name,
+                                       Utils.getTpEventTime() ]);
+        notification.set_default_action_and_target('app.join-room', param);
+        return notification;
+    },
+
+    _onMessageReceived: function(channel, msg) {
+        let [id, ] = msg.get_pending_message_id();
+        let room = this._roomManager.lookupRoomByChannel(channel);
+
+        // Rooms are removed instantly when the user requests it, but closing
+        // the corresponding channel may take a bit; it would be surprising
+        // to get notifications for a "closed" room, so just bail out
+        if (!room || this._app.isRoomFocused(room))
+            return;
+
+        let [text, ] = msg.to_text();
+        let nick = msg.sender.alias;
+        if (!room.should_highlight_message(nick, text))
+            return;
+
+        let summary = '%s %s'.format(room.display_name, nick);
+        let notification = this._createNotification(room, summary, text);
+        this._app.send_notification(this._getPendingNotificationID(room, id), notification);
+    },
+
+    _onPendingMessageRemoved: function(channel, msg) {
+        let [id, valid] = msg.get_pending_message_id();
+        if (!valid)
+            return;
+
+        let room = this._roomManager.lookupRoomByChannel(channel);
+        this._app.withdraw_notification(this._getPendingNotificationID(room, id));
     }
 });


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]