]> git.scottworley.com Git - reliable-chat/blobdiff - webclient/rc.html
Keep messages sorted by timestamp
[reliable-chat] / webclient / rc.html
index ff78fc0bad03aea4f3f4d1c79738cc3bda5f3a49..c3284da03b1480583a40cfb4eb93b0d332d22196 100644 (file)
        var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
 
        var session = Math.random();  // For outgoing message IDs
-       var since = {};  // server -> time: For fetch?since=
-       var seen = {};  // seen_key -> true
+       var since = {};    // server -> time: For fetch?since=
+       var seen = {};     // seen_key -> message
+       var history = [];  // List of messages sorted by Time
+       // Messages have these fields:
+       //   Time: The timestamp.  Median of ServerTimes
+       //   ID: Some unique string for deduping
+       //   Text: The text of the message
+       //   ServerTimes: server -> timestamp
+       //   UI: The DOM node for this message in the UI
 
        function rcnick() {
                var nick = localStorage.getItem("nick");
                }
        }
 
+       function rcformattime(t) {
+               var d = t.getDay();
+               d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
+               var h = t.getHours();
+               var m = t.getMinutes();
+               var s = t.getSeconds();
+               function pad(x) {
+                       return (x < 10 ? "0" : "") + x;
+               }
+               return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
+       }
+
        function rcaddmessagetohistory(message) {
-               var d = document.createElement("div");
-               d.appendChild(document.createTextNode(message));
+               var message_i;
+               if (message.Time) {
+                       for (var i = history.length - 1; ; i--) {
+                               if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
+                                       message_i = i+1;
+                                       history.splice(message_i, 0, message);
+                                       break;
+                               }
+                       }
+               } else {
+                       history.push(message);
+                       message_i = history.length-1;
+               }
+
+               var text = (message.Time ? rcformattime(message.Time) : "") + " " + message.Text;
+               message.UI = document.createElement("div");
+               message.UI.appendChild(document.createTextNode(text));
                var h = document.getElementById("history");
-               h.appendChild(d);
+               if (message_i + 1 < history.length) {
+                       h.insertBefore(message.UI, history[message_i + 1].UI);
+               } else {
+                       h.appendChild(message.UI);
+               }
                window.scrollTo(0, document.body.scrollHeight);
-               return d;
        }
 
        function make_seen_key(id, text) {
                return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
        }
 
+       function rcupdatemessagetime(message) {
+               // Set message.Time to be the median of message.ServerTimes
+               var times = [];
+               for (var i in message.ServerTimes) {
+                       times.push(message.ServerTimes[i]);
+               }
+               times.sort();
+               var middle = times.length/2;
+               if (times.length % 2) {
+                       message.Time = times[middle];
+               } else {
+                       var difference = times[middle].getTime() - times[middle-1].getTime();
+                       message.Time = new Date(times[middle-1].getTime() + difference/2);
+               }
+       }
+
        function rcreceivemessages(server, messages) {
                for (var i in messages) {
-                       var seen_key = make_seen_key(messages[i]['ID'], messages[i]['Text']);
-                       if (!(seen_key in seen)) {
-                               seen[seen_key] = true;
-                               rcaddmessagetohistory(messages[i]['Text']);
+                       var m = messages[i];
+                       m.Time = new Date(m.Time);
+                       var seen_key = make_seen_key(m.ID, m.Text);
+                       if (seen_key in seen) {
+                               seen[seen_key].ServerTimes[server] = m.Time;
+                               rcupdatemessagetime(seen[seen_key]);
+                       } else {
+                               m.ServerTimes = {};
+                               m.ServerTimes[server] = m.Time;
+                               seen[seen_key] = m;
+                               rcaddmessagetohistory(m);
                                for (var i in servers) {
                                        rcchangeserverstatus(servers[i], "sad");
                                }
                                        if (rtxt != null) {
                                                var messages = JSON.parse(rtxt);
                                                if (messages != null) {
-                                                       rcreceivemessages(server, messages);
                                                        delay = 40;
                                                        if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
-                                                               since[server] = messages[messages.length-1]["Time"];
+                                                               since[server] = messages[messages.length-1].Time;
                                                        }
+                                                       rcreceivemessages(server, messages);
                                                }
                                        }
                                }
        }
 
        function rcsend(d, message) {
-               var id = new Date().getTime() + "-" + session + "-" + Math.random();
-               seen[make_seen_key(id, message)] = true;
+               message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
+               seen[make_seen_key(message.ID, message.Text)] = message;
                var path = "/speak" +
-                       "?id=" + encodeURIComponent(id) +
-                       "&text=" + encodeURIComponent(message);
+                       "?id=" + encodeURIComponent(message.ID) +
+                       "&text=" + encodeURIComponent(message.Text);
                for (var i in servers) {
                        var uri = rcserverbase(servers[i]) + path;
                        var img = document.createElement("img");
                        message = "<" + rcnick() + "> " + input;
                }
 
-               var d = rcaddmessagetohistory(message);
-               rcsend(d, message);
+               var m = {'Text': message, 'ServerTimes': {}};
+               rcaddmessagetohistory(m);
+               rcsend(m.UI, m);
        }
 
        function rckeydown(event) {