From 641b69446d516cd15a950d6d49a8644da6f2fc06 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 16 Sep 2019 16:48:32 +0200 Subject: [PATCH] Fix issues and replace chat module --- www/common/common-ui-elements.js | 2 +- www/common/messenger-ui.js | 44 +++++++++++++++++++++---------- www/common/outer/async-store.js | 44 +++---------------------------- www/common/outer/messenger.js | 13 +++++---- www/common/outer/noworker.js | 3 --- www/common/outer/serviceworker.js | 6 ----- www/common/outer/sharedworker.js | 6 ----- www/common/outer/store-rpc.js | 3 --- www/common/outer/webworker.js | 3 --- www/common/sframe-common-outer.js | 13 ++++++--- 10 files changed, 50 insertions(+), 87 deletions(-) diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index 89bc248b2..cd2c1e370 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -774,7 +774,7 @@ define([ edPublic: t.edPublic, avatar: t.avatar, id: id - } + }; }); var teamsList = UIElements.getFriendsList('Share with a team', { common: common, diff --git a/www/common/messenger-ui.js b/www/common/messenger-ui.js index 223f67bb3..202e25cec 100644 --- a/www/common/messenger-ui.js +++ b/www/common/messenger-ui.js @@ -40,7 +40,6 @@ define([ }; MessengerUI.create = function ($container, common, toolbar) { - var sframeChan = common.getSframeChannel(); var metadataMgr = common.getMetadataMgr(); var origin = metadataMgr.getPrivateData().origin; var readOnly = metadataMgr.getPrivateData().readOnly; @@ -76,12 +75,17 @@ define([ ]), ]); + var execCommand = function () { + console.warn(arguments); + }; + /* var execCommand = function (cmd, data, cb) { sframeChan.query('Q_CHAT_COMMAND', {cmd: cmd, data: data}, function (err, obj) { if (err || (obj && obj.error)) { return void cb(err || (obj && obj.error)); } cb(void 0, obj); }); }; + */ var $userlist = $(friendList).appendTo($container); var $messages = $(messaging).appendTo($container); @@ -780,10 +784,6 @@ define([ // var onLeaveRoom - execCommand('GET_MY_INFO', null, function (e, info) { - contactsData[info.curvePublic] = info; - }); - var ready = false; var onMessengerReady = function () { if (isApp) { return; } @@ -826,15 +826,8 @@ define([ $messages.find('.cp-app-contacts-input textarea').prop('disabled', false); }; - // Initialize chat when outer is ready (all channels loaded) - // TODO: try again in outer if fail to load a channel - if (!isApp) { - execCommand('INIT_FRIENDS', null, function () {}); - execCommand('IS_READY', null, function (err, yes) { - if (yes) { onMessengerReady(); } - }); - } - sframeChan.on('EV_CHAT_EVENT', function (obj) { + //sframeChan.on('EV_CHAT_EVENT', function (obj) { + var onEvent = function (obj) { var cmd = obj.ev; var data = obj.data; if (cmd === 'READY') { @@ -881,7 +874,30 @@ define([ onUnfriend(data); return; } + }; + var module = common.makeUniversal('messenger', { + onEvent: onEvent + }); + execCommand = function (cmd, data, cb) { + module.execCommand(cmd, data, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(void 0, obj); + }); + }; + //}); + + execCommand('GET_MY_INFO', null, function (e, info) { + contactsData[info.curvePublic] = info; }); + + + // Initialize chat when outer is ready (all channels loaded) + // TODO: try again in outer if fail to load a channel + if (!isApp) { + execCommand('INIT_FRIENDS', null, function () { + onMessengerReady(); + }); + } }; return MessengerUI; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 1b1824597..b766370c3 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -9,7 +9,6 @@ define([ '/common/common-feedback.js', '/common/common-realtime.js', '/common/common-messaging.js', - '/common/common-messenger.js', '/common/outer/sharedfolder.js', '/common/outer/cursor.js', '/common/outer/onlyoffice.js', @@ -27,8 +26,8 @@ define([ '/bower_components/nthen/index.js', '/bower_components/saferphore/index.js', ], function (Sortify, UserObject, ProxyManager, Migrate, Hash, Util, Constants, Feedback, - Realtime, Messaging, Messenger, - SF, Cursor, OnlyOffice, Mailbox, Profile, Team, _Messenger, + Realtime, Messaging, + SF, Cursor, OnlyOffice, Mailbox, Profile, Team, Messenger, NetConfig, AppConfig, Crypto, ChainPad, CpNetflux, Listmap, nThen, Saferphore) { @@ -1243,14 +1242,6 @@ define([ }); }; - // Messenger - Store.messenger = { - execCommand: function (clientId, data, cb) { - if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); } - store.messenger.execCommand(data, cb); - } - }; - // OnlyOffice Store.onlyoffice = { execCommand: function (clientId, data, cb) { @@ -1755,7 +1746,6 @@ define([ // Clients management var driveEventClients = []; - var messengerEventClients = []; var dropChannel = function (chanId) { try { @@ -1781,10 +1771,6 @@ define([ if (driveIdx !== -1) { driveEventClients.splice(driveIdx, 1); } - var messengerIdx = messengerEventClients.indexOf(clientId); - if (messengerIdx !== -1) { - messengerEventClients.splice(messengerIdx, 1); - } try { store.cursor.removeClient(clientId); } catch (e) { console.error(e); } @@ -1875,28 +1861,6 @@ define([ } }; - var sendMessengerEvent = function (q, data) { - messengerEventClients.forEach(function (cId) { - postMessage(cId, q, data); - }); - }; - Store._subscribeToMessenger = function (clientId) { - if (messengerEventClients.indexOf(clientId) === -1) { - messengerEventClients.push(clientId); - } - }; - var loadMessenger = function () { - if (AppConfig.availablePadTypes.indexOf('contacts') === -1) { return; } - var messenger = store.messenger = Messenger.messenger(store, function () { - broadcast([], "UPDATE_METADATA"); - }); - messenger.on('event', function (ev, data) { - sendMessengerEvent('CHAT_EVENT', { - ev: ev, - data: data - }); - }); - }; /* var loadProfile = function (waitFor) { @@ -2027,10 +1991,10 @@ define([ }); userObject.fixFiles(); SF.loadSharedFolders(Store, store.network, store, userObject, waitFor); - loadMessenger(); loadCursor(); loadOnlyOffice(); - loadUniversal(_Messenger, 'messenger', waitFor); + loadUniversal(Messenger, 'messenger', waitFor); + store.messenger = store.modules['messenger']; loadUniversal(Profile, 'profile', waitFor); loadUniversal(Team, 'team', waitFor); cleanFriendRequests(); diff --git a/www/common/outer/messenger.js b/www/common/outer/messenger.js index 9f3e9aad0..2c951da6b 100644 --- a/www/common/outer/messenger.js +++ b/www/common/outer/messenger.js @@ -160,8 +160,8 @@ define([ var setChannelHead = function (ctx, id, hash, cb) { var channel = ctx.channels[id]; - if (channel.friend) { - var friend = channel.friend; + if (channel.isFriendChat) { + var friend = getFriendFromChannel(ctx, id); if (!friend) { return void cb({error: 'NO_SUCH_FRIEND'}); } friend.lastKnownHash = hash; } else if (channel.isPadChat) { @@ -292,7 +292,7 @@ define([ }; var onDirectMessage = function (ctx, msg, sender) { - var hk = ctx.store.etwork.historyKeeper; + var hk = ctx.store.network.historyKeeper; if (sender !== hk) { return void onIdMessage(ctx, msg, sender); } var parsed = JSON.parse(msg); @@ -356,7 +356,7 @@ define([ orderMessages(channel, decrypted); req.cb(decrypted); - return deleteRangeRequest(txid); + return deleteRangeRequest(ctx, txid); } else { console.log(parsed); } @@ -617,7 +617,7 @@ define([ clients: clientId ? [clientId] : ctx.friendsClients, onReady: cb }; - openChannel(data); + openChannel(ctx, data); }; var initFriends = function (ctx, clientId, cb) { @@ -750,7 +750,7 @@ define([ clients: [clientId], onReady: cb }; - openChannel(chanData); + openChannel(ctx, chanData); }; var clearOwnedChannel = function (ctx, id, cb) { @@ -882,7 +882,6 @@ define([ removeClient(ctx, clientId); }; messenger.execCommand = function (clientId, obj, cb) { - console.log(obj); var cmd = obj.cmd; var data = obj.data; if (cmd === 'INIT_FRIENDS') { diff --git a/www/common/outer/noworker.js b/www/common/outer/noworker.js index a412f2da0..a1507c2d0 100644 --- a/www/common/outer/noworker.js +++ b/www/common/outer/noworker.js @@ -54,9 +54,6 @@ define([ if (cfg.driveEvents) { Rpc._subscribeToDrive(clientId); } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); - } cb(data); }); }); diff --git a/www/common/outer/serviceworker.js b/www/common/outer/serviceworker.js index 54bfecaf5..d9ff9e096 100644 --- a/www/common/outer/serviceworker.js +++ b/www/common/outer/serviceworker.js @@ -75,9 +75,6 @@ var init = function (client, cb) { if (cfg.driveEvents) { Rpc._subscribeToDrive(clientId); } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); - } return void cb(self.store); } @@ -104,9 +101,6 @@ var init = function (client, cb) { if (cfg.driveEvents) { Rpc._subscribeToDrive(clientId); } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); - } if (data && data.state === "ALREADY_INIT") { self.store = data.returned; return void cb(data.returned); diff --git a/www/common/outer/sharedworker.js b/www/common/outer/sharedworker.js index 8f59e603e..706512847 100644 --- a/www/common/outer/sharedworker.js +++ b/www/common/outer/sharedworker.js @@ -75,9 +75,6 @@ var init = function (client, cb) { if (cfg.driveEvents) { Rpc._subscribeToDrive(clientId); } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); - } return void cb(self.store); } @@ -104,9 +101,6 @@ var init = function (client, cb) { if (cfg.driveEvents) { Rpc._subscribeToDrive(clientId); } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); - } if (data && data.state === "ALREADY_INIT") { self.store = data.returned; return void cb(data.returned); diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index 36ec9541c..c75e69b70 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -59,8 +59,6 @@ define([ // Messaging ANSWER_FRIEND_REQUEST: Store.answerFriendRequest, SEND_FRIEND_REQUEST: Store.sendFriendRequest, - // Chat - CHAT_COMMAND: Store.messenger.execCommand, // OnlyOffice OO_COMMAND: Store.onlyoffice.execCommand, // Cursor @@ -100,7 +98,6 @@ define([ // Internal calls Rpc._removeClient = Store._removeClient; Rpc._subscribeToDrive = Store._subscribeToDrive; - Rpc._subscribeToMessenger = Store._subscribeToMessenger; return Rpc; }; diff --git a/www/common/outer/webworker.js b/www/common/outer/webworker.js index f1cc1a3a2..30dff7d0d 100644 --- a/www/common/outer/webworker.js +++ b/www/common/outer/webworker.js @@ -63,9 +63,6 @@ require(['/api/config?cb=' + (+new Date()).toString(16)], function (ApiConfig) { if (cfg.driveEvents) { Rpc._subscribeToDrive(clientId); } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); - } cb(data); }); }); diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 0a233dcf8..a4a452521 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -1091,20 +1091,25 @@ define([ Notifier.getPermission(); sframeChan.on('Q_CHAT_OPENPADCHAT', function (data, cb) { - Cryptpad.messenger.execCommand({ - cmd: 'OPEN_PAD_CHAT', + Cryptpad.universal.execCommand({ + type: 'messenger', data: { - channel: data, - secret: secret + cmd: 'OPEN_PAD_CHAT', + data: { + channel: data, + secret: secret + } } }, cb); }); + /* DEPRECATED sframeChan.on('Q_CHAT_COMMAND', function (data, cb) { Cryptpad.messenger.execCommand(data, cb); }); Cryptpad.messenger.onEvent.reg(function (data) { sframeChan.event('EV_CHAT_EVENT', data); }); + */ } // Chrome 68 on Mac contains a bug resulting in the page turning white after a few seconds