diff --git a/www/common/common-messenger.js b/www/common/common-messenger.js index 5bc1f5829..7d1116981 100644 --- a/www/common/common-messenger.js +++ b/www/common/common-messenger.js @@ -387,6 +387,38 @@ define([ } }; + var getChannelMessagesSince = function (chan, data, keys) { + 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 msg0 = ['GET_HISTORY_RANGE', chan.id, { + //from: hash, + count: 10, + txid: txid, + } + ]; + network.sendto(network.historyKeeper, JSON.stringify(msg0)).then(function () { + }, function (err) { + throw new Error(err); + }); + return; + } + + var cfg = { + validateKey: keys ? keys.validateKey : undefined, + owners: [proxy.edPublic, data.edPublic], + lastKnownHash: data.lastKnownHash + }; + var msg = ['GET_HISTORY', chan.id, cfg]; + network.sendto(network.historyKeeper, JSON.stringify(msg)) + .then(function () {}, function (err) { + throw new Error(err); + }); + }; + var onChannelReady = function (chanId) { var cb = joining[chanId]; if (typeof(cb) !== 'function') { @@ -469,15 +501,31 @@ define([ if ((parsed.validateKey || parsed.owners) && parsed.channel) { return; } - // End of initial history - if (parsed.state && parsed.state === 1 && parsed.channel) { - if (channels[parsed.channel]) { + if (parsed.channel && channels[parsed.channel]) { + // Error in initial history + // History cleared while we're in the channel + if (parsed.error === 'ECLEARED') { + messenger.setChannelHead(parsed.channel, '', function () {}); + emit('CLEAR_CHANNEL', parsed.channel); + return; + } + // History cleared while we were offline + // ==> we asked for an invalid last known hash + if (parsed.error && parsed.errorCode === "EINVAL") { + messenger.setChannelHead(parsed.channel, '', function () { + getChannelMessagesSince(getChannel(parsed.channel), {}, {}); + }); + return; + } + + // End of initial history + if (parsed.state && parsed.state === 1 && parsed.channel) { // parsed.channel is Ready // channel[parsed.channel].ready(); channels[parsed.channel].ready = true; onChannelReady(parsed.channel); + return; } - return; } // Initial history message var chan = parsed[3]; @@ -495,7 +543,6 @@ define([ //channels[chan.id].notify(); } //channels[chan.id].refresh(); - // TODO emit message event } }; @@ -547,38 +594,6 @@ define([ } }; - var getChannelMessagesSince = function (chan, data, keys) { - 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 msg0 = ['GET_HISTORY_RANGE', chan.id, { - //from: hash, - count: 10, - txid: txid, - } - ]; - network.sendto(network.historyKeeper, JSON.stringify(msg0)).then(function () { - }, function (err) { - throw new Error(err); - }); - return; - } - - var cfg = { - validateKey: keys ? keys.validateKey : undefined, - owners: [proxy.edPublic, data.edPublic], - lastKnownHash: data.lastKnownHash - }; - var msg = ['GET_HISTORY', chan.id, cfg]; - network.sendto(network.historyKeeper, JSON.stringify(msg)) - .then(function () {}, function (err) { - throw new Error(err); - }); - }; - var openChannel = function (data) { var keys = data.keys; var encryptor = data.encryptor || Curve.createEncryptor(keys); diff --git a/www/contacts/messenger-ui.js b/www/contacts/messenger-ui.js index d73ce5604..e514dd96b 100644 --- a/www/contacts/messenger-ui.js +++ b/www/contacts/messenger-ui.js @@ -202,6 +202,9 @@ define([ }, []); }; + var clearChannel = function (id) { + $(getChat(id)).find('.cp-app-contacts-messages').html(''); + }; markup.chatbox = function (id, data, curvePublic) { var moreHistory = h('span.cp-app-contacts-more-history.fa.fa-history', { title: Messages.contacts_fetchHistory, @@ -277,14 +280,14 @@ define([ UI.alert(Messages.contacts_removeHistoryServerError); return; } - // TODO clear the UI + clearChannel(id); }); }); }); var avatar = h('div.cp-avatar'); - var headerContent = [avatar, moreHistory, data.isFriendCHat ? removeHistory : undefined]; + var headerContent = [avatar, moreHistory, data.isFriendChat ? removeHistory : undefined]; if (isApp) { headerContent = [ h('div.cp-app-contacts-header-title', Messages.contacts_padTitle), @@ -814,6 +817,10 @@ define([ onMessengerReady(); return; } + if (obj.ev === 'CLEAR_CHANNEL') { + clearChannel(obj.data); + return; + } if (obj.ev === 'PADCHAT_READY') { onPadChatReady(obj.data); return;