Use lastKnownHash to handle checkpoints in the realtime channel

This commit is contained in:
yflory
2019-01-18 18:17:34 +01:00
parent 8dbeee1af9
commit a66d8c1384
5 changed files with 234 additions and 138 deletions

View File

@@ -454,6 +454,7 @@ define([
},
// "priv" is not shared with other users but is needed by the apps
priv: {
clientId: clientId,
edPublic: store.proxy.edPublic,
friends: store.proxy.friends || {},
settings: store.proxy.settings,

View File

@@ -1,11 +1,7 @@
define([
'/common/common-util.js',
], function (Util) {
], function () {
var OO = {};
var getHistory = function (ctx, data, clientId, cb) {
};
var openChannel = function (ctx, obj, client, cb) {
var channel = obj.channel;
var padChan = obj.padChan;
@@ -29,7 +25,6 @@ define([
// ==> Use our netflux ID to create our client ID
if (!c.id) { c.id = chan.wc.myID + '-' + client; }
/// XXX send chan.history to client
chan.history.forEach(function (msg) {
ctx.emit('MESSAGE', msg, [client]);
});
@@ -61,17 +56,11 @@ define([
}
wc.on('join', function () {
// XXX
});
wc.on('leave', function (peer) {
// XXX
wc.on('leave', function () {
});
wc.on('message', function (msg) {
if (/^cp\|/.test(msg)) {
chan.history = [];
} else {
chan.history.push(msg);
}
chan.history.push(msg);
ctx.emit('MESSAGE', msg, chan.clients);
});
@@ -79,11 +68,7 @@ define([
chan.sendMsg = function (msg, cb) {
cb = cb || function () {};
wc.bcast(msg).then(function () {
if (/^cp\|/.test(msg)) {
chan.history = [];
} else {
chan.history.push(msg);
}
chan.history.push(msg);
cb();
}, function (err) {
cb({error: err});
@@ -92,7 +77,7 @@ define([
if (first) {
chan.clients = [client];
chan.lastCp = obj.lastCp;
chan.lastCpHash = obj.lastCpHash;
first = false;
cb();
}
@@ -100,7 +85,7 @@ define([
var hk = network.historyKeeper;
var cfg = {
validateKey: obj.validateKey,
lastKnownHash: chan.lastKnownHash,
lastKnownHash: chan.lastKnownHash || chan.lastCpHash,
owners: obj.owners,
};
var msg = ['GET_HISTORY', wc.id, cfg];
@@ -137,24 +122,11 @@ define([
}
if (parsed.error && parsed.channel) { return; }
var msg = parsed[4];
msg = parsed[4];
// Keep only the history for our channel
if (parsed[3] !== channel) { return; }
if (chan.lastCp) {
if (chan.lastCp === msg.slice(0, 11)) {
delete chan.lastCp;
}
return;
}
var isCp = /^cp\|/.test(msg);
if (isCp) {
chan.history = [];
return;
}
chan.lastKnownHash = msg.slice(0,64);
ctx.emit('MESSAGE', msg, chan.clients);
chan.history.push(msg);
@@ -172,6 +144,25 @@ define([
});
};
var updateHash = function (ctx, data, clientId, cb) {
var c = ctx.clients[clientId];
if (!c) { return void cb({ error: 'NOT_IN_CHANNEL' }); }
var chan = ctx.channels[c.channel];
if (!chan) { return void cb({ error: 'INVALID_CHANNEL' }); }
var hash = data;
var index = -1;
chan.history.some(function (msg, idx) {
if (msg.slice(0,64) === hash) {
index = idx + 1;
return true;
}
});
if (index !== -1) {
chan.history = chan.history.slice(index);
}
cb();
};
var sendMessage = function (ctx, data, clientId, cb) {
var c = ctx.clients[clientId];
if (!c) { return void cb({ error: 'NOT_IN_CHANNEL' }); }
@@ -214,6 +205,11 @@ define([
}
}
var oldChannel = ctx.clients[clientId].channel;
var oldChan = ctx.channels[oldChannel];
if (oldChan) {
ctx.emit('LEAVE', {id: clientId}, [oldChan.clients[0]]);
}
delete ctx.clients[clientId];
};
@@ -240,6 +236,9 @@ define([
if (cmd === 'SEND_MESSAGE') {
return void sendMessage(ctx, data, clientId, cb);
}
if (cmd === 'UPDATE_HASH') {
return void updateHash(ctx, data, clientId, cb);
}
if (cmd === 'OPEN_CHANNEL') {
return void openChannel(ctx, data, clientId, cb);
}