Put constants in a separate file

This commit is contained in:
yflory
2017-11-21 16:46:19 +01:00
parent 7f88d1d43a
commit 477f14bb8b
10 changed files with 73 additions and 97 deletions

View File

@@ -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);
};

View File

@@ -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) {

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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();