diff --git a/historyKeeper.js b/historyKeeper.js index fe16a204c..c636a090c 100644 --- a/historyKeeper.js +++ b/historyKeeper.js @@ -739,6 +739,7 @@ module.exports.create = function (cfg) { var config = parsed[2]; var metadata = {}; var lastKnownHash; + var txid; // clients can optionally pass a map of attributes // if the channel already exists this map will be ignored @@ -746,6 +747,7 @@ module.exports.create = function (cfg) { if (config && typeof config === "object" && !Array.isArray(parsed[2])) { lastKnownHash = config.lastKnownHash; metadata = config.metadata || {}; + txid = config.txid; if (metadata.expire) { metadata.expire = +metadata.expire * 1000 + (+new Date()); } @@ -796,11 +798,12 @@ module.exports.create = function (cfg) { msgCount++; // avoid sending the metadata message a second time if (isMetadataMessage(msg) && metadata_cache[channelName]) { return readMore(); } + if (txid) { msg[0] = txid; } sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(msg)], readMore); }, (err) => { if (err && err.code !== 'ENOENT') { if (err.message !== 'EINVAL') { Log.error("HK_GET_HISTORY", err); } - const parsedMsg = {error:err.message, channel: channelName}; + const parsedMsg = {error:err.message, channel: channelName, txid: txid}; sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(parsedMsg)]); return; } @@ -848,7 +851,7 @@ module.exports.create = function (cfg) { } // End of history message: - let parsedMsg = {state: 1, channel: channelName}; + let parsedMsg = {state: 1, channel: channelName, txid: txid}; sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(parsedMsg)]); }); }); diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 580cae7e9..152b9c9f9 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1855,6 +1855,8 @@ define([ } }; + var txid = Math.floor(Math.random() * 1000000); + var msgs = []; var completed = false; var onMsg = function (msg, sender) { @@ -1863,6 +1865,8 @@ define([ var parsed = parse(msg); if (!parsed) { return; } + if (parsed.txid && parsed.txid !== txid) { return; } + // Ignore the metadata message if (parsed.validateKey && parsed.channel) { return; } if (parsed.error && parsed.channel) { @@ -1883,9 +1887,20 @@ define([ return; } - msg = parsed[4]; + if (Array.isArray(parsed) && parsed[0] && parsed[0] !== txid) { return; } + // Keep only the history for our channel if (parsed[3] !== data.channel) { return; } + // If we want the full messages, push the parsed data + if (parsed[4] && full) { + msgs.push({ + msg: msg, + hash: parsed[4].slice(0,64) + }); + return; + } + // Otherwise, push the messages + msg = parsed[4]; if (msg) { msg = msg.replace(/cp\|(([A-Za-z0-9+\/=]+)\|)?/, ''); msgs.push(msg); @@ -1894,6 +1909,7 @@ define([ network.on('message', onMsg); var cfg = { + txid: txid, lastKnownHash: data.lastKnownHash }; var msg = ['GET_HISTORY', data.channel, cfg];