From 71b085ba23ee781abd01e37f297527a81bbfa9b0 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 20 Jun 2018 16:39:01 +0200 Subject: [PATCH] Owned drive migration UI --- customize.dist/translations/messages.js | 5 ++ www/common/cryptpad-common.js | 66 +++++++++++++++++++++++++ www/common/sframe-common-outer.js | 4 ++ www/common/sframe-protocol.js | 3 ++ www/settings/inner.js | 44 +++++++++++++++++ 5 files changed, 122 insertions(+) diff --git a/customize.dist/translations/messages.js b/customize.dist/translations/messages.js index 16e837a35..e69835c36 100644 --- a/customize.dist/translations/messages.js +++ b/customize.dist/translations/messages.js @@ -602,6 +602,11 @@ define(function () { out.settings_templateSkip = "Skip the template selection modal"; out.settings_templateSkipHint = "When you create a new empty pad, if you have stored templates for this type of pad, a modal appears to ask if you want to use a template. Here you can choose to never show this modal and so to never use a template."; + out.settings_ownDriveTitle = "Drive migration"; // XXX + out.settings_ownDriveHint = "Migrating your drive to the new version will give you access to new features..."; // XXX + out.settings_ownDriveButton = "Migrate"; // XXX + out.settings_ownDriveConfirm = "Are you sure?"; // XXX + out.upload_title = "File upload"; out.upload_modal_title = "File upload options"; out.upload_modal_filename = "File name (extension {0} added automatically)"; diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index d095d2b3c..f89c27143 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -691,6 +691,72 @@ define([ }); }; + common.ownUserDrive = function (Crypt, edPublic, cb) { + var hash = LocalStore.getUserHash(); + //var href = '/drive/#' + hash; + var secret = Hash.getSecrets('drive', hash); + var newHash, newHref, newSecret; + Nthen(function (waitFor) { + // Check if our drive is already owned + common.anonRpcMsg('GET_METADATA', secret.channel, waitFor(function (err, obj) { + if (err || obj.error) { return; } + if (obj.owners && Array.isArray(obj.owners) && + obj.owners.indexOf(edPublic) !== -1) { + waitFor.abort(); + cb({ + error: 'ALREADY_OWNED' + }); + } + })); + }).nThen(function (waitFor) { + waitFor.abort(); // TODO remove this line + // Create a new user hash + // Get the current content, store it in the new user file + // and make sure the new user drive is owned + newHash = Hash.createRandomHash('drive'); + newHref = '/drive/#' + newHash; + newSecret = Hash.getSecrets('drive', newHash); + + var optsPut = { + owners: [edPublic] + }; + + Crypt.get(hash, waitFor(function (err, val) { + if (err) { + waitFor.abort(); + return void cb({ error: err }); + } + Crypt.put(newHash, val, waitFor(function (err) { + if (err) { + waitFor.abort(); + return void cb({ error: err }); + } + }), optsPut); + })); + }).nThen(function (waitFor) { + // Migration success + // TODO: Replace user hash in login block + }).nThen(function (waitFor) { + // New drive hash is in login block, unpin the old one and pin the new one + common.unpinPads([secret.channel], waitFor()); + common.pinPads([newSecret.channel], waitFor()); + }).nThen(function (waitFor) { + // Login block updated + // TODO: logout everywhere + // * It should wipe localStorage.User_hash, ... + // * login will get the new value from loginBlock and store it in localStorage + // * SharedWorker will reconnect with the new value in other locations + // TODO: then DISCONNECT here + common.logoutFromAll(waitFor(function () { + postMessage("DISCONNECT"); + })); + }).nThen(function () { + // We have the new drive, with the new login block + // TODO: maybe reload automatically? + cb({ state: true }); + }); + }; + // Loading events common.loading = {}; common.loading.onDriveEvent = Util.mkEvent(); diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index c40bff5e3..f82293e8a 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -643,6 +643,10 @@ define([ Cryptpad.changePadPassword(Cryptget, href, data.password, edPublic, cb); }); + sframeChan.on('Q_OWN_USER_DRIVE', function (data, cb) { + Cryptpad.ownUserDrive(Cryptget, edPublic, cb); + }); + sframeChan.on('Q_WRITE_LOGIN_BLOCK', function (data, cb) { Cryptpad.writeLoginBlock(data, cb); }); diff --git a/www/common/sframe-protocol.js b/www/common/sframe-protocol.js index 8bb68aeef..15ef32e86 100644 --- a/www/common/sframe-protocol.js +++ b/www/common/sframe-protocol.js @@ -237,6 +237,9 @@ define({ // Change pad password 'Q_PAD_PASSWORD_CHANGE': true, + // Migrate drive to owned drive + 'Q_OWN_USER_DRIVE': true, + // Loading events to display in the loading screen 'EV_LOADING_INFO': true, // Critical error outside the iframe during loading screen diff --git a/www/settings/inner.js b/www/settings/inner.js index acfa8bafd..bf2fe3bc0 100644 --- a/www/settings/inner.js +++ b/www/settings/inner.js @@ -49,6 +49,7 @@ define([ 'cp-settings-thumbnails', 'cp-settings-userfeedback', 'cp-settings-change-password', + 'cp-settings-migrate', 'cp-settings-delete' ], 'creation': [ @@ -394,6 +395,49 @@ define([ return $div; }; + create['migrate'] = function () { + // TODO + // if (!loginBlock) { return; } + // if (alreadyMigrated) { return; } + if (!common.isLoggedIn()) { return; } + + var $div = $('
', { 'class': 'cp-settings-migrate cp-sidebarlayout-element'}); + + $('', {'class': 'label'}).text(Messages.settings_ownDriveTitle).appendTo($div); + + $('', {'class': 'cp-sidebarlayout-description'}) + .append(Messages.settings_ownDriveHint).appendTo($div); + + var $ok = $('', {'class': 'fa fa-check', title: Messages.saved}); + var $spinner = $('', {'class': 'fa fa-spinner fa-pulse'}); + + var $button = $('