Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encode now-timestamps properly to avoid overwrites #62

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions views/messages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var View = require('kappa-view-level')
var timestamp = require('monotonic-timestamp')
var through = require('through2')
var readonly = require('read-only-stream')
var charwise = require('charwise')
Expand All @@ -15,12 +16,11 @@ module.exports = function (lvl) {
if (!msg.value.timestamp) return []

// If the message is from <<THE FUTURE>>, index it at _now_.
var timestamp = msg.value.timestamp
var now = new Date().getTime()
if (timestamp > now) timestamp = now
var ts = msg.value.timestamp
if (isFutureMonotonicTimestamp(ts)) ts = timestamp()

if (msg.value.type.startsWith('chat/') && msg.value.content.channel) {
var key = 'msg!' + msg.value.content.channel + '!' + charwise.encode(timestamp)
var key = 'msg!' + msg.value.content.channel + '!' + charwise.encode(ts)
return [
[key, msg]
]
Expand Down Expand Up @@ -89,3 +89,17 @@ function sanitize (msg) {
if (typeof msg.value.content.text !== 'string') return null
return msg
}

function monotonicTimestampToTimestamp (timestamp) {
if (/^[0-9]+\.[0-9]+$/.test(String(timestamp))) {
return Number(String(timestamp).split('.')[0])
} else {
return timestamp
}
}

function isFutureMonotonicTimestamp () {
var timestamp = monotonicTimestampToTimestamp(timestamp)
var now = new Date().getTime()
return timestamp > now
}