Improve getHistoryRange to support 'toHash'

pull/1/head
yflory 4 years ago
parent a46baea85b
commit b31707098a

@ -662,10 +662,11 @@ const handleGetHistoryRange = function (Env, Server, seq, userId, parsed) {
} }
var oldestKnownHash = map.from; var oldestKnownHash = map.from;
var untilHash = map.to;
var desiredMessages = map.count; var desiredMessages = map.count;
var desiredCheckpoint = map.cpCount; var desiredCheckpoint = map.cpCount;
var txid = map.txid; var txid = map.txid;
if (typeof(desiredMessages) !== 'number' && typeof(desiredCheckpoint) !== 'number') { if (typeof(desiredMessages) !== 'number' && typeof(desiredCheckpoint) !== 'number' && !untilHash) {
return void Server.send(userId, [seq, 'ERROR', 'UNSPECIFIED_COUNT', HISTORY_KEEPER_ID]); return void Server.send(userId, [seq, 'ERROR', 'UNSPECIFIED_COUNT', HISTORY_KEEPER_ID]);
} }
@ -674,7 +675,7 @@ const handleGetHistoryRange = function (Env, Server, seq, userId, parsed) {
} }
Server.send(userId, [seq, 'ACK']); Server.send(userId, [seq, 'ACK']);
Env.getOlderHistory(channelName, oldestKnownHash, desiredMessages, desiredCheckpoint, function (err, toSend) { Env.getOlderHistory(channelName, oldestKnownHash, untilHash, desiredMessages, desiredCheckpoint, function (err, toSend) {
if (err && err.code !== 'ENOENT') { if (err && err.code !== 'ENOENT') {
Env.Log.error("HK_GET_OLDER_HISTORY", err); Env.Log.error("HK_GET_OLDER_HISTORY", err);
} }

@ -221,6 +221,7 @@ const computeMetadata = function (data, cb) {
const getOlderHistory = function (data, cb) { const getOlderHistory = function (data, cb) {
const oldestKnownHash = data.hash; const oldestKnownHash = data.hash;
const untilHash = data.toHash;
const channelName = data.channel; const channelName = data.channel;
const desiredMessages = data.desiredMessages; const desiredMessages = data.desiredMessages;
const desiredCheckpoint = data.desiredCheckpoint; const desiredCheckpoint = data.desiredCheckpoint;
@ -251,6 +252,13 @@ const getOlderHistory = function (data, cb) {
var toSend = []; var toSend = [];
if (typeof (desiredMessages) === "number") { if (typeof (desiredMessages) === "number") {
toSend = messages.slice(-desiredMessages); toSend = messages.slice(-desiredMessages);
} else if (untilHash) {
for (var i = messages.length - 1; i >= 0; i--) {
toSend.unshift(messages[i]);
if (Array.isArray(messages[i]) && HK.getHash(messages[i][4], Log) === untilHash) {
break;
}
}
} else { } else {
let cpCount = 0; let cpCount = 0;
for (var i = messages.length - 1; i >= 0; i--) { for (var i = messages.length - 1; i >= 0; i--) {

@ -281,12 +281,13 @@ Workers.initialize = function (Env, config, _cb) {
}); });
}; };
Env.getOlderHistory = function (channel, oldestKnownHash, desiredMessages, desiredCheckpoint, cb) { Env.getOlderHistory = function (channel, oldestKnownHash, toHash, desiredMessages, desiredCheckpoint, cb) {
Env.store.getWeakLock(channel, function (next) { Env.store.getWeakLock(channel, function (next) {
sendCommand({ sendCommand({
channel: channel, channel: channel,
command: "GET_OLDER_HISTORY", command: "GET_OLDER_HISTORY",
hash: oldestKnownHash, hash: oldestKnownHash,
toHash: toHash,
desiredMessages: desiredMessages, desiredMessages: desiredMessages,
desiredCheckpoint: desiredCheckpoint, desiredCheckpoint: desiredCheckpoint,
}, Util.both(next, cb)); }, Util.both(next, cb));

@ -2069,7 +2069,7 @@ define([
if (first) { if (first) {
// If the first message if not a checkpoint, it means it is the first // If the first message if not a checkpoint, it means it is the first
// message of the pad, so we have the full history! // message of the pad, so we have the full history!
if (!/^cp\|/.test(msg)) { fullHistory = true; } if (!/^cp\|/.test(msg) && !data.toHash) { fullHistory = true; }
lastKnownHash = msg.slice(0,64); lastKnownHash = msg.slice(0,64);
first = false; first = false;
} }
@ -2086,7 +2086,8 @@ define([
network.on('message', onMsg); network.on('message', onMsg);
network.sendto(hk, JSON.stringify(['GET_HISTORY_RANGE', data.channel, { network.sendto(hk, JSON.stringify(['GET_HISTORY_RANGE', data.channel, {
from: data.lastKnownHash, from: data.lastKnownHash,
cpCount: data.cpCount || 2, to: data.toHash,
cpCount: data.cpCount || 2, // Ignored if "to" is provided
txid: txid txid: txid
}])); }]));
}; };

@ -1108,6 +1108,7 @@ define([
Cryptpad.getHistoryRange({ Cryptpad.getHistoryRange({
channel: channel, channel: channel,
validateKey: validate, validateKey: validate,
toHash: data.toHash,
lastKnownHash: data.lastKnownHash lastKnownHash: data.lastKnownHash
}, function (data) { }, function (data) {
cb({ cb({

Loading…
Cancel
Save