an attempt to address #69

Add a global-like object that stores the delta from server time and
client time in a list and use the resulting average to "correct"
utils.ago diffs.
This commit is contained in:
Martin Zimmermann 2014-03-04 15:48:44 +01:00
parent c4b80ff702
commit 9a678e4691
5 changed files with 30 additions and 8 deletions

View File

@ -183,7 +183,7 @@ def make_app(conf=None, threading=True, multiprocessing=False, uwsgi=False):
wrapper.append(partial(wsgi.CORSMiddleware, wrapper.append(partial(wsgi.CORSMiddleware,
origin=origin(isso.conf.getiter("general", "host")), origin=origin(isso.conf.getiter("general", "host")),
allowed=("Origin", "Content-Type"), exposed=("X-Set-Cookie", ))) allowed=("Origin", "Content-Type"), exposed=("X-Set-Cookie", "Date")))
wrapper.extend([wsgi.SubURI, ProxyFix]) wrapper.extend([wsgi.SubURI, ProxyFix])

View File

@ -1,4 +1,4 @@
define(["app/lib/promise"], function(Q) { define(["app/lib/promise", "app/globals"], function(Q, globals) {
"use strict"; "use strict";
@ -41,8 +41,9 @@ define(["app/lib/promise"], function(Q) {
function onload() { function onload() {
var cookie = xhr.getResponseHeader("X-Set-Cookie"); globals.offset.update(new Date(xhr.getResponseHeader("Date")));
var cookie = xhr.getResponseHeader("X-Set-Cookie");
if (cookie && cookie.match(/^isso-/)) { if (cookie && cookie.match(/^isso-/)) {
document.cookie = cookie; document.cookie = cookie;
} }

21
isso/js/app/globals.js Normal file
View File

@ -0,0 +1,21 @@
define(function() {
"use strict";
var Offset = function() {
this.values = [];
};
Offset.prototype.update = function(remoteTime) {
this.values.push((new Date()).getTime() - remoteTime);
};
Offset.prototype.localTime = function() {
return new Date((new Date()).getTime() + this.values.reduce(
function(a, b) { return a + b; }) / this.values.length);
};
return {
offset: new Offset()
};
});

View File

@ -1,7 +1,7 @@
/* Isso Ich schrei sonst! /* Isso Ich schrei sonst!
*/ */
define(["app/text/html", "app/dom", "app/utils", "app/config", "app/api", "app/markup", "app/i18n", "app/lib"], define(["app/text/html", "app/dom", "app/utils", "app/config", "app/api", "app/markup", "app/i18n", "app/lib", "app/globals"],
function(templates, $, utils, config, api, Mark, i18n, lib) { function(templates, $, utils, config, api, Mark, i18n, lib, globals) {
"use strict"; "use strict";
@ -100,7 +100,7 @@ define(["app/text/html", "app/dom", "app/utils", "app/config", "app/api", "app/m
// update datetime every 60 seconds // update datetime every 60 seconds
var refresh = function() { var refresh = function() {
$(".permalink > date", el).textContent = utils.ago( $(".permalink > date", el).textContent = utils.ago(
new Date(parseInt(comment.created, 10) * 1000)); globals.offset.localTime(), new Date(parseInt(comment.created, 10) * 1000));
setTimeout(refresh, 60*1000); setTimeout(refresh, 60*1000);
}; };

View File

@ -5,9 +5,9 @@ define(["app/markup"], function(Mark) {
return (document.cookie.match('(^|; )' + cookie + '=([^;]*)') || 0)[2]; return (document.cookie.match('(^|; )' + cookie + '=([^;]*)') || 0)[2];
}; };
var ago = function(date) { var ago = function(localTime, date) {
var diff = (((new Date()).getTime() - date.getTime()) / 1000), var diff = ((localTime.getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400); day_diff = Math.floor(diff / 86400);
if (isNaN(day_diff) || day_diff < 0) { if (isNaN(day_diff) || day_diff < 0) {