From 60bea6b33b5a85c713d914e5f01dca7eed2ba0c1 Mon Sep 17 00:00:00 2001 From: yflory Date: Sat, 13 Jul 2019 11:47:58 +0200 Subject: [PATCH] Request capabilities part 2 --- www/common/cryptpad-common.js | 3 ++ www/common/notifications.js | 43 +++++++++++++++++++++++----- www/common/outer/async-store.js | 40 +++++++++++++++++++++++--- www/common/outer/mailbox-handlers.js | 30 +++++++++++++++---- www/common/outer/store-rpc.js | 1 + www/common/sframe-common-outer.js | 6 ++++ www/common/toolbar3.js | 5 ++-- www/notifications/inner.js | 2 +- 8 files changed, 110 insertions(+), 20 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index bd467b764..0311387ee 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -696,6 +696,9 @@ define([ pad.requestAccess = function (data, cb) { postMessage("REQUEST_PAD_ACCESS", data, cb); }; + pad.giveAccess = function (data, cb) { + postMessage("GIVE_PAD_ACCESS", data, cb); + }; common.changePadPassword = function (Crypt, href, newPassword, edPublic, cb) { if (!href) { return void cb({ error: 'EINVAL_HREF' }); } diff --git a/www/common/notifications.js b/www/common/notifications.js index 8aa46fde1..e57228265 100644 --- a/www/common/notifications.js +++ b/www/common/notifications.js @@ -79,15 +79,15 @@ define([ return Messages._getKey(key, [msg.content.name || Messages.anonymous, msg.content.title]); }; content.handler = function () { - var todo = function () { common.openURL(msg.content.href); }; + var todo = function () { + common.openURL(msg.content.href); + defaultDismiss(common, data)(); + }; if (!msg.content.password) { return void todo(); } common.getSframeChannel().query('Q_SESSIONSTORAGE_PUT', { key: 'newPadPassword', value: msg.content.password }, todo); - common.mailbox.dismiss(data, function (err) { - if (err) { return void console.error(err); } - }); }; if (!content.archived) { content.dismissHandler = defaultDismiss(common, data); @@ -103,16 +103,45 @@ define([ // Display the notification content.getFormatText = function () { - return 'Edit access request: ' + msg.content.channel + ' - ' + msg.content.user.displayName; - }; + return 'Edit access request: ' + msg.content.title + ' - ' + msg.content.user.displayName; + }; // XXX // if not archived, add handlers content.handler = function () { UI.confirm("Give edit rights?", function (yes) { if (!yes) { return; } - // XXX Command to worker to get the edit href and send it to msg.content.user + common.getSframeChannel().event('EV_GIVE_ACCESS', { + channel: msg.content.channel, + user: msg.content.user + }); + defaultDismiss(common, data)(); }); }; + + if (!content.archived) { + content.dismissHandler = defaultDismiss(common, data); + } + }; + + handlers['GIVE_PAD_ACCESS'] = function (common, data) { + var content = data.content; + var msg = content.msg; + + // Check authenticity + if (msg.author !== msg.content.user.curvePublic) { return; } + + if (!msg.content.href) { return; } + + // Display the notification + content.getFormatText = function () { + return 'Edit access received: ' + msg.content.title + ' from ' + msg.content.user.displayName; + }; // XXX + + // if not archived, add handlers + content.handler = function () { + common.openURL(msg.content.href); + defaultDismiss(common, data)(); + }; }; return { diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index a4aaf2b5c..ca7e4cd7c 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1260,10 +1260,10 @@ define([ if (i >= 300) { // One minute timeout clearInterval(it); } - i++ + i++; }, 200); return; - }; + } var fData = channel.data || {}; if (fData.owners) { var friends = store.proxy.friends || {}; @@ -1280,9 +1280,7 @@ define([ }); }); if (owner) { - console.log(owner); if (data.send) { - // XXX send the pad title here...? or get it from the recipient's drive var myData = Messaging.createData(store.proxy); delete myData.channel; store.mailbox.sendTo('REQUEST_PAD_ACCESS', { @@ -1302,6 +1300,40 @@ define([ } cb({sent: false}); }; + Store.givePadAccess = function (clientId, data, cb) { + var edPublic = store.proxy.edPublic; + var channel = data.channel; + var res = store.manager.findChannel(channel); + + if (!data.user || !data.user.notifications || !data.user.curvePublic) { + return void cb({error: 'EINVAL'}); + } + + var href, title; + + if (!res.some(function (obj) { + if (obj.data && + Array.isArray(obj.data.owners) && obj.data.owners.indexOf(edPublic) !== -1 && + obj.data.href) { + href = obj.data.href; + title = obj.data.title; + return true; + } + })) { return void cb({error: 'ENOTFOUND'}); } + + var myData = Messaging.createData(store.proxy); + delete myData.channel; + store.mailbox.sendTo("GIVE_PAD_ACCESS", { + channel: channel, + href: href, + title: title, + user: myData + }, { + channel: data.user.notifications, + curvePublic: data.user.curvePublic + }); + cb(); + }; // GET_FULL_HISTORY from sframe-common-outer Store.getFullHistory = function (clientId, data, cb) { diff --git a/www/common/outer/mailbox-handlers.js b/www/common/outer/mailbox-handlers.js index e460233c2..42997b3e0 100644 --- a/www/common/outer/mailbox-handlers.js +++ b/www/common/outer/mailbox-handlers.js @@ -202,9 +202,8 @@ define([ }; // Incoming edit rights request: add data before sending it to inner - handlers['REQUEST_PAD_ACCESS'] = function (ctx, box, data, hash) { + handlers['REQUEST_PAD_ACCESS'] = function (ctx, box, data, cb) { var msg = data.msg; - var hash = data.hash; var content = msg.content; if (msg.author !== content.user.curvePublic) { return void cb(true); } @@ -214,9 +213,9 @@ define([ if (!res.length) { return void cb(true); } - var edPublic = store.proxy.edPublic; + var edPublic = ctx.store.proxy.edPublic; var title; - if (!res.some(functon (obj) { + if (!res.some(function (obj) { if (obj.data && Array.isArray(obj.data.owners) && obj.data.owners.indexOf(edPublic) !== -1 && obj.data.href) { @@ -226,8 +225,29 @@ define([ })) { return void cb(true); } content.title = title; + cb(false); + }; + + handlers['GIVE_PAD_ACCESS'] = function (ctx, box, data, cb) { + var msg = data.msg; + var content = msg.content; + + if (msg.author !== content.user.curvePublic) { return void cb(true); } - }); + var channel = content.channel; + var res = ctx.store.manager.findChannel(channel); + + var title; + res.forEach(function (obj) { + if (obj.data && !obj.data.href) { + if (!title) { title = obj.data.filename || obj.data.title; } + obj.data.href = content.href; + } + }); + + content.title = title || content.title; + cb(false); + }; return { add: function (ctx, box, data, cb) { diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index adfe4115c..bb8e0a674 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -79,6 +79,7 @@ define([ GET_HISTORY_RANGE: Store.getHistoryRange, IS_NEW_CHANNEL: Store.isNewChannel, REQUEST_PAD_ACCESS: Store.requestPadAccess, + GIVE_PAD_ACCESS: Store.givePadAccess, // Drive DRIVE_USEROBJECT: Store.userObjectCommand, // Settings, diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 8c6f79eb2..81ea43d81 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -941,7 +941,13 @@ define([ sframeChan.event('EV_WORKER_TIMEOUT'); }); + sframeChan.on('EV_GIVE_ACCESS', function (data, cb) { + Cryptpad.padRpc.giveAccess(data, cb); + }); sframeChan.on('Q_REQUEST_ACCESS', function (data, cb) { + if (readOnly && hashes.editHash) { + return void cb({error: 'ALREADYKNOWN'}); + } Cryptpad.padRpc.requestAccess({ send: data, channel: secret.channel diff --git a/www/common/toolbar3.js b/www/common/toolbar3.js index 98c2801e7..53aa93f4f 100644 --- a/www/common/toolbar3.js +++ b/www/common/toolbar3.js @@ -589,9 +589,8 @@ MessengerUI, Messages) { }).hide(); // If we have access to the owner's mailbox, display the button and enable it - // XXX the data sent to outer is false if we want to check if we can contact the owner - // and it is true if we change to contact it (action) - // XXX get pad title here?? + // false => check if we can contact the owner + // true ==> send the request Common.getSframeChannel().query('Q_REQUEST_ACCESS', false, function (err, obj) { if (obj && obj.state) { $requestBlock.show().click(function () { diff --git a/www/notifications/inner.js b/www/notifications/inner.js index fd5f69de0..eba1ad7dc 100644 --- a/www/notifications/inner.js +++ b/www/notifications/inner.js @@ -44,7 +44,7 @@ define([ ], }; - var notifsAllowedTypes = ["FRIEND_REQUEST", "FRIEND_REQUEST_ACCEPTED", "FRIEND_REQUEST_DECLINED", "SHARE_PAD"]; + var notifsAllowedTypes = ["FRIEND_REQUEST", "FRIEND_REQUEST_ACCEPTED", "FRIEND_REQUEST_DECLINED", "SHARE_PAD", "REQUEST_PAD_ACCESS"]; var create = {};