From 3fbb771b9c2e3f6b6f37d2d909689fc5108a38c3 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 31 May 2021 20:10:47 +0530 Subject: [PATCH] add a setting to preserve redirect-to-drive behaviour from the home page disable it by default --- customize.dist/main.js | 2 +- www/common/common-constants.js | 1 + www/common/cryptpad-common.js | 10 +++++++ www/common/outer/async-store.js | 4 +++ www/common/outer/local-store.js | 10 +++++++ www/common/sframe-common-outer.js | 1 + www/settings/inner.js | 50 +++++++++++++++++++++++++++++-- www/settings/main.js | 3 ++ 8 files changed, 78 insertions(+), 3 deletions(-) diff --git a/customize.dist/main.js b/customize.dist/main.js index 44ef51d0d..5969c6c71 100644 --- a/customize.dist/main.js +++ b/customize.dist/main.js @@ -12,7 +12,7 @@ define([ // Make sure we don't display non-translated content (empty button) $main.find('#data').removeClass('hidden'); - if (LocalStore.isLoggedIn()) { + if (LocalStore.isLoggedIn() && LocalStore.getDriveRedirectPreference()) { if (window.location.pathname === '/') { window.location = '/drive/'; return; diff --git a/www/common/common-constants.js b/www/common/common-constants.js index 0a1c1d7f6..fd4d1474b 100644 --- a/www/common/common-constants.js +++ b/www/common/common-constants.js @@ -10,6 +10,7 @@ define(['/customize/application_config.js'], function (AppConfig) { oldStorageKey: 'CryptPad_RECENTPADS', storageKey: 'filesData', tokenKey: 'loginToken', + prefersDriveRedirectKey: 'prefersDriveRedirect', displayPadCreationScreen: 'displayPadCreationScreen', deprecatedKey: 'deprecated', MAX_TEAMS_SLOTS: AppConfig.maxTeamsSlots || 5, diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index e9635d5b5..fd2d29cd2 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -1103,6 +1103,11 @@ define([ postMessage('BURN_PAD', data); }; + common.setDriveRedirectPreference = function (data, cb) { + LocalStore.setDriveRedirectPreference(data && data.value); + cb(); + }; + common.changePadPassword = function (Crypt, Crypto, data, cb) { var href = data.href; var oldPassword = data.oldPassword; @@ -2506,6 +2511,11 @@ define([ } if (data.anonHash && !cfg.userHash) { LocalStore.setFSHash(data.anonHash); } + var prefersDriveRedirect = data[Constants.prefersDriveRedirectKey]; + if (typeof(prefersDriveRedirect) === 'boolean') { + LocalStore.setDriveRedirectPreference(prefersDriveRedirect); + } + initialized = true; channelIsReady(); }); diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 21bc77798..cec0d7d73 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -3167,6 +3167,10 @@ define([ initialized = false; } + var redirect = Constants.prefersDriveRedirectKey; + var redirectPreference = Util.find(store, [ 'proxy', 'settings', 'general', redirect, ]); + ret[redirect] = redirectPreference; + callback(ret); }); diff --git a/www/common/outer/local-store.js b/www/common/outer/local-store.js index 9b62fbbde..fb49d084d 100644 --- a/www/common/outer/local-store.js +++ b/www/common/outer/local-store.js @@ -76,6 +76,16 @@ define([ return window.CP_logged_in || typeof getUserHash() === "string"; }; + LocalStore.getDriveRedirectPreference = function () { + try { + return JSON.parse(localStorage[Constants.redirectToDriveKey]); + } catch (err) { return; } + }; + + LocalStore.setDriveRedirectPreference = function (bool) { + localStorage.setItem(Constants.redirectToDriveKey, Boolean(bool)); + }; + LocalStore.login = function (hash, name, cb) { if (!hash) { throw new Error('expected a user hash'); } if (!name) { throw new Error('expected a user name'); } diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 198d17099..cc1ddf2f5 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -615,6 +615,7 @@ define([ newTemplate: Array.isArray(Cryptpad.initialPath) && Cryptpad.initialPath[0] === "template", feedbackAllowed: Utils.Feedback.state, + prefersDriveRedirect: Utils.LocalStore.getDriveRedirectPreference(), isPresent: parsed.hashData && parsed.hashData.present, isEmbed: parsed.hashData && parsed.hashData.embed, oldVersionHash: parsed.hashData && parsed.hashData.version < 2, // password diff --git a/www/settings/inner.js b/www/settings/inner.js index 24e03a4cf..f979379d7 100644 --- a/www/settings/inner.js +++ b/www/settings/inner.js @@ -14,6 +14,7 @@ define([ '/api/config', '/common/make-backup.js', '/common/common-feedback.js', + '/common/common-constants.js', '/common/jscolor.js', '/bower_components/file-saver/FileSaver.min.js', @@ -35,7 +36,8 @@ define([ AppConfig, ApiConfig, Backup, - Feedback + Feedback, + Constants ) { var saveAs = window.saveAs; var APP = window.APP = {}; @@ -72,7 +74,8 @@ define([ 'cp-settings-thumbnails', 'cp-settings-drive-backup', 'cp-settings-drive-import-local', - 'cp-settings-trim-history' + 'cp-settings-trim-history', + 'cp-settings-redirect', //'cp-settings-drive-reset' ], 'cursor': [ // Msg.settings_cat_cursor @@ -841,6 +844,49 @@ define([ return $div; }; + Messages.settings_driveRedirectTitle = "DRIVE REDIRECT TITLE"; // XXX redirect + Messages.settings_driveRedirectHint = "DRIVE REDIRECT HINT"; // XXX redirect + Messages.settings_driveRedirect = "DRIVE REDIRECT"; // XXX redirect + + create['redirect'] = function () { + if (!common.isLoggedIn()) { return; } + var $div = $('
', { 'class': 'cp-settings-redirect cp-sidebarlayout-element' }); + + $('', { 'class': 'label' }).text(Messages.settings_driveRedirectTitle).appendTo($div); + + $('', { 'class': 'cp-sidebarlayout-description' }) + .append(Messages.settings_driveRedirectHint) + .appendTo($div); + + var $ok = $('', { 'class': 'fa fa-check', title: Messages.saved }); + var $spinner = $('', { 'class': 'fa fa-spinner fa-pulse' }); + + var $cbox = $(UI.createCheckbox('cp-settings-redirect', + Messages.settings_driveRedirect, + false, { label: { class: 'noTitle' } })); + var $checkbox = $cbox.find('input').on('change', function() { + $spinner.show(); + $ok.hide(); + var val = $checkbox.is(':checked') || false; + common.setAttribute(['general', Constants.prefersDriveRedirectKey, val, function() { + $spinner.hide(); + $ok.show(); + sframeChan.query("Q_SET_DRIVE_REDIRECT_PREFERENCE", { + value: val, + }, console.log); + }); + }); + + $cbox.appendTo($div); + + $ok.hide().appendTo($cbox); + $spinner.hide().appendTo($cbox); + + if (privateData.prefersDriveRedirect === true) { + $checkbox[0].checked = true; + } + return $div; + }; create['resettips'] = function() { var $div = $('
', { 'class': 'cp-settings-resettips cp-sidebarlayout-element' }); diff --git a/www/settings/main.js b/www/settings/main.js index 3cfd22fcc..8101203b1 100644 --- a/www/settings/main.js +++ b/www/settings/main.js @@ -78,6 +78,9 @@ define([ } cb(); }); + sframeChan.on('Q_SET_DRIVE_REDIRECT_PREFERENCE', function (data, cb) { + Cryptpad.setDriveRedirectPreference(data, cb); + }); }; var category; if (window.location.hash) {