[polari/wip/bastianilso/status-hiding: 2/2] chatView: Compress status messages on idle channels



commit fec6911a6ac3da6dc6355552c9388d802066d26b
Author: Bastian Ilsø <bastianilso src gnome org>
Date:   Wed Jun 24 15:28:34 2015 +0200

    chatView: Compress status messages on idle channels
    
    If a channel is idle (no activity in 5 minutes), then
    display compressed status messages. Clicking on the compressed
    status message will uncompress it (useful, should the user
    be waiting for someone specific to chat with).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=711542

 src/chatView.js |  160 ++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 134 insertions(+), 26 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index 700bc0f..5d79488 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -20,6 +20,7 @@ const TIMESTAMP_INTERVAL = 300; // seconds of inactivity after which to
                                 // insert a timestamp
 
 const ACTIVITY_DURATION = 300; // min
+const STATUS_NOISE_MAXIMUM = 4;
 
 const NUM_INITIAL_LOG_EVENTS = 50; // number of log events to fetch on start
 const NUM_LOG_EVENTS = 10; // number of log events to fetch when requesting more
@@ -103,6 +104,11 @@ const ChatView = new Lang.Class({
         this._needsIndicator = true;
         this._pending = {};
         this._pendingLogs = [];
+        this._statusCounts = [
+            { left: 0,
+              joined: 0,
+              total: 0 },
+            ];
 
         let isRoom = room.type == Tp.HandleType.ROOM;
         let target = new Tpl.Entity({ type: isRoom ? Tpl.EntityType.ROOM
@@ -176,6 +182,8 @@ const ChatView = new Lang.Class({
             left_margin: MARGIN },
           { name: 'url',
             underline: Pango.Underline.SINGLE },
+          { name: 'expandable',
+            underline: Pango.Underline.SINGLE },
           { name: 'loading',
             justification: Gtk.Justification.CENTER }
         ];
@@ -491,6 +499,12 @@ const ChatView = new Lang.Class({
                     return Gdk.EVENT_STOP;
                 }
                 break;
+            } else if (tags[i].name.indexOf('status-compressed') != -1) {
+                if (isPress && button == Gdk.BUTTON_PRIMARY) {
+                    let statusTag = this._lookupTag('status' + tags[i].name.slice(-1));
+                    statusTag.invisible = !statusTag.invisible;
+                }
+                return Gdk.EVENT_STOP;
             }
         }
         return Gdk.EVENT_PROPAGATE;
@@ -504,7 +518,7 @@ const ChatView = new Lang.Class({
         let tags = iter.get_tags();
         let hovering = false;
         for (let i = 0; i < tags.length && !hovering; i++)
-            if (tags[i]._url)
+            if (tags[i]._url || tags[i].name.indexOf('status-compressed' != -1))
                 hovering = true;
 
         if (this._hoveringLink != hovering) {
@@ -601,7 +615,7 @@ const ChatView = new Lang.Class({
             { name: 'message-received',
               handler: Lang.bind(this, this._onMessageReceived) },
             { name: 'message-sent',
-              handler: Lang.bind(this, this._insertTpMessage) },
+              handler: Lang.bind(this, this._onMessageSent) },
             { name: 'pending-message-removed',
               handler: Lang.bind(this, this._pendingMessageRemoved) }
         ];
@@ -629,59 +643,81 @@ const ChatView = new Lang.Class({
     },
 
     _onMemberRenamed: function(room, oldMember, newMember) {
+        let time = GLib.DateTime.new_now_utc().to_unix();
+        let text = _("%s is now known as %s").format(oldMember.alias, newMember.alias);
         if (this._shouldStatus(oldMember.alias)) {
-            this._insertStatus(_("%s is now known as %s").format(oldMember.alias,
-                                                                 newMember.alias));
+            this._insertStatus(text);
+        } else if (time - this._state.lastTimestamp > ACTIVITY_DURATION) {
+            this._insertStatusCompressed(text, 'renamed');
         }
         this._setNickStatus(oldMember.alias, Tp.ConnectionPresenceType.OFFLINE);
         this._setNickStatus(newMember.alias, Tp.ConnectionPresenceType.AVAILABLE);
     },
 
     _onMemberDisconnected: function(room, member, message) {
+        let time = GLib.DateTime.new_now_utc().to_unix();
+        let text = _("%s has disconnected").format(member.alias);
+        if (message)
+            text += ' (%s)'.format(message);
         if (this._shouldStatus(member.alias)) {
-            let text = _("%s has disconnected").format(member.alias);
-            if (message)
-                text += ' (%s)'.format(message);
             this._insertStatus(text);
+        } else if (time - this._state.lastTimestamp > ACTIVITY_DURATION) {
+            this._insertStatusCompressed(text, 'left');
         }
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.OFFLINE);
     },
 
     _onMemberKicked: function(room, member, actor) {
+        let time = GLib.DateTime.new_now_utc().to_unix();
+        let message =
+            actor ? _("%s has been kicked by %s").format(member.alias,
+                                                         actor.alias)
+                  : _("%s has been kicked").format(member.alias);
         if (this._shouldStatus(member.alias)) {
-            let message =
-                actor ? _("%s has been kicked by %s").format(member.alias,
-                                                             actor.alias)
-                      : _("%s has been kicked").format(member.alias);
             this._insertStatus(message);
+        } else if (time - this._state.lastTimestamp > ACTIVITY_DURATION) {
+            this._insertStatusCompressed(text, 'left');
         }
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.OFFLINE);
     },
 
     _onMemberBanned: function(room, member, actor) {
+        let time = GLib.DateTime.new_now_utc().to_unix();
+        let message =
+            actor ? _("%s has been banned by %s").format(member.alias,
+                                                         actor.alias)
+                  : _("%s has been banned").format(member.alias)
         if (this._shouldStatus(member.alias)) {
-            let message =
-                actor ? _("%s has been banned by %s").format(member.alias,
-                                                             actor.alias)
-                      : _("%s has been banned").format(member.alias)
             this._insertStatus(message);
+        } else if (time - this._state.lastTimestamp > ACTIVITY_DURATION) {
+            this._insertStatusCompressed(text, 'left');
         }
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.OFFLINE);
     },
 
     _onMemberJoined: function(room, member) {
-        if (this._shouldStatus(member.alias))
-            this._insertStatus(_("%s joined").format(member.alias));
+        let time = GLib.DateTime.new_now_utc().to_unix();
+        let text = _("%s joined").format(member.alias);
+        if (this._shouldStatus(member.alias)) {
+            this._insertStatus(text);
+        } else if (time - this._state.lastTimestamp > ACTIVITY_DURATION) {
+            this._insertStatusCompressed(text, 'joined');
+        }
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.AVAILABLE);
     },
 
     _onMemberLeft: function(room, member, message) {
-        if (this._shouldStatus(member.alias)) {
-            let text = _("%s left").format(member.alias);
-            if (message)
-                text += ' (%s)'.format(message);
+        let time = GLib.DateTime.new_now_utc().to_unix();
+        let text = _("%s left").format(member.alias);
+
+        if (message)
+            text += ' (%s)'.format(message);
+
+        if (this._shouldStatus(member.alias))
             this._insertStatus(text);
-        }
+        else if (time - this._state.lastTimestamp > ACTIVITY_DURATION)
+            this._insertStatusCompressed(text, 'left');
+
         this._setNickStatus(member.alias, Tp.ConnectionPresenceType.OFFLINE);
     },
 
@@ -693,6 +729,24 @@ const ChatView = new Lang.Class({
         if (!nickTag)
            return;
         nickTag._active = GLib.DateTime.new_now_utc().to_unix();
+        this._resetStatusCompressed();
+    },
+
+    _onMessageSent: function(room, tpMessage) {
+        this._insertTpMessage(room, tpMessage);
+        this._resetStatusCompressed();
+    },
+
+    _resetStatusCompressed: function() {
+        let markStart = this._view.buffer.get_mark('idle-status-start');
+        let markEnd = this._view.buffer.get_mark('idle-status-end');
+        if (!markStart || !markEnd)
+            return;
+
+        this._view.buffer.delete_mark(markStart);
+        this._view.buffer.delete_mark(markEnd);
+        let statusCount =  { left: 0, joined: 0, total: 0 };
+        this._statusCounts.push(statusCount);
     },
 
     _shouldStatus: function(nick) {
@@ -705,14 +759,68 @@ const ChatView = new Lang.Class({
         return time - nickTag._active < ACTIVITY_DURATION;
     },
 
-    _insertStatus: function(text) {
+    _insertStatusCompressed: function(statusText, status) {
         let time = GLib.DateTime.new_now_utc().to_unix();
+        let buffer = this._view.buffer;
+
         if (time - this._joinTime < IGNORE_STATUS_TIME)
             return;
+
+        let statusGroupNumber = this._statusCounts.length-1;
+        let statusGroupTag = this._lookupTag('status' + statusGroupNumber);
+        if (!statusGroupTag) {
+            statusGroupTag = new Gtk.TextTag({ name: 'status' + statusGroupNumber });
+            buffer.tag_table.add(statusGroupTag);
+        }
+
+        let statusTag = this._lookupTag('status');
+
+        let statusCount = this._statusCounts[this._statusCounts.length-1];
+
+        if (statusCount.total == 0)
+            this._ensureNewLine();
+
+        if (status == 'left')
+            statusCount.left++;
+        else if (status == 'joined')
+            statusCount.joined++;
+        statusCount.total++;
+
+        let markStart = buffer.get_mark('idle-status-start');
+        if (!markStart) {
+            let iter = buffer.get_end_iter();
+            markStart = buffer.create_mark('idle-status-start', iter, true);
+        }
+
+
         this._state.lastNick = null;
-        this._ensureNewLine();
-        let iter = this._view.buffer.get_end_iter();
-        this._insertWithTagName(iter, text, 'status');
+
+        let endIter = buffer.get_end_iter();
+
+
+        this._insertWithTags(endIter, statusText + '\n', [statusGroupTag, statusTag]);
+
+        if (statusCount.total > STATUS_NOISE_MAXIMUM) {
+
+            let statusCompressedTag = this._lookupTag('status-compressed' + statusGroupNumber);
+            if (!statusCompressedTag) {
+                statusCompressedTag = new Gtk.TextTag({ name: 'status-compressed' + statusGroupNumber });
+                buffer.tag_table.add(statusCompressedTag);
+                statusGroupTag.invisible = true;
+            }
+
+            let compressedText = statusCount.joined + " users joined, "
+                                 + statusCount.left + " users left. ";
+
+            let markStartIter = buffer.get_iter_at_mark(markStart);
+            let markEndIter = markStartIter.copy();
+            markEndIter.forward_line();
+            buffer.delete(markStartIter, markEndIter);
+
+            markStartIter = buffer.get_iter_at_mark(markStart);
+            this._insertWithTags(markStartIter, compressedText + ' ( \u2026 ) \n' , [statusTag, 
statusCompressedTag]);
+
+        }
     },
 
     _formatTimestamp: function(timestamp) {


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