From 62905b47fc59e69818f67c2d3268a33a5f206a37 Mon Sep 17 00:00:00 2001 From: hackmud-dtr Date: Tue, 17 Aug 2021 12:47:14 -0700 Subject: [PATCH] Track server date via headers to avoid drifting. With the new-ish time-gate on after, a user who doesn't get many chats but continues to request after:date of last chat will eventually get locked out of new chats. Previously, this used local dates to try to avoid that, but local dates need not correlate at all to server dates. This patch uses server dates from the date header where possible, falling back to local dates only if necessary. This should make it more reliably avoid drifting and getting stuck without chats forever. --- js/chat.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/js/chat.js b/js/chat.js index 022310a..67b0a7a 100644 --- a/js/chat.js +++ b/js/chat.js @@ -6,7 +6,13 @@ if(typeof window!="undefined") { var x=new XMLHttpRequest(); x.onreadystatechange=function() { if(x.readyState !== XMLHttpRequest.DONE)return; - cb(null,{statusCode:x.status},x.responseText?JSON.parse(x.responseText):[]); + var headers=x.getAllResponseHeaders().trim().split('\r\n'); + var map={} + for(var i=0;i { return new Promise( (resolve,reject) => { request({ method: 'POST', uri: 'https://'+API.domain_root+'/mobile/'+endpoint+'.json', json:dat}, (error,response,body) => { + LAST_SERVER_DATE=new Date(response.headers.date)/1||-1; if(!error && response.statusCode == 200) resolve(body) else { @@ -124,7 +133,15 @@ Account.prototype.poll=function(ext={}) { ext.before=this.last+0.1; if(ext.after=='last') { ext.after=this.last-0.1; - var five_min_ago=new Date()/1000 - 300; + var five_min_ago; + if(LAST_SERVER_DATE > 0) { + // if we have a last date from the server, we can use it to ensure we don't drift + five_min_ago=LAST_SERVER_DATE/1000 - 300; + } + else { + // reluctantly use local dates + five_min_ago=new Date()/1000 - 300; + } if(ext.after < five_min_ago) ext.after=five_min_ago }