Improve messenger initial history

pull/1/head
yflory 6 years ago
parent 26055e5e55
commit f3c36bfd57

@ -113,12 +113,11 @@ define([
return friend; return friend;
}; };
var initRangeRequest = function (txid, chanId, sig, cb) { var initRangeRequest = function (txid, chanId, cb) {
messenger.range_requests[txid] = { messenger.range_requests[txid] = {
messages: [], messages: [],
cb: cb, cb: cb,
chanId: chanId, chanId: chanId,
sig: sig,
}; };
}; };
@ -145,7 +144,7 @@ define([
} }
var txid = Util.uid(); var txid = Util.uid();
initRangeRequest(txid, chanId, hash, cb); initRangeRequest(txid, chanId, cb);
var msg = [ 'GET_HISTORY_RANGE', chan.id, { var msg = [ 'GET_HISTORY_RANGE', chan.id, {
from: hash, from: hash,
count: count, count: count,
@ -275,7 +274,7 @@ define([
network.sendto(sender, JSON.stringify(data)); network.sendto(sender, JSON.stringify(data));
}; };
var orderMessages = function (channel, new_messages /*, sig */) { var orderMessages = function (channel, new_messages) {
var messages = channel.messages; var messages = channel.messages;
// TODO improve performance, guarantee correct ordering // TODO improve performance, guarantee correct ordering
@ -403,6 +402,21 @@ define([
return void console.error("received response to unknown request"); return void console.error("received response to unknown request");
} }
if (!req.cb) {
// This is the initial history for a pad chat
if (type === 'HISTORY_RANGE') {
if (!getChannel(req.chanId)) { return; }
if (!Array.isArray(parsed[2])) { return; }
pushMsg(getChannel(req.chanId), parsed[2][4]);
} else if (type === 'HISTORY_RANGE_END') {
if (!getChannel(req.chanId)) { return; }
getChannel(req.chanId).ready = true;
onChannelReady(req.chanId);
return;
}
return;
}
if (type === 'HISTORY_RANGE') { if (type === 'HISTORY_RANGE') {
req.messages.push(parsed[2]); req.messages.push(parsed[2]);
} else if (type === 'HISTORY_RANGE_END') { } else if (type === 'HISTORY_RANGE_END') {
@ -434,7 +448,7 @@ define([
}; };
}); });
orderMessages(channel, decrypted, req.sig); orderMessages(channel, decrypted);
req.cb(void 0, decrypted); req.cb(void 0, decrypted);
return deleteRangeRequest(txid); return deleteRangeRequest(txid);
} else { } else {
@ -453,11 +467,6 @@ define([
// channel[parsed.channel].ready(); // channel[parsed.channel].ready();
channels[parsed.channel].ready = true; channels[parsed.channel].ready = true;
onChannelReady(parsed.channel); onChannelReady(parsed.channel);
var updateTypes = channels[parsed.channel].updateOnReady;
if (updateTypes) {
//channels[parsed.channel].updateUI(updateTypes);
}
} }
return; return;
} }
@ -531,6 +540,24 @@ define([
var getChannelMessagesSince = function (chan, data, keys) { var getChannelMessagesSince = function (chan, data, keys) {
console.log('Fetching [%s] messages since [%s]', chan.id, data.lastKnownHash || ''); console.log('Fetching [%s] messages since [%s]', chan.id, data.lastKnownHash || '');
if (chan.isPadChat) {
// We need to use GET_HISTORY_RANGE to make sure we won't get the full history
var txid = Util.uid();
initRangeRequest(txid, chan.id, undefined);
var msg = ['GET_HISTORY_RANGE', chan.id, {
//from: hash,
count: 10,
txid: txid,
}
];
network.sendto(network.historyKeeper, JSON.stringify(msg)).then(function () {
}, function (err) {
throw new Error(err);
});
return;
}
var cfg = { var cfg = {
validateKey: keys ? keys.validateKey : undefined, validateKey: keys ? keys.validateKey : undefined,
owners: [proxy.edPublic, data.edPublic], owners: [proxy.edPublic, data.edPublic],
@ -608,7 +635,7 @@ define([
}); });
// FIXME don't subscribe to the channel implicitly // FIXME don't subscribe to the channel implicitly
getChannelMessagesSince(chan, data, keys); getChannelMessagesSince(channel, data, keys);
}, function (err) { }, function (err) {
console.error(err); console.error(err);
}); });

@ -32,6 +32,7 @@ define([
messages: info.messages || [], messages: info.messages || [],
name: info.name, name: info.name,
isFriendChat: info.isFriendChat, isFriendChat: info.isFriendChat,
needMoreHistory: !info.isPadChat,
isPadChat: info.isPadChat, isPadChat: info.isPadChat,
curvePublic: info.curvePublic, curvePublic: info.curvePublic,
HEAD: h || info.lastKnownHash, HEAD: h || info.lastKnownHash,
@ -412,16 +413,12 @@ define([
$messages.find('div.cp-app-contacts-chat[data-key]').hide(); $messages.find('div.cp-app-contacts-chat[data-key]').hide();
if ($chat.length) { if ($chat.length) {
var $chat_messages = $chat.find('div.cp-app-contacts-message'); var $chat_messages = $chat.find('div.cp-app-contacts-message');
if (!$chat_messages.length) { if (!$chat_messages.length || channel.needMoreHistory) {
delete channel.needMoreHistory;
var $more = $chat.find('.cp-app-contacts-more-history'); var $more = $chat.find('.cp-app-contacts-more-history');
$more.click(); $more.click();
} }
$chat.show(); $chat.show();
if (channel.isPadChat) {
// Always scroll bottom for now in pad chat (no last known hash)
var $messagebox = $chat.find('.cp-app-contacts-messages');
$messagebox.scrollTop($messagebox.outerHeight());
}
return; return;
} else { } else {
console.error("Chat is missing... Please reload the page and try again."); console.error("Chat is missing... Please reload the page and try again.");

Loading…
Cancel
Save