Time synchronization for TTL

This commit is contained in:
Juribiyan 2019-03-01 20:35:41 +05:00
parent dd4a87a8b3
commit a4886b05e4
4 changed files with 79 additions and 15 deletions

View File

@ -1921,6 +1921,64 @@ function readyset() {
}).trigger('change')
}
const NTP = {
calc: (t0, t1, t2, t3) => ((t1 - t0) + (t2 - t3)) / 2,
sync: function(force) {
this.pending = true
let _this = this
, t0 = (new Date()).valueOf()
$.ajax({
url: '/ntp.php',
success: function(servertime) {
let t1 = servertime,
t2 = servertime,
t3 = (new Date()).valueOf()
_this.offset = _this.calc(t0, t1, t2, t3)
},
error: function() {
pups.err('Failed to synchronize time with the server')
},
complete: function() {
_this.synced = true
while (_this.stack.length) {
_this.stack.shift().resolve(_this.timeWithOffset)
}
}
});
},
offset: 0,
stack: [],
get timeWithOffset() {
return new Date(new Date().getTime() + this.offset).getTime()
},
getServerTime: function() {
if (this.synced)
return Promise.resolve(this.timeWithOffset)
let promise = externallyResolvingPromise()
if (! this.pending)
this.sync()
this.stack.push(promise)
return promise.promise
}
}
function externallyResolvingPromise() {
let promiseResolve, promiseReject
, promise = new Promise((resolve, reject) => {
promiseResolve = resolve
promiseReject = reject
});
return {
promise: promise,
resolve: promiseResolve,
reject: promiseReject
}
}
// this will be applied to every new inserted node (post)
function processNodeInsertion($node) {
if(typeof $node === 'undefined')
@ -1934,21 +1992,26 @@ function processNodeInsertion($node) {
LatexIT.render($node);
// Post time-to-live updating
$node.find('.post-ttl').each(function() {
$node.find('.post-ttl:not(._inserted_)').each(function() {
let $this = $(this)
$this.addClass('_inserted_')
let iv
, updateTTL = () => {
if ($(this).length) {
let time_left = $(this).data('deleted-timestamp') - Math.round((new Date().getTime())/1000)
if (time_left >= 0) {
time_left = time_left / 3600
$(this).text(`${_.padLeft(Math.floor(time_left), 2, '0')}:${_.padLeft(Math.round((time_left%1)*60), 2, '0')}`)
}
else {
$.get(document.forms.delform.action) // call board.php to force delete posts
deleteItems([{action: 'delete_post', id: $(this).parents('.posthead').find('.reflink > a:not(.shl)').text()}], false, updater.markOnly)
clearInterval(iv)
}
time_left = (time_left >= 0) ? time_left / 3600 : 0
if ($this.length) {
NTP.getServerTime().then(srvTime => {
let ms_left = $this.data('deleted-timestamp')*1000 - srvTime
if (ms_left >= 0) {
let min_left = Math.round(ms_left / 60000)
, h_left = Math.floor(min_left / 60)
min_left -= h_left * 60
$this.text(`${_.padLeft(h_left, 2, '0')}:${_.padLeft(min_left, 2, '0')}`)
}
else {
$.get(document.forms.delform.action) // call board.php to force delete posts
deleteItems([{action: 'delete_post', id: $this.parents('.posthead').find('.reflink > a:not(.shl)').text()}], false, updater.markOnly)
clearInterval(iv)
}
})
}
else
clearInterval(iv)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

1
ntp.php Normal file
View File

@ -0,0 +1 @@
<?php echo (string) round($_SERVER['REQUEST_TIME_FLOAT'] * 1000);