[polari/wip/fmuellner/tracker: 29/36] logger: Add LogWalker for traversing a channel's logs



commit 38548868f275b71de7bcf96f3f2e74111a5d5e95
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Jan 8 16:10:17 2016 +0100

    logger: Add LogWalker for traversing a channel's logs
    
    We still use tp-logger for the history in the chat log. While it works
    well enough in that case, it is odd to use two completely different
    components for log handling, so add our own tracker-based LogWalker
    implementation as a replacement.

 src/logger.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
---
diff --git a/src/logger.js b/src/logger.js
index 86141ad..c72a5d4 100644
--- a/src/logger.js
+++ b/src/logger.js
@@ -1,3 +1,4 @@
+const GLib = imports.gi.GLib;
 const Gio = imports.gi.Gio;
 const Lang = imports.lang;
 const Polari = imports.gi.Polari;
@@ -133,3 +134,51 @@ var GenericQuery = new Lang.Class({
         }
     }
 });
+
+var LogWalker = new Lang.Class({
+    Name: 'LogWalker',
+
+    _init: function(room) {
+        this._room = room;
+        this._query = null;
+    },
+
+    getEvents: function(numEvents, callback) {
+        let returnFunc = r => {
+            callback(r.reverse().map(m => {
+                let { text, sender, isAction, isSelf } = m;
+                let timestamp = new Date(m.time).toLocaleFormat('%s');
+                let dt = GLib.DateTime.new_from_unix_utc(timestamp);
+                return new Polari.Message(text, sender, dt, isAction, isSelf);
+            }));
+        };
+
+        if (!this._query) {
+            this._query = new GenericQuery(numEvents);
+
+            let sparql = `
+                select polari:text(?msg) as ?text
+                       polari:nick(?sender) as ?sender
+                       polari:time(?msg) as ?time
+                       (exists { ?msg a polari:ActionMessage }) as ?isAction
+                       (exists { ?sender a polari:SelfContact }) as ?isSelf
+                { ?msg a polari:Message;
+                       polari:sender ?sender;
+                       polari:channel ?chan .
+                  ?chan polari:account ?account;
+                        polari:name "${this._room.channel_name}" .
+                  ?account polari:id "${this._room.account.get_path_suffix()}"
+                } order by desc(?time) desc(tracker:id(?msg))
+            `
+            this._query.run(sparql, null, returnFunc);
+        } else {
+            this._query.next(numEvents, null, returnFunc);
+        }
+    },
+
+    isEnd: function() {
+        if (this._query)
+            return this._query.isClosed();
+        return false;
+    }
+});


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