From e7861171108a980c8b2552f878c0458a0a6f7c0a Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 3 Aug 2017 15:04:29 +0200 Subject: [PATCH] get new messages when reconnecting and WIP get more history --- www/common/common-messaging.js | 89 +++++++++++++++++++++++++++++----- www/common/cryptpad-common.js | 1 + 2 files changed, 77 insertions(+), 13 deletions(-) diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index 4fecba707..3e620e82f 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -9,10 +9,15 @@ define([ }; var Messages; - var setEditable = Msg.setEditable = function (bool) { + Msg.setEditable = function (bool) { bool = !bool; Msg.inputs.forEach(function (input) { - input.setAttribute('disabled', bool); + if (bool) { + input.setAttribute('disabled', bool); + } else { + input.removeAttribute('disabled'); + } + if (Messages) { // set placeholder var placeholder = bool? Messages.disconnected: Messages.contacts_typeHere; @@ -281,6 +286,7 @@ define([ var chan = parsed[3]; if (!chan || !channels[chan]) { return; } pushMsg(common, channels[chan], parsed[4]); + channels[chan].refresh(); }; var onMessage = function (common, msg, sender, chan) { if (!channels[chan.id]) { return; } @@ -298,12 +304,26 @@ define([ var data = getFriend(common, curvePublic); var proxy = common.getProxy(); + // Input + var channel = channels[data.channel]; + var $header = $('
', { 'class': 'header', }).appendTo($container); var $avatar = $('
', {'class': 'avatar'}).appendTo($header); + // more history... + $('', { + 'class': 'more-history', + }) + .text('get more history') + .click(function () { + console.log("GETTING HISTORY"); + channel.getPreviousMessages(); + }) + .appendTo($header); + var $removeHistory = $('', { 'class': 'remove-history fa fa-eraser', title: common.Messages.contacts_removeHistoryTitle @@ -394,6 +414,15 @@ define([ }; + Msg.getLatestMessages = function () { + Object.keys(channels).forEach(function (id) { + if (id === 'me') { return; } + var friend = channels[id]; + friend.getMessagesSinceDisconnect(); + friend.refresh(); + }); + }; + Msg.init = function (common, $listContainer, $msgContainer) { var network = common.getNetwork(); var proxy = common.getProxy(); @@ -470,6 +499,7 @@ define([ $chat.show(); Msg.active = curvePublic; + // TODO don't mark messages as read unless you have displayed them refresh(curvePublic); }; @@ -559,6 +589,37 @@ define([ var statusText = status ? 'online' : 'offline'; $friend.find('.status').attr('class', 'status '+statusText); }; + var getMoreHistory = function (network, chan, hash, count) { + var msg = [ + 'GET_HISTORY_RANGE', + chan.id, + { + from: hash, + count: count, + } + ]; + + console.log(msg); + + network.sendto(network.historyKeeper, JSON.stringify(msg)).then(function (a, b, c) { + console.log(a, b, c); + }, function (err) { + throw new Error(err); + }); + }; + + var getChannelMessagesSince = function (network, chan, data, keys) { + var cfg = { + validateKey: keys.validateKey, + owners: [proxy.edPublic, data.edPublic], + lastKnownHash: data.lastKnownHash + }; + var msg = ['GET_HISTORY', chan.id, cfg]; + network.sendto(network.historyKeeper, JSON.stringify(msg)) + .then($.noop, function (err) { + throw new Error(err); + }); + }; // Open the channels var openFriendChannel = function (f) { @@ -578,9 +639,18 @@ define([ removeUI: function () { removeUI(data.curvePublic); }, updateUI: function (types) { updateUI(data.curvePublic, types); }, updateStatus: function () { updateStatus(data.curvePublic); }, + getMessagesSinceDisconnect: function () { + getChannelMessagesSince(network, chan, data, keys); + }, wc: chan, userList: [], - mapId: {} + mapId: {}, + getPreviousMessages: function () { + var oldestMessages = channel.messages[0]; + var oldestHash = oldestMessages[0]; + + getMoreHistory(network, chan, oldestHash, 10); + }, }; chan.on('message', function (msg, sender) { onMessage(common, msg, sender, chan); @@ -609,20 +679,13 @@ define([ } channel.updateStatus(); }); - var cfg = { - validateKey: keys.validateKey, - owners: [proxy.edPublic, data.edPublic], - lastKnownHash: data.lastKnownHash - }; - var msg = ['GET_HISTORY', chan.id, cfg]; - network.sendto(network.historyKeeper, JSON.stringify(msg)) - .then($.noop, function (err) { - throw new Error(err); - }); + + getChannelMessagesSince(network, chan, data, keys); }, function (err) { console.error(err); }); }; + Object.keys(friends).forEach(openFriendChannel); var checkNewFriends = function () { diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 190069804..3dfe11a5d 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -129,6 +129,7 @@ define([ common.createData = Messaging.createData; common.getPendingInvites = Messaging.getPending; common.enableMessaging = Messaging.setEditable; + common.getLatestMessages = Messaging.getLatestMessages; // Userlist common.createUserList = UserList.create;