From 477f14bb8b8c092c2147b703f79aac84de3d66ea Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 21 Nov 2017 16:46:19 +0100 Subject: [PATCH] Put constants in a separate file --- customize.dist/main.js | 5 ++-- www/common/common-messaging.js | 9 ++++--- www/common/cryptpad-common.js | 45 ++++++++++++++-------------------- www/common/fsStore.js | 15 ++++++------ www/common/mergeDrive.js | 2 +- www/common/userObject.js | 16 ++++++------ www/drive/inner.js | 37 ++++++---------------------- www/drive/tests.js | 7 +++++- www/register/main.js | 9 ++++--- www/settings/main.js | 25 ++++++++----------- 10 files changed, 73 insertions(+), 97 deletions(-) diff --git a/customize.dist/main.js b/customize.dist/main.js index f0d310e67..a110caaa3 100644 --- a/customize.dist/main.js +++ b/customize.dist/main.js @@ -4,8 +4,9 @@ define([ '/common/cryptpad-common.js', '/common/common-interface.js', '/common/common-realtime.js', + '/common/common-constants.js', '/customize/messages.js', -], function ($, Config, Cryptpad, UI, Realtime, Messages) { +], function ($, Config, Cryptpad, UI, Realtime, Constants, Messages) { window.APP = { Cryptpad: Cryptpad, @@ -33,7 +34,7 @@ define([ $main.find('a[href="/drive/"] div.pad-button-text h4') .text(Messages.main_yourCryptDrive); - var name = localStorage[Cryptpad.userNameKey] || sessionStorage[Cryptpad.userNameKey]; + var name = localStorage[Constants.userNameKey] || sessionStorage[Constants.userNameKey]; var $loggedInBlock = $main.find('#loggedIn'); var $hello = $loggedInBlock.find('#loggedInHello'); var $logout = $loggedInBlock.find('#loggedInLogOut'); diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index d9d376cbc..da5ac004a 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -4,11 +4,12 @@ define([ '/common/curve.js', '/common/common-hash.js', '/common/common-util.js', + '/common/common-constants.js', '/customize/messages.js', '/bower_components/marked/marked.min.js', '/common/common-realtime.js', -], function ($, Crypto, Curve, Hash, Util, Messages, Marked, Realtime) { +], function ($, Crypto, Curve, Hash, Util, Constants, Messages, Marked, Realtime) { var Msg = { inputs: [], }; @@ -88,7 +89,7 @@ define([ if (e) { console.error(e); } }); }); - common.changeDisplayName(proxy[common.displayNameKey]); + common.changeDisplayName(proxy[Constants.displayNameKey]); }; /* Used to accept friend requests within apps other than /contacts/ */ @@ -170,7 +171,7 @@ define([ logText: Messages.contacts_rejected, netfluxId: sender }); - common.changeDisplayName(proxy[common.displayNameKey]); + common.changeDisplayName(proxy[Constants.displayNameKey]); return; } if (msg[0] === "FRIEND_REQ_ACK") { @@ -220,7 +221,7 @@ define([ var proxy = common.getProxy(); // this redraws the userlist after a change has occurred // TODO rename this function to reflect its purpose - common.changeDisplayName(proxy[common.displayNameKey]); + common.changeDisplayName(proxy[Constants.displayNameKey]); } network.sendto(netfluxId, msgStr); }; diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 9428c189a..1d8b73a68 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -9,6 +9,7 @@ define([ '/file/file-crypto.js', '/common/common-realtime.js', '/common/common-language.js', + '/common/common-constants.js', '/common/clipboard.js', '/common/pinpad.js', @@ -17,7 +18,7 @@ define([ '/bower_components/nthen/index.js', '/bower_components/localforage/dist/localforage.min.js', ], function ($, Config, Messages, Store, Util, Hash, - Messaging, FileCrypto, Realtime, Language, Clipboard, + Messaging, FileCrypto, Realtime, Language, Constants, Clipboard, Pinpad, AppConfig, MediaTag, Nthen, localForage) { // Configure MediaTags to use our local viewer @@ -42,14 +43,6 @@ define([ MediaTag: MediaTag, }; - // constants - var userHashKey = common.userHashKey = 'User_hash'; - var userNameKey = common.userNameKey = 'User_name'; - var fileHashKey = common.fileHashKey = 'FS_hash'; - common.displayNameKey = 'cryptpad.username'; - var newPadPathKey = common.newPadPathKey = "newPadPath"; - common.oldStorageKey = 'CryptPad_RECENTPADS'; - common.storageKey = 'filesData'; var PINNING_ENABLED = AppConfig.enablePinning; var store; @@ -158,14 +151,14 @@ define([ common.getDisplayName = function (cb) { var name; if (getProxy()) { - name = getProxy()[common.displayNameKey]; + name = getProxy()[Constants.displayNameKey]; } name = name || ''; if (typeof cb === "function") { cb(null, name); } return name; }; common.getAccountName = function () { - return localStorage[common.userNameKey]; + return localStorage[Constants.userNameKey]; }; // REFACTOR: move to util? @@ -250,8 +243,8 @@ define([ if (!hash) { throw new Error('expected a user hash'); } if (!name) { throw new Error('expected a user name'); } hash = Hash.serializeHash(hash); - localStorage.setItem(userHashKey, hash); - localStorage.setItem(userNameKey, name); + localStorage.setItem(Constants.userHashKey, hash); + localStorage.setItem(Constants.userNameKey, name); if (cb) { cb(); } }; @@ -272,8 +265,8 @@ define([ var logoutHandlers = []; common.logout = function (cb) { [ - userNameKey, - userHashKey, + Constants.userNameKey, + Constants.userHashKey, 'loginToken', 'plan', ].forEach(function (k) { @@ -285,8 +278,8 @@ define([ localForage.clear(); // Make sure we have an FS_hash in localStorage before reloading all the tabs // so that we don't end up with tabs using different anon hashes - if (!localStorage[fileHashKey]) { - localStorage[fileHashKey] = Hash.createRandomHash(); + if (!localStorage[Constants.fileHashKey]) { + localStorage[Constants.fileHashKey] = Hash.createRandomHash(); } eraseTempSessionValues(); @@ -303,16 +296,16 @@ define([ }; var getUserHash = common.getUserHash = function () { - var hash = localStorage[userHashKey]; + var hash = localStorage[Constants.userHashKey]; if (['undefined', 'undefined/'].indexOf(hash) !== -1) { - localStorage.removeItem(userHashKey); + localStorage.removeItem(Constants.userHashKey); return; } if (hash) { var sHash = Hash.serializeHash(hash); - if (sHash !== hash) { localStorage[userHashKey] = sHash; } + if (sHash !== hash) { localStorage[Constants.userHashKey] = sHash; } } return hash; @@ -365,7 +358,7 @@ define([ }; common.setDisplayName = function (value, cb) { if (getProxy()) { - getProxy()[common.displayNameKey] = value; + getProxy()[Constants.displayNameKey] = value; } if (typeof cb === "function") { Realtime.whenRealtimeSyncs(getRealtime(), cb); } }; @@ -716,7 +709,7 @@ define([ var fo = proxy.fo; // start with your userHash... - var userHash = localStorage && localStorage.User_hash; + var userHash = localStorage && localStorage[Constants.userHashKey]; if (!userHash) { return null; } var userParsedHash = Hash.parseTypeHash('drive', userHash); @@ -1070,9 +1063,9 @@ define([ return void setTimeout(function () { f(void 0, env); }); } - if (sessionStorage[newPadPathKey]) { - common.initialPath = sessionStorage[newPadPathKey]; - delete sessionStorage[newPadPathKey]; + if (sessionStorage[Constants.newPadPathKey]) { + common.initialPath = sessionStorage[Constants.newPadPathKey]; + delete sessionStorage[Constants.newPadPathKey]; } var proxy; @@ -1138,7 +1131,7 @@ define([ }; // Listen for login/logout in other tabs window.addEventListener('storage', function (e) { - if (e.key !== common.userHashKey) { return; } + if (e.key !== Constants.userHashKey) { return; } var o = e.oldValue; var n = e.newValue; if (!o && n) { diff --git a/www/common/fsStore.js b/www/common/fsStore.js index 86f17359a..beff0078e 100644 --- a/www/common/fsStore.js +++ b/www/common/fsStore.js @@ -6,8 +6,9 @@ define([ '/common/userObject.js', '/common/common-interface.js', '/common/common-hash.js', + '/common/common-constants.js', '/common/migrate-user-object.js', -], function ($, Listmap, Crypto, TextPatcher, FO, UI, Hash, Migrate) { +], function ($, Listmap, Crypto, TextPatcher, FO, UI, Hash, Constants, Migrate) { /* This module uses localStorage, which is synchronous, but exposes an asyncronous API. This is so that we can substitute other storage @@ -191,7 +192,7 @@ define([ var onReady = function (f, proxy, Cryptpad, exp) { var fo = exp.fo = FO.init(proxy.drive, { Cryptpad: Cryptpad, - rt: exp.realtime + loggedIn: Cryptpad.isLoggedIn() }); var todo = function () { fo.fixFiles(); @@ -258,17 +259,17 @@ define([ return void requestLogin(); } - proxy.on('change', [Cryptpad.displayNameKey], function (o, n) { + proxy.on('change', [Constants.displayNameKey], function (o, n) { if (typeof(n) !== "string") { return; } Cryptpad.changeDisplayName(n); }); proxy.on('change', ['profile'], function () { // Trigger userlist update when the avatar has changed - Cryptpad.changeDisplayName(proxy[Cryptpad.displayNameKey]); + Cryptpad.changeDisplayName(proxy[Constants.displayNameKey]); }); proxy.on('change', ['friends'], function () { // Trigger userlist update when the avatar has changed - Cryptpad.changeDisplayName(proxy[Cryptpad.displayNameKey]); + Cryptpad.changeDisplayName(proxy[Constants.displayNameKey]); }); proxy.on('change', [tokenKey], function () { var localToken = tryParsing(localStorage.getItem(tokenKey)); @@ -318,9 +319,9 @@ define([ if (!rt.proxy.drive || typeof(rt.proxy.drive) !== 'object') { rt.proxy.drive = {}; } var drive = rt.proxy.drive; // Creating a new anon drive: import anon pads from localStorage - if ((!drive[Cryptpad.oldStorageKey] || !Cryptpad.isArray(drive[Cryptpad.oldStorageKey])) + if ((!drive[Constants.oldStorageKey] || !Array.isArray(drive[Constants.oldStorageKey])) && !drive['filesData']) { - drive[Cryptpad.oldStorageKey] = []; + drive[Constants.oldStorageKey] = []; onReady(f, rt.proxy, Cryptpad, exp); return; } diff --git a/www/common/mergeDrive.js b/www/common/mergeDrive.js index d9e9b3987..ad57aef37 100644 --- a/www/common/mergeDrive.js +++ b/www/common/mergeDrive.js @@ -105,7 +105,7 @@ define([ if (parsed) { var proxy = proxyData.proxy; var oldFo = FO.init(parsed.drive, { - Cryptpad: Cryptpad + loggedIn: Cryptpad.isLoggedIn() }); var onMigrated = function () { oldFo.fixFiles(); diff --git a/www/common/userObject.js b/www/common/userObject.js index 799ffdef9..58e45e8ee 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -3,8 +3,10 @@ define([ '/customize/application_config.js', '/common/common-util.js', '/common/common-hash.js', - '/common/common-realtime.js' -], function ($, AppConfig, Util, Hash, Realtime) { + '/common/common-realtime.js', + '/common/common-constants.js', + '/customize/messages.js' +], function ($, AppConfig, Util, Hash, Realtime, Constants, Messages) { var module = {}; var ROOT = module.ROOT = "root"; @@ -20,11 +22,10 @@ define([ module.init = function (files, config) { var exp = {}; var Cryptpad = config.Cryptpad; - var Messages = Cryptpad.Messages; - var loggedIn = config.loggedIn || Cryptpad.isLoggedIn(); + var loggedIn = config.loggedIn; - var FILES_DATA = module.FILES_DATA = exp.FILES_DATA = Cryptpad.storageKey; - var OLD_FILES_DATA = module.OLD_FILES_DATA = exp.OLD_FILES_DATA = Cryptpad.oldStorageKey; + var FILES_DATA = module.FILES_DATA = exp.FILES_DATA = Constants.storageKey; + var OLD_FILES_DATA = module.OLD_FILES_DATA = exp.OLD_FILES_DATA = Constants.oldStorageKey; var NEW_FOLDER_NAME = Messages.fm_newFolder; var NEW_FILE_NAME = Messages.fm_newFile; @@ -487,6 +488,7 @@ define([ // FILES DATA exp.pushData = function (data, cb) { // TODO: can only be called from outside atm + if (!Cryptpad) { return; } if (typeof cb !== "function") { cb = function () {}; } var todo = function () { var id = Util.createRandomInteger(); @@ -855,8 +857,6 @@ define([ } try { debug("Migrating file system..."); - // TODO - Cryptpad.feedback('Migrate-oldFilesData', true); files.migrate = 1; var next = function () { var oldData = files[OLD_FILES_DATA].slice(); diff --git a/www/drive/inner.js b/www/drive/inner.js index 917ac22d2..e105ff47f 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -3,11 +3,11 @@ define([ '/bower_components/textpatcher/TextPatcher.js', '/common/toolbar3.js', 'json.sortify', - '/common/cryptpad-common.js', '/common/common-util.js', '/common/common-hash.js', '/common/common-ui-elements.js', '/common/common-interface.js', + '/common/common-constants.js', '/common/cryptget.js', '/bower_components/nthen/index.js', '/common/sframe-common.js', @@ -25,11 +25,11 @@ define([ TextPatcher, Toolbar, JSONSortify, - Cryptpad, Util, Hash, UIElements, UI, + Constants, Cryptget, nThen, SFCommon, @@ -41,7 +41,6 @@ define([ { var APP = window.APP = { editable: false, - Cryptpad: Cryptpad, mobile: function () { return $('body').width() <= 600; } // Menu and content area are not inline-block anymore for mobiles }; @@ -55,7 +54,7 @@ define([ var SEARCH_NAME = Messages.fm_searchName; var ROOT = "root"; var ROOT_NAME = Messages.fm_rootName; - var FILES_DATA = Cryptpad.storageKey; + var FILES_DATA = Constants.storageKey; var FILES_DATA_NAME = Messages.fm_filesDataName; var TEMPLATE = "template"; var TEMPLATE_NAME = Messages.fm_templateName; @@ -195,13 +194,11 @@ define([ var user = metadataMgr.getUserData(); var isOwnDrive = function () { return true; // TODO - //return Cryptpad.getUserHash() === APP.hash || localStorage.FS_hash === APP.hash; }; var isWorkgroup = function () { return files.workgroup === 1; }; config.workgroup = isWorkgroup(); - config.Cryptpad = Cryptpad; config.loggedIn = APP.loggedIn; APP.origin = priv.origin; @@ -1505,7 +1502,7 @@ define([ .click(function () { var type = $(this).attr('data-type') || 'pad'; var path = filesOp.isPathIn(currentPath, [TRASH]) ? '' : currentPath; - common.sessionStorage.put(Cryptpad.newPadPathKey, path, function () { + common.sessionStorage.put(Constants.newPadPathKey, path, function () { common.openURL('/' + type + '/'); }); }); @@ -2695,8 +2692,10 @@ define([ } else if ($(this).hasClass("cp-app-drive-context-newdoc")) { var type = $(this).data('type') || 'pad'; - sessionStorage[Cryptpad.newPadPathKey] = filesOp.isPathIn(currentPath, [TRASH]) ? '' : currentPath; - window.open(APP.origin + '/' + type + '/'); + var path2 = filesOp.isPathIn(currentPath, [TRASH]) ? '' : currentPath; + common.sessionStorage.put(Constants.newPadPathKey, path2, function () { + common.openURL('/' + type + '/'); + }); } APP.hideMenu(); }); @@ -3008,26 +3007,6 @@ define([ $toolbar: APP.$bar, }; - /* TODO iframe: backup button here? - if (!readOnly && !APP.loggedIn) { - // TODO secure drive - // cryptpad-backup --> cp-toolbar-backup - var $backupButton = Cryptpad.createButton('', true).removeClass('fa').removeClass('fa-question').addClass('cryptpad-backup'); - $backupButton.append($backupIcon.clone().css('marginRight', '0px')); - $backupButton.attr('title', Messages.fm_backup_title); - $backupButton.on('click', function() { - var url = window.location.origin + window.location.pathname + '#' + editHash; - var msg = Messages.fm_alert_backupUrl + ''; - UI.alert(msg, undefined, true); - $('#fm_backupUrl').val(url); - $('#fm_backupUrl').click(function () { - $(this).select(); - }); - }); - $userBlock.append($backupButton); - } - */ - metadataMgr.onChange(function () { var name = metadataMgr.getUserData().name || Messages.anonymous; APP.$displayName.text(name); diff --git a/www/drive/tests.js b/www/drive/tests.js index eaae78e13..23ed2172f 100644 --- a/www/drive/tests.js +++ b/www/drive/tests.js @@ -69,7 +69,12 @@ define([ }; module.test = function (assert) { - var config = {Cryptpad: Cryptpad, workgroup: false, testMode: true}; + var config = { + Cryptpad: Cryptpad, + workgroup: false, + testMode: true, + loggedIn: false + }; // MIGRATION FROM HREF TO ID assert(function (cb) { diff --git a/www/register/main.js b/www/register/main.js index e88ea90ec..41ac6c81b 100644 --- a/www/register/main.js +++ b/www/register/main.js @@ -7,9 +7,10 @@ define([ '/common/common-interface.js', '/common/common-util.js', '/common/common-realtime.js', + '/common/common-constants.js', 'less!/bower_components/components-font-awesome/css/font-awesome.min.css', -], function ($, Login, Cryptpad, Test, Cred, UI, Util, Realtime) { +], function ($, Login, Cryptpad, Test, Cred, UI, Util, Realtime, Constants) { var Messages = Cryptpad.Messages; $(function () { @@ -174,8 +175,8 @@ define([ if (!yes) { return; } proxy.login_name = uname; - if (!proxy[Cryptpad.displayNameKey]) { - proxy[Cryptpad.displayNameKey] = uname; + if (!proxy[Constants.displayNameKey]) { + proxy[Constants.displayNameKey] = uname; } Cryptpad.eraseTempSessionValues(); logMeIn(result); @@ -197,7 +198,7 @@ define([ } proxy.login_name = uname; - proxy[Cryptpad.displayNameKey] = uname; + proxy[Constants.displayNameKey] = uname; sessionStorage.createReadme = 1; logMeIn(result); diff --git a/www/settings/main.js b/www/settings/main.js index a7f8f5ef2..9c4bd42ec 100644 --- a/www/settings/main.js +++ b/www/settings/main.js @@ -36,17 +36,6 @@ define([ }; window.addEventListener('message', onMsg); }).nThen(function (/*waitFor*/) { - /* TODO - window.addEventListener('storage', function (e) { - if (e.key !== Cryptpad.userHashKey) { return; } - var o = e.oldValue; - var n = e.newValue; - window.location.reload(); - if (o && !n) { // disconnect - //redirectToMain(); - } - }); - */ var addRpc = function (sframeChan, Cryptpad, Utils) { sframeChan.on('Q_THUMBNAIL_CLEAR', function (d, cb) { Cryptpad.clearThumbnail(function (err, data) { @@ -58,8 +47,11 @@ define([ }); sframeChan.on('Q_SETTINGS_DRIVE_SET', function (data, cb) { var sjson = JSON.stringify(data); - var k = Cryptpad.getUserHash() || localStorage[Cryptpad.fileHashKey]; - require(['/common/cryptget.js'], function (Crypt) { + require([ + '/common/cryptget.js', + '/common/common-constants.js' + ], function (Crypt, Constants) { + var k = Cryptpad.getUserHash() || localStorage[Constants.fileHashKey]; Crypt.put(k, sjson, function (err) { cb(err); }); @@ -81,8 +73,11 @@ define([ }); sframeChan.on('Q_SETTINGS_IMPORT_LOCAL', function (data, cb) { var proxyData = Cryptpad.getStore().getProxy(); - require(['/common/mergeDrive.js'], function (Merge) { - Merge.anonDriveIntoUser(proxyData, localStorage.FS_hash, cb); + require([ + '/common/mergeDrive.js', + '/common/common-constants.js' + ], function (Merge, Constants) { + Merge.anonDriveIntoUser(proxyData, localStorage[Constants.fileHashKey], cb); }); }); };