define([ 'jquery', '/api/config', '/customize/messages.js', '/common/fsStore.js', '/common/common-util.js', '/common/common-hash.js', '/common/common-interface.js', '/common/common-history.js', '/common/common-userlist.js', '/common/common-title.js', '/common/common-metadata.js', '/common/common-messaging.js', '/common/common-codemirror.js', '/common/common-file.js', '/file/file-crypto.js', '/common/clipboard.js', '/common/pinpad.js', '/customize/application_config.js', '/common/media-tag.js', ], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Title, Metadata, Messaging, CodeMirror, Files, FileCrypto, Clipboard, Pinpad, AppConfig, MediaTag) { // Configure MediaTags to use our local viewer if (MediaTag && MediaTag.PdfPlugin) { MediaTag.PdfPlugin.viewer = '/common/pdfjs/web/viewer.html'; } /* This file exposes functionality which is specific to Cryptpad, but not to any particular pad type. This includes functions for committing metadata about pads to your local storage for future use and improved usability. Additionally, there is some basic functionality for import/export. */ var origin = encodeURIComponent(window.location.hostname); var common = window.Cryptpad = { Messages: Messages, Clipboard: Clipboard, donateURL: 'https://accounts.cryptpad.fr/#/donate?on=' + origin, upgradeURL: 'https://accounts.cryptpad.fr/#/?on=' + origin, account: {}, 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 newPadNameKey = common.newPadNameKey = "newPadName"; var newPadPathKey = common.newPadPathKey = "newPadPath"; common.oldStorageKey = 'CryptPad_RECENTPADS'; common.storageKey = 'filesData'; var PINNING_ENABLED = AppConfig.enablePinning; var store; var rpc; var anon_rpc; // import UI elements common.findCancelButton = UI.findCancelButton; common.findOKButton = UI.findOKButton; common.listenForKeys = UI.listenForKeys; common.stopListening = UI.stopListening; common.prompt = UI.prompt; common.confirm = UI.confirm; common.alert = UI.alert; common.log = UI.log; common.warn = UI.warn; common.spinner = UI.spinner; common.addLoadingScreen = UI.addLoadingScreen; common.removeLoadingScreen = UI.removeLoadingScreen; common.errorLoadingScreen = UI.errorLoadingScreen; common.notify = UI.notify; common.unnotify = UI.unnotify; common.getIcon = UI.getIcon; common.addTooltips = UI.addTooltips; common.clearTooltips = UI.clearTooltips; // import common utilities for export common.find = Util.find; var fixHTML = common.fixHTML = Util.fixHTML; common.hexToBase64 = Util.hexToBase64; common.base64ToHex = Util.base64ToHex; var deduplicateString = common.deduplicateString = Util.deduplicateString; common.uint8ArrayToHex = Util.uint8ArrayToHex; common.replaceHash = Util.replaceHash; common.getHash = Util.getHash; common.fixFileName = Util.fixFileName; common.bytesToMegabytes = Util.bytesToMegabytes; common.bytesToKilobytes = Util.bytesToKilobytes; common.fetch = Util.fetch; common.throttle = Util.throttle; common.createRandomInteger = Util.createRandomInteger; common.getAppType = Util.getAppType; common.notAgainForAnother = Util.notAgainForAnother; // import hash utilities for export var createRandomHash = common.createRandomHash = Hash.createRandomHash; common.parseTypeHash = Hash.parseTypeHash; var parsePadUrl = common.parsePadUrl = Hash.parsePadUrl; var isNotStrongestStored = common.isNotStrongestStored = Hash.isNotStrongestStored; var hrefToHexChannelId = common.hrefToHexChannelId = Hash.hrefToHexChannelId; var getRelativeHref = common.getRelativeHref = Hash.getRelativeHref; common.getBlobPathFromHex = Hash.getBlobPathFromHex; common.getEditHashFromKeys = Hash.getEditHashFromKeys; common.getViewHashFromKeys = Hash.getViewHashFromKeys; common.getFileHashFromKeys = Hash.getFileHashFromKeys; common.getUserHrefFromKeys = Hash.getUserHrefFromKeys; common.getSecrets = Hash.getSecrets; common.getHashes = Hash.getHashes; common.createChannelId = Hash.createChannelId; common.findWeaker = Hash.findWeaker; common.findStronger = Hash.findStronger; common.serializeHash = Hash.serializeHash; common.createInviteUrl = Hash.createInviteUrl; // Messaging common.initMessaging = Messaging.init; common.addDirectMessageHandler = Messaging.addDirectMessageHandler; common.inviteFromUserlist = Messaging.inviteFromUserlist; common.createOwnedChannel = Messaging.createOwnedChannel; common.getFriendList = Messaging.getFriendList; common.getFriendChannelsList = Messaging.getFriendChannelsList; common.createData = Messaging.createData; common.getPendingInvites = Messaging.getPending; common.getLatestMessages = Messaging.getLatestMessages; common.initMessagingUI = Messaging.UI.init; // Userlist common.createUserList = UserList.create; // Title common.createTitle = Title.create; // Metadata common.createMetadata = Metadata.create; // CodeMirror common.createCodemirror = CodeMirror.create; // Files common.createFileManager = function (config) { return Files.create(common, config); }; // History common.getHistory = function (config) { return History.create(common, config); }; var getStore = common.getStore = function () { if (store) { return store; } throw new Error("Store is not ready!"); }; var getProxy = common.getProxy = function () { if (store && store.getProxy()) { return store.getProxy().proxy; } }; common.getFO = function () { if (store && store.getProxy()) { return store.getProxy().fo; } }; var getNetwork = common.getNetwork = function () { if (store) { if (store.getProxy() && store.getProxy().info) { return store.getProxy().info.network; } } return; }; common.getUserlist = function () { if (store) { if (store.getProxy() && store.getProxy().info) { return store.getProxy().info.userList; } } return; }; common.getProfileUrl = function () { if (store && store.getProfile()) { return store.getProfile().view; } }; common.getAvatarUrl = function () { if (store && store.getProfile()) { return store.getProfile().avatar; } }; common.getDisplayName = function () { if (getProxy()) { return getProxy()[common.displayNameKey] || ''; } return ''; }; var randomToken = function () { return Math.random().toString(16).replace(/0./, ''); }; var feedback = common.feedback = function (action, force) { if (force !== true) { if (!action) { return; } try { if (!getStore().getProxy().proxy.allowUserFeedback) { return; } } catch (e) { return void console.error(e); } } var href = '/common/feedback.html?' + action + '=' + randomToken(); $.ajax({ type: "HEAD", url: href, }); }; common.reportAppUsage = function () { var pattern = window.location.pathname.split('/') .filter(function (x) { return x; }).join('.'); if (/^#\/1\/view\//.test(window.location.hash)) { feedback(pattern + '_VIEW'); } else { feedback(pattern); } }; common.reportScreenDimensions = function () { var h = window.innerHeight; var w = window.innerWidth; feedback('DIMENSIONS:' + h + 'x' + w); }; common.reportLanguage = function () { feedback('LANG_' + Messages._languageUsed); }; common.getUid = function () { if (store && store.getProxy() && store.getProxy().proxy) { return store.getProxy().proxy.uid; } }; var getRealtime = common.getRealtime = function () { if (store && store.getProxy() && store.getProxy().info) { return store.getProxy().info.realtime; } return; }; common.infiniteSpinnerDetected = false; var BAD_STATE_TIMEOUT = typeof(AppConfig.badStateTimeout) === 'number'? AppConfig.badStateTimeout: 30000; var whenRealtimeSyncs = common.whenRealtimeSyncs = function (realtime, cb) { realtime.sync(); window.setTimeout(function () { if (realtime.getAuthDoc() === realtime.getUserDoc()) { return void cb(); } var to = setTimeout(function () { realtime.abort(); // don't launch more than one popup if (common.infiniteSpinnerDetected) { return; } // inform the user their session is in a bad state common.confirm(Messages.realtime_unrecoverableError, function (yes) { if (!yes) { return; } window.location.reload(); }); common.infiniteSpinnerDetected = true; }, BAD_STATE_TIMEOUT); realtime.onSettle(function () { clearTimeout(to); cb(); }); }, 0); }; common.getWebsocketURL = function () { if (!Config.websocketPath) { return Config.websocketURL; } var path = Config.websocketPath; if (/^ws{1,2}:\/\//.test(path)) { return path; } var protocol = window.location.protocol.replace(/http/, 'ws'); var host = window.location.host; var url = protocol + '//' + host + path; return url; }; common.login = function (hash, name, cb) { if (!hash) { throw new Error('expected a user hash'); } if (!name) { throw new Error('expected a user name'); } hash = common.serializeHash(hash); localStorage.setItem(userHashKey, hash); localStorage.setItem(userNameKey, name); if (cb) { cb(); } }; var eraseTempSessionValues = common.eraseTempSessionValues = function () { // delete sessionStorage values that might have been left over // from the main page's /user redirect [ 'login', 'login_user', 'login_pass', 'login_rmb', 'register' ].forEach(function (k) { delete sessionStorage[k]; }); }; var logoutHandlers = []; common.logout = function (cb) { [ userNameKey, userHashKey, 'loginToken', 'plan', ].forEach(function (k) { sessionStorage.removeItem(k); localStorage.removeItem(k); delete localStorage[k]; delete sessionStorage[k]; }); // 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] = common.createRandomHash(); } eraseTempSessionValues(); logoutHandlers.forEach(function (h) { if (typeof (h) === "function") { h(); } }); if (cb) { cb(); } }; common.onLogout = function (h) { if (typeof (h) !== "function") { return; } if (logoutHandlers.indexOf(h) !== -1) { return; } logoutHandlers.push(h); }; var getUserHash = common.getUserHash = function () { var hash = localStorage[userHashKey]; if (['undefined', 'undefined/'].indexOf(hash) !== -1) { localStorage.removeItem(userHashKey); return; } if (hash) { var sHash = common.serializeHash(hash); if (sHash !== hash) { localStorage[userHashKey] = sHash; } } return hash; }; var isLoggedIn = common.isLoggedIn = function () { return typeof getUserHash() === "string"; }; common.hasSigningKeys = function (proxy) { return typeof(proxy) === 'object' && typeof(proxy.edPrivate) === 'string' && typeof(proxy.edPublic) === 'string'; }; common.hasCurveKeys = function (proxy) { return typeof(proxy) === 'object' && typeof(proxy.curvePrivate) === 'string' && typeof(proxy.curvePublic) === 'string'; }; common.getPublicKeys = function (proxy) { proxy = proxy || common.getProxy(); if (!proxy || !proxy.edPublic || !proxy.curvePublic) { return; } return { curve: proxy.curvePublic, ed: proxy.edPublic, }; }; common.isArray = $.isArray; /* * localStorage formatting */ /* the first time this gets called, your local storage will migrate to a new format. No more indices for values, everything is named now. * href * atime (access time) * title * ??? // what else can we put in here? */ var checkObjectData = function (pad, cb) { if (!pad.ctime) { pad.ctime = pad.atime; } if (/^https*:\/\//.test(pad.href)) { pad.href = common.getRelativeHref(pad.href); } var parsed = common.parsePadUrl(pad.href); if (!parsed || !parsed.hash) { return; } if (typeof(cb) === 'function') { cb(parsed); } if (!pad.title) { pad.title = common.getDefaultName(parsed); } return parsed.hashData; }; // Migrate from legacy store (localStorage) common.migrateRecentPads = function (pads) { return pads.map(function (pad) { var parsedHash; if (Array.isArray(pad)) { // TODO DEPRECATE_F return { href: pad[0], atime: pad[1], title: pad[2] || '', ctime: pad[1], }; } else if (pad && typeof(pad) === 'object') { parsedHash = checkObjectData(pad); if (!parsedHash || !parsedHash.type) { return; } return pad; } else { console.error("[Cryptpad.migrateRecentPads] pad had unexpected value"); console.log(pad); return; } }).filter(function (x) { return x; }); }; // Remove everything from RecentPads that is not an object and check the objects var checkRecentPads = common.checkRecentPads = function (pads) { Object.keys(pads).forEach(function (id, i) { var pad = pads[id]; if (pad && typeof(pad) === 'object') { var parsedHash = checkObjectData(pad); if (!parsedHash || !parsedHash.type) { console.error("[Cryptpad.checkRecentPads] pad had unexpected value", pad); getStore().removeData(i); return; } return pad; } console.error("[Cryptpad.checkRecentPads] pad had unexpected value", pad); getStore().removeData(i); }); }; // Create untitled documents when no name is given var getLocaleDate = common.getLocaleDate = function () { if (window.Intl && window.Intl.DateTimeFormat) { var options = {weekday: "short", year: "numeric", month: "long", day: "numeric"}; return new window.Intl.DateTimeFormat(undefined, options).format(new Date()); } return new Date().toString().split(' ').slice(0,4).join(' '); }; var getDefaultName = common.getDefaultName = function (parsed) { var type = parsed.type; var name = (Messages.type)[type] + ' - ' + getLocaleDate(); return name; }; common.isDefaultName = function (parsed, title) { var name = getDefaultName(parsed); return title === name; }; var makePad = common.makePad = function (href, title) { var now = +new Date(); return { href: href, atime: now, ctime: now, title: title || getDefaultName(parsePadUrl(href)), }; }; /* Sort pads according to how recently they were accessed */ common.mostRecent = function (a, b) { return new Date(b.atime).getTime() - new Date(a.atime).getTime(); }; // STORAGE common.setPadAttribute = function (attr, value, cb) { var href = getRelativeHref(window.location.href); getStore().setPadAttribute(href, attr, value, cb); }; common.setAttribute = function (attr, value, cb) { getStore().set(["cryptpad", attr].join('.'), value, function (err, data) { if (cb) { cb(err, data); } }); }; common.setLSAttribute = function (attr, value) { localStorage[attr] = value; }; // STORAGE common.getPadAttribute = function (attr, cb) { var href = getRelativeHref(window.location.href); getStore().getPadAttribute(href, attr, cb); }; common.getAttribute = function (attr, cb) { getStore().get(["cryptpad", attr].join('.'), function (err, data) { cb(err, data); }); }; common.getLSAttribute = function (attr) { return localStorage[attr]; }; // STORAGE - TEMPLATES var listTemplates = common.listTemplates = function (type) { var allTemplates = getStore().listTemplates(); if (!type) { return allTemplates; } var templates = allTemplates.filter(function (f) { var parsed = parsePadUrl(f.href); return parsed.type === type; }); return templates; }; common.addTemplate = function (data) { getStore().pushData(data, function (e, id) { if (e) { return void console.error("Error while adding a template:", e); } // TODO LIMIT getStore().addPad(id, ['template']); }); }; common.isTemplate = function (href) { var rhref = getRelativeHref(href); var templates = listTemplates(); return templates.some(function (t) { return t.href === rhref; }); }; common.selectTemplate = function (type, rt, Crypt) { if (!AppConfig.enableTemplates) { return; } var temps = listTemplates(type); if (temps.length === 0) { return; } var $content = $('
', {id:"selectTemplate"}).appendTo($content);
common.alert($content.html(), null, true);
var $p = $('#selectTemplate');
temps.forEach(function (t, i) {
$('', {href: t.href, title: t.title}).text(t.title).click(function (e) {
e.preventDefault();
var parsed = parsePadUrl(t.href);
if(!parsed) { throw new Error("Cannot get template hash"); }
common.addLoadingScreen(null, true);
Crypt.get(parsed.hash, function (err, val) {
if (err) { throw new Error(err); }
var p = parsePadUrl(window.location.href);
Crypt.put(p.hash, val, function () {
common.findOKButton().click();
common.removeLoadingScreen();
common.feedback('TEMPLATE_USED');
});
});
}).appendTo($p);
if (i !== temps.length) { $('
').appendTo($p); }
});
common.findOKButton().text(Messages.cancelButton);
};
// STORAGE
/* fetch and migrate your pad history from the store */
var getRecentPads = common.getRecentPads = function (cb) {
getStore().getDrive('filesData', function (err, recentPads) {
if (typeof(recentPads) === "object") {
checkRecentPads(recentPads);
cb(void 0, recentPads);
return;
}
cb(void 0, {});
});
};
// STORAGE: Display Name
common.getLastName = function (cb) {
common.getAttribute('username', function (err, userName) {
cb(err, userName);
});
};
var _onDisplayNameChanged = [];
common.onDisplayNameChanged = function (h) {
if (typeof(h) !== "function") { return; }
if (_onDisplayNameChanged.indexOf(h) !== -1) { return; }
_onDisplayNameChanged.push(h);
};
common.changeDisplayName = function (newName, isLocal) {
_onDisplayNameChanged.forEach(function (h) {
h(newName, isLocal);
});
};
// STORAGE
common.forgetPad = function (href, cb) {
if (typeof(getStore().forgetPad) === "function") {
getStore().forgetPad(common.getRelativeHref(href), cb);
return;
}
cb ("store.forgetPad is not a function");
};
common.setPadTitle = function (name, padHref, cb) {
var href = typeof padHref === "string" ? padHref : window.location.href;
var parsed = parsePadUrl(href);
if (!parsed.hash) { return; }
href = getRelativeHref(href);
// getRecentPads return the array from the drive, not a copy
// We don't have to call "set..." at the end, everything is stored with listmap
getRecentPads(function (err, recent) {
if (err) {
cb(err);
return;
}
var updateWeaker = [];
var contains;
Object.keys(recent).forEach(function (id) {
var pad = recent[id];
var p = parsePadUrl(pad.href);
if (p.type !== parsed.type) { return pad; }
var shouldUpdate = p.hash.replace(/\/$/, '') === parsed.hash.replace(/\/$/, '');
// Version 1 : we have up to 4 differents hash for 1 pad, keep the strongest :
// Edit > Edit (present) > View > View (present)
var pHash = p.hashData;
var parsedHash = parsed.hashData;
if (!pHash) { return; } // We may have a corrupted pad in our storage, abort here in that case
if (!shouldUpdate && pHash.version === 1 && parsedHash.version === 1 && pHash.channel === parsedHash.channel) {
if (pHash.mode === 'view' && parsedHash.mode === 'edit') { shouldUpdate = true; }
else if (pHash.mode === parsedHash.mode && pHash.present) { shouldUpdate = true; }
else {
// Editing a "weaker" version of a stored hash : update the date and do not push the current hash
pad.atime = +new Date();
contains = true;
return pad;
}
}
if (shouldUpdate) {
contains = true;
// update the atime
pad.atime = +new Date();
// set the name
pad.title = name;
// If we now have a stronger version of a stored href, replace the weaker one by the strong one
if (pad && pad.href && href !== pad.href) {
updateWeaker.push({
o: pad.href,
n: href
});
}
pad.href = href;
}
return pad;
});
if (updateWeaker.length > 0) {
updateWeaker.forEach(function (obj) {
// If we have a stronger url, and if all the occurences of the weaker were
// in the trash, add remove them from the trash and add the stronger in root
getStore().restoreHref(obj.n);
});
}
if (!contains && href) {
var data = makePad(href, name);
getStore().pushData(data, function (e, id) {
if (e) {
if (e === 'E_OVER_LIMIT') {
common.alert(Messages.pinLimitNotPinned, null, true);
}
return void cb(e);
}
getStore().addPad(id, common.initialPath);
cb(err, recent);
});
return;
}
cb(err, recent);
});
};
var errorHandlers = [];
common.onError = function (h) {
if (typeof h !== "function") { return; }
errorHandlers.push(h);
};
common.storeError = function () {
errorHandlers.forEach(function (h) {
if (typeof h === "function") {
h({type: "store"});
}
});
};
/*
* Buttons
*/
common.renamePad = function (title, href, callback) {
if (title === null) { return; }
if (title.trim() === "") {
var parsed = parsePadUrl(href || window.location.href);
title = getDefaultName(parsed);
}
common.setPadTitle(title, href, function (err) {
if (err) {
console.log("unable to set pad title");
console.error(err);
return;
}
callback(null, title);
});
};
common.getUserFilesList = function () {
var store = common.getStore();
var proxy = store.getProxy();
var fo = proxy.fo;
var hashes = [];
var list = fo.getFiles().filter(function (id) {
var href = fo.getFileData(id).href;
var parsed = parsePadUrl(href);
if ((parsed.type === 'file' || parsed.type === 'media')
&& hashes.indexOf(parsed.hash) === -1) {
hashes.push(parsed.hash);
return true;
}
});
return list;
};
var getUserChannelList = common.getUserChannelList = function () {
var store = common.getStore();
var proxy = store.getProxy();
var fo = proxy.fo;
// start with your userHash...
var userHash = localStorage && localStorage.User_hash;
if (!userHash) { return null; }
var userParsedHash = common.parseTypeHash('drive', userHash);
var userChannel = userParsedHash && userParsedHash.channel;
if (!userChannel) { return null; }
var list = fo.getFiles([fo.FILES_DATA]).map(function (id) {
return hrefToHexChannelId(fo.getFileData(id).href);
})
.filter(function (x) { return x; });
// Get the avatar
var profile = store.getProfile();
if (profile) {
var profileChan = profile.edit ? hrefToHexChannelId('/profile/#' + profile.edit) : null;
if (profileChan) { list.push(profileChan); }
var avatarChan = profile.avatar ? hrefToHexChannelId(profile.avatar) : null;
if (avatarChan) { list.push(avatarChan); }
}
if (getProxy().friends) {
var fList = common.getFriendChannelsList(common);
list = list.concat(fList);
}
list.push(common.base64ToHex(userChannel));
list.sort();
return list;
};
var getCanonicalChannelList = common.getCanonicalChannelList = function () {
return deduplicateString(getUserChannelList()).sort();
};
var pinsReady = common.pinsReady = function () {
if (!isLoggedIn()) {
return false;
}
if (!PINNING_ENABLED) {
console.error('[PINNING_DISABLED]');
return false;
}
if (!rpc) {
console.error('RPC_NOT_READY');
return false;
}
return true;
};
common.arePinsSynced = function (cb) {
if (!pinsReady()) { return void cb ('RPC_NOT_READY'); }
var list = getCanonicalChannelList();
var local = Hash.hashChannelList(list);
rpc.getServerHash(function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash === local);
});
};
common.resetPins = function (cb) {
if (!pinsReady()) { return void cb ('RPC_NOT_READY'); }
var list = getCanonicalChannelList();
rpc.reset(list, function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash);
});
};
common.pinPads = function (pads, cb) {
if (!pinsReady()) { return void cb ('RPC_NOT_READY'); }
rpc.pin(pads, function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash);
});
};
common.unpinPads = function (pads, cb) {
if (!pinsReady()) { return void cb ('RPC_NOT_READY'); }
rpc.unpin(pads, function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash);
});
};
common.getPinnedUsage = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
rpc.getFileListSize(function (err, bytes) {
if (typeof(bytes) === 'number') {
common.account.usage = bytes;
}
cb(err, bytes);
});
};
common.getFileSize = function (href, cb) {
if (!anon_rpc) { return void cb('ANON_RPC_NOT_READY'); }
//if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
var channelId = Hash.hrefToHexChannelId(href);
anon_rpc.send("GET_FILE_SIZE", channelId, function (e, response) {
if (e) { return void cb(e); }
if (response && response.length && typeof(response[0]) === 'number') {
return void cb(void 0, response[0]);
} else {
cb('INVALID_RESPONSE');
}
});
};
common.getMultipleFileSize = function (files, cb) {
if (!anon_rpc) { return void cb('ANON_RPC_NOT_READY'); }
if (!Array.isArray(files)) {
return void setTimeout(function () { cb('INVALID_FILE_LIST'); });
}
anon_rpc.send('GET_MULTIPLE_FILE_SIZE', files, function (e, res) {
if (e) { return cb(e); }
if (res && res.length && typeof(res[0]) === 'object') {
cb(void 0, res[0]);
} else {
cb('UNEXPECTED_RESPONSE');
}
});
};
common.updatePinLimit = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
rpc.updatePinLimits(function (e, limit, plan, note) {
if (e) { return cb(e); }
common.account.limit = limit;
common.account.plan = plan;
common.account.note = note;
cb(e, limit, plan, note);
});
};
common.getPinLimit = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
var account = common.account;
var ALWAYS_REVALIDATE = true;
if (ALWAYS_REVALIDATE || typeof(account.limit) !== 'number' ||
typeof(account.plan) !== 'string' ||
typeof(account.note) !== 'string') {
return void rpc.getLimit(function (e, limit, plan, note) {
if (e) { return cb(e); }
common.account.limit = limit;
common.account.plan = plan;
common.account.note = note;
cb(void 0, limit, plan, note);
});
}
cb(void 0, account.limit, account.plan, account.note);
};
common.isOverPinLimit = function (cb) {
if (!common.isLoggedIn()) { return void cb(null, false); }
var usage;
var andThen = function (e, limit, plan) {
if (e) { return void cb(e); }
var data = {usage: usage, limit: limit, plan: plan};
if (usage > limit) {
return void cb (null, true, data);
}
return void cb (null, false, data);
};
var todo = function (e, used) {
if (e) { return void cb(e); }
usage = used;
common.getPinLimit(andThen);
};
common.getPinnedUsage(todo);
};
common.clearOwnedChannel = function (channel, cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
rpc.clearOwnedChannel(channel, cb);
};
common.uploadComplete = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
rpc.uploadComplete(cb);
};
common.uploadStatus = function (size, cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
rpc.uploadStatus(size, cb);
};
common.uploadCancel = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
rpc.uploadCancel(cb);
};
/* Create a usage bar which keeps track of how much storage space is used
by your CryptDrive. The getPinnedUsage RPC is one of the heavier calls,
so we throttle its usage. Clients will not update more than once per
LIMIT_REFRESH_RATE. It will be update at least once every three such intervals
If changes are made to your drive in the interim, they will trigger an
update.
*/
var LIMIT_REFRESH_RATE = 30000; // milliseconds
common.createUsageBar = function (cb) {
// getPinnedUsage updates common.account.usage, and other values
// so we can just use those and only check for errors
var $container = $('', {'class':'limit-container'});
var todo;
var updateUsage = window.updateUsage = common.notAgainForAnother(function () {
common.getPinnedUsage(todo);
}, LIMIT_REFRESH_RATE);
todo = function (err) {
if (err) { return void console.error(err); }
$container.html('');
var unit = Util.magnitudeOfBytes(common.account.limit);
var usage = unit === 'GB'? Util.bytesToGigabytes(common.account.usage):
Util.bytesToMegabytes(common.account.usage);
var limit = unit === 'GB'? Util.bytesToGigabytes(common.account.limit):
Util.bytesToMegabytes(common.account.limit);
var $limit = $('', {'class': 'cryptpad-limit-bar'}).appendTo($container);
var quota = usage/limit;
var $usage = $('', {'class': 'usage'}).css('width', quota*100+'%');
var makeDonateButton = function () {
$('', {
'class': 'upgrade btn btn-success',
href: common.donateURL,
rel: "noreferrer noopener",
target: "_blank",
}).text(Messages.supportCryptpad).appendTo($container);
};
var makeUpgradeButton = function () {
$('', {
'class': 'upgrade btn btn-success',
href: common.upgradeURL,
rel: "noreferrer noopener",
target: "_blank",
}).text(Messages.upgradeAccount).appendTo($container);
};
if (!Config.removeDonateButton) {
if (!common.isLoggedIn() || !Config.allowSubscriptions) {
// user is not logged in, or subscriptions are disallowed
makeDonateButton();
} else if (!common.account.plan) {
// user is logged in and subscriptions are allowed
// and they don't have one. show upgrades
makeUpgradeButton();
} else {
// they have a plan. show nothing
}
}
var prettyUsage;
var prettyLimit;
if (unit === 'GB') {
prettyUsage = Messages._getKey('formattedGB', [usage]);
prettyLimit = Messages._getKey('formattedGB', [limit]);
} else {
prettyUsage = Messages._getKey('formattedMB', [usage]);
prettyLimit = Messages._getKey('formattedMB', [limit]);
}
if (quota < 0.8) { $usage.addClass('normal'); }
else if (quota < 1) { $usage.addClass('warning'); }
else { $usage.addClass('above'); }
var $text = $('', {'class': 'usageText'});
$text.text(usage + ' / ' + prettyLimit);
$limit.append($usage).append($text);
};
setInterval(function () {
updateUsage();
}, LIMIT_REFRESH_RATE * 3);
updateUsage();
getProxy().on('change', ['drive'], function () {
updateUsage();
});
cb(null, $container);
};
var prepareFeedback = common.prepareFeedback = function (key) {
if (typeof(key) !== 'string') { return $.noop; }
var type = common.getAppType();
return function () {
feedback((key + (type? '_' + type: '')).toUpperCase());
};
};
common.createButton = function (type, rightside, data, callback) {
var button;
var size = "17px";
switch (type) {
case 'export':
button = $('