[polari/wip/fmuellner/window-experiments: 3/30] chatView: Split out _createMessage() method



commit f0559c35773cdfde97371f8a4cf0301a40535b8e
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Jul 16 02:04:49 2016 +0200

    chatView: Split out _createMessage() method
    
    As we display both Tp.Messages we send/receive and Tpl.Events we
    get from logs, we use a small intermediate object that abstracts
    the differences when passed to insertMessage(). Splitting the
    current code to create those objects into a helper methods makes
    it easier to keep the two code paths in sync, plus we'll soon
    use it from more places.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=768907

 src/chatView.js |   38 ++++++++++++++++++++++----------------
 1 files changed, 22 insertions(+), 16 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index 218a8c1..96264a2 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -477,6 +477,24 @@ const ChatView = new Lang.Class({
         this._insertPendingLogs();
     },
 
+    _createMessage: function(source) {
+        if (source instanceof Tp.Message) {
+            let [text, ] = source.to_text();
+            return { nick: source.sender.alias,
+                     text: text,
+                     timestamp: source.get_sent_timestamp() ||
+                                source.get_received_timestamp(),
+                     messageType: source.get_message_type() };
+        } else if (source instanceof Tpl.Event) {
+            return { nick: source.sender.alias,
+                     text: source.message,
+                     timestamp: source.timestamp,
+                     messageType: source.get_message_type() };
+        }
+
+        throw new Error('Cannot create message from source ' + source);
+    },
+
     _insertPendingLogs: function() {
         if (this._pendingLogs.length == 0)
             return;
@@ -504,10 +522,7 @@ const ChatView = new Lang.Class({
         let state = { lastNick: null, lastTimestamp: 0 };
         let iter = this._view.buffer.get_start_iter();
         for (let i = 0; i < pending.length; i++) {
-            let message = { nick: pending[i].sender.alias,
-                            text: pending[i].message,
-                            timestamp: pending[i].timestamp,
-                            messageType: pending[i].get_message_type() };
+            let message = this._createMessage(pending[i]);
             this._insertMessage(iter, message, state);
 
             if (!iter.is_end() || i < pending.length - 1)
@@ -1141,18 +1156,9 @@ const ChatView = new Lang.Class({
     },
 
     _insertTpMessage: function(tpMessage) {
-        let [text, flags] = tpMessage.to_text();
-
-        let message = { nick: tpMessage.sender.alias,
-                        text: text,
-                        messageType: tpMessage.get_message_type() };
-
-        let timestamp = tpMessage.get_sent_timestamp();
-        if (!timestamp)
-            timestamp = tpMessage.get_received_timestamp();
-        message.timestamp = timestamp;
-
-        let shouldHighlight = this._room.should_highlight_message(message.nick, text);
+        let message = this._createMessage(tpMessage);
+        let shouldHighlight = this._room.should_highlight_message(message.nick,
+                                                                  message.text);
 
         this._ensureNewLine();
 


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