untested implementation of trimHistory
This commit is contained in:
@@ -10,29 +10,13 @@ const Meta = require("./metadata");
|
||||
const WriteQueue = require("./write-queue");
|
||||
const BatchRead = require("./batch-read");
|
||||
|
||||
const Extras = require("./hk-util.js");
|
||||
|
||||
let Log;
|
||||
const now = function () { return (new Date()).getTime(); };
|
||||
const ONE_DAY = 1000 * 60 * 60 * 24; // one day in milliseconds
|
||||
|
||||
/* getHash
|
||||
* this function slices off the leading portion of a message which is
|
||||
most likely unique
|
||||
* these "hashes" are used to identify particular messages in a channel's history
|
||||
* clients store "hashes" either in memory or in their drive to query for new messages:
|
||||
* when reconnecting to a pad
|
||||
* when connecting to chat or a mailbox
|
||||
* thus, we can't change this function without invalidating client data which:
|
||||
* is encrypted clientside
|
||||
* can't be easily migrated
|
||||
* don't break it!
|
||||
*/
|
||||
const getHash = function (msg) {
|
||||
if (typeof(msg) !== 'string') {
|
||||
Log.warn('HK_GET_HASH', 'getHash() called on ' + typeof(msg) + ': ' + msg);
|
||||
return '';
|
||||
}
|
||||
return msg.slice(0,64);
|
||||
};
|
||||
const getHash = Extras.getHash;
|
||||
|
||||
const tryParse = function (str) {
|
||||
try {
|
||||
@@ -185,7 +169,7 @@ module.exports.create = function (cfg) {
|
||||
if (msg[0] === 0 && msg[2] === 'MSG' && typeof(msg[4]) === 'string') {
|
||||
// msgObj.offset is API guaranteed by our storage module
|
||||
// it should always be a valid positive integer
|
||||
offsetByHash[getHash(msg[4])] = msgObj.offset;
|
||||
offsetByHash[getHash(msg[4], Log)] = msgObj.offset;
|
||||
}
|
||||
// There is a trailing \n at the end of the file
|
||||
size = msgObj.offset + msgObj.buff.length + 1;
|
||||
@@ -502,7 +486,7 @@ module.exports.create = function (cfg) {
|
||||
msgStruct.push(now());
|
||||
|
||||
// storeMessage
|
||||
storeMessage(ctx, channel, JSON.stringify(msgStruct), isCp, getHash(msgStruct[4]));
|
||||
storeMessage(ctx, channel, JSON.stringify(msgStruct), isCp, getHash(msgStruct[4], Log));
|
||||
});
|
||||
};
|
||||
|
||||
@@ -601,7 +585,7 @@ module.exports.create = function (cfg) {
|
||||
const msg = tryParse(msgObj.buff.toString('utf8'));
|
||||
// if it was undefined then go onto the next message
|
||||
if (typeof msg === "undefined") { return readMore(); }
|
||||
if (typeof(msg[4]) !== 'string' || lastKnownHash !== getHash(msg[4])) {
|
||||
if (typeof(msg[4]) !== 'string' || lastKnownHash !== getHash(msg[4], Log)) {
|
||||
return void readMore();
|
||||
}
|
||||
offset = msgObj.offset;
|
||||
@@ -672,7 +656,7 @@ module.exports.create = function (cfg) {
|
||||
var content = parsed[4];
|
||||
if (typeof(content) !== 'string') { return; }
|
||||
|
||||
var hash = getHash(content);
|
||||
var hash = getHash(content, Log);
|
||||
if (hash === oldestKnownHash) {
|
||||
found = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user