cryptpad/www/common/cryptpad-common.js

1004 lines
34 KiB
JavaScript
Raw Normal View History

2016-05-28 11:13:54 +00:00
define([
'jquery',
'/api/config',
2017-04-13 10:18:08 +00:00
'/customize/messages.js',
'/common/fsStore.js',
'/common/common-util.js',
2017-04-13 15:04:15 +00:00
'/common/common-hash.js',
2017-07-05 16:42:32 +00:00
'/common/common-messaging.js',
2017-08-11 08:59:54 +00:00
'/common/common-realtime.js',
'/common/common-language.js',
2017-11-21 15:46:19 +00:00
'/common/common-constants.js',
2017-11-23 11:28:49 +00:00
'/common/common-feedback.js',
'/common/outer/local-store.js',
'/common/pinpad.js',
2017-07-05 09:57:53 +00:00
'/customize/application_config.js',
2017-09-14 09:23:37 +00:00
'/bower_components/nthen/index.js',
], function ($, Config, Messages, Store, Util, Hash,
2017-11-23 11:28:49 +00:00
Messaging, Realtime, Language, Constants, Feedback, LocalStore,
Pinpad, AppConfig, Nthen) {
/* 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.
*/
2017-06-09 08:46:11 +00:00
var origin = encodeURIComponent(window.location.hostname);
2016-12-21 17:06:05 +00:00
var common = window.Cryptpad = {
2016-10-24 09:39:28 +00:00
Messages: Messages,
2017-06-09 08:46:11 +00:00
donateURL: 'https://accounts.cryptpad.fr/#/donate?on=' + origin,
upgradeURL: 'https://accounts.cryptpad.fr/#/?on=' + origin,
account: {},
};
var PINNING_ENABLED = AppConfig.enablePinning;
var store;
var rpc;
var anon_rpc;
var getStore = common.getStore = function () {
if (store) { return store; }
2016-08-30 16:09:53 +00:00
throw new Error("Store is not ready!");
};
var getProxy = common.getProxy = function () {
if (store && store.getProxy()) {
return store.getProxy().proxy;
}
};
2017-06-15 09:04:55 +00:00
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;
};
2017-11-13 15:32:40 +00:00
// REFACTOR pull language directly
common.getLanguage = function () {
return Messages._languageUsed;
};
common.setLanguage = function (l, cb) {
Language.setLanguage(l, null, cb);
};
2017-11-13 15:32:40 +00:00
// REAFCTOR store.getProfile should be store.get(['profile'])
2017-06-30 14:57:23 +00:00
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 (cb) {
var name;
2017-07-07 16:53:21 +00:00
if (getProxy()) {
2017-11-21 15:46:19 +00:00
name = getProxy()[Constants.displayNameKey];
2017-07-07 16:53:21 +00:00
}
name = name || '';
if (typeof cb === "function") { cb(null, name); }
return name;
2017-07-07 16:53:21 +00:00
};
2017-06-22 15:42:24 +00:00
2017-05-04 14:16:09 +00:00
common.getUid = function () {
2017-04-14 13:32:12 +00:00
if (store && store.getProxy() && store.getProxy().proxy) {
return store.getProxy().proxy.uid;
}
};
2017-02-24 14:22:26 +00:00
var getRealtime = common.getRealtime = function () {
2017-04-14 13:32:12 +00:00
if (store && store.getProxy() && store.getProxy().info) {
2017-02-24 14:22:26 +00:00
return store.getProxy().info.realtime;
}
return;
};
2017-05-04 14:16:09 +00:00
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';
};
2017-07-10 07:21:27 +00:00
common.getPublicKeys = function (proxy) {
proxy = proxy || common.getProxy();
if (!proxy || !proxy.edPublic || !proxy.curvePublic) { return; }
return {
curve: proxy.curvePublic,
ed: proxy.edPublic,
};
};
2017-06-08 15:52:00 +00:00
var makePad = common.makePad = function (href, title) {
var now = +new Date();
2016-08-30 16:09:53 +00:00
return {
href: href,
atime: now,
ctime: now,
2017-11-21 10:09:59 +00:00
title: title || Hash.getDefaultName(Hash.parsePadUrl(href)),
2016-08-30 16:09:53 +00:00
};
};
// STORAGE
2017-10-09 09:52:34 +00:00
common.setPadAttribute = function (attr, value, cb, href) {
2017-11-13 15:32:40 +00:00
href = Hash.getRelativeHref(href || window.location.href);
2017-07-05 10:21:01 +00:00
getStore().setPadAttribute(href, attr, value, cb);
2016-07-22 10:24:54 +00:00
};
common.setDisplayName = function (value, cb) {
if (getProxy()) {
2017-11-21 15:46:19 +00:00
getProxy()[Constants.displayNameKey] = value;
}
if (typeof cb === "function") { Realtime.whenRealtimeSyncs(getRealtime(), cb); }
};
2017-05-04 14:16:09 +00:00
common.setAttribute = function (attr, value, cb) {
getStore().setAttribute(attr, value, function (err, data) {
2017-06-22 14:14:54 +00:00
if (cb) { cb(err, data); }
2016-09-22 15:12:46 +00:00
});
};
2016-07-22 10:24:54 +00:00
2016-08-30 16:09:53 +00:00
// STORAGE
2017-05-04 14:16:09 +00:00
common.getPadAttribute = function (attr, cb) {
2017-11-13 15:32:40 +00:00
var href = Hash.getRelativeHref(window.location.href);
2017-07-05 10:21:01 +00:00
getStore().getPadAttribute(href, attr, cb);
2016-07-22 10:24:54 +00:00
};
2017-05-04 14:16:09 +00:00
common.getAttribute = function (attr, cb) {
getStore().getAttribute(attr, function (err, data) {
2016-09-22 15:12:46 +00:00
cb(err, data);
});
};
2017-08-31 16:06:26 +00:00
2017-11-07 13:51:53 +00:00
2017-08-31 16:06:26 +00:00
/* this returns a reference to your proxy. changing it will change your drive.
*/
var getFileEntry = common.getFileEntry = function (href, cb) {
if (typeof(cb) !== 'function') { return; }
var store = getStore();
if (!store) { return void cb('NO_STORE'); }
href = href || (window.location.pathname + window.location.hash);
var id = store.getIdFromHref(href);
if (!id) { return void cb('NO_ID'); }
2017-11-13 15:32:40 +00:00
var entry = Util.find(getProxy(), [
2017-08-31 16:06:26 +00:00
'drive',
'filesData',
id
]);
cb(void 0, entry);
};
2017-09-19 13:30:08 +00:00
common.resetTags = function (href, tags, cb) {
cb = cb || $.noop;
if (!Array.isArray(tags)) { return void cb('INVALID_TAGS'); }
getFileEntry(href, function (e, entry) {
if (e) { return void cb(e); }
if (!entry) { cb('NO_ENTRY'); }
entry.tags = tags.slice();
cb();
});
};
2017-08-31 16:06:26 +00:00
common.tagPad = function (href, tag, cb) {
if (typeof(cb) !== 'function') {
return void console.error('EXPECTED_CALLBACK');
}
if (typeof(tag) !== 'string') { return void cb('INVALID_TAG'); }
getFileEntry(href, function (e, entry) {
if (e) { return void cb(e); }
if (!entry) { cb('NO_ENTRY'); }
if (!entry.tags) {
entry.tags = [tag];
} else if (entry.tags.indexOf(tag) === -1) {
entry.tags.push(tag);
}
cb();
});
};
common.untagPad = function (href, tag, cb) {
if (typeof(cb) !== 'function') {
return void console.error('EXPECTED_CALLBACK');
}
if (typeof(tag) !== 'string') { return void cb('INVALID_TAG'); }
getFileEntry(href, function (e, entry) {
if (e) { return void cb(e); }
if (!entry) { cb('NO_ENTRY'); }
if (!entry.tags) { return void cb(); }
var idx = entry.tags.indexOf(tag);
if (idx === -1) { return void cb(); }
entry.tags.splice(idx, 1);
cb();
});
};
common.getPadTags = function (href, cb) {
if (typeof(cb) !== 'function') { return; }
getFileEntry(href, function (e, entry) {
if (entry) {
return void cb(void 0, entry.tags?
JSON.parse(JSON.stringify(entry.tags)): []);
}
return cb('NO_ENTRY');
});
};
2017-09-05 14:52:22 +00:00
common.listAllTags = function (cb) {
var all = [];
var proxy = getProxy();
2017-11-13 15:32:40 +00:00
var files = Util.find(proxy, ['drive', 'filesData']);
2017-09-05 14:52:22 +00:00
if (typeof(files) !== 'object') { return cb('invalid_drive'); }
Object.keys(files).forEach(function (k) {
var file = files[k];
if (!Array.isArray(file.tags)) { return; }
file.tags.forEach(function (tag) {
if (all.indexOf(tag) === -1) {
all.push(tag);
}
});
});
cb(void 0, all);
};
// STORAGE - TEMPLATES
var listTemplates = common.listTemplates = function (type) {
var allTemplates = getStore().listTemplates();
if (!type) { return allTemplates; }
var templates = allTemplates.filter(function (f) {
2017-11-13 15:32:40 +00:00
var parsed = Hash.parsePadUrl(f.href);
return parsed.type === type;
});
return templates;
};
2017-05-04 14:16:09 +00:00
common.addTemplate = function (data) {
2017-06-08 15:52:00 +00:00
getStore().pushData(data, function (e, id) {
if (e) { return void console.error("Error while adding a template:", e); } // TODO LIMIT
getStore().addPad(id, ['template']);
});
};
2017-05-04 14:16:09 +00:00
common.isTemplate = function (href) {
2017-11-13 15:32:40 +00:00
var rhref = Hash.getRelativeHref(href);
var templates = listTemplates();
return templates.some(function (t) {
return t.href === rhref;
});
};
2017-09-05 09:35:15 +00:00
// Secure iframes
common.useTemplate = function (href, Crypt, cb) {
2017-11-13 15:32:40 +00:00
var parsed = Hash.parsePadUrl(href);
2017-09-05 09:35:15 +00:00
if(!parsed) { throw new Error("Cannot get template hash"); }
Crypt.get(parsed.hash, function (err, val) {
if (err) { throw new Error(err); }
2017-11-13 15:32:40 +00:00
var p = Hash.parsePadUrl(window.location.href);
2017-09-05 09:35:15 +00:00
Crypt.put(p.hash, val, cb);
});
};
2016-07-22 10:24:54 +00:00
2016-08-30 16:09:53 +00:00
// STORAGE
2017-04-11 12:53:44 +00:00
/* fetch and migrate your pad history from the store */
var getRecentPads = common.getRecentPads = function (cb) {
2017-06-08 15:52:00 +00:00
getStore().getDrive('filesData', function (err, recentPads) {
if (typeof(recentPads) === "object") {
2017-04-11 12:53:44 +00:00
cb(void 0, recentPads);
return;
}
2017-06-08 15:52:00 +00:00
cb(void 0, {});
});
};
// STORAGE: Display Name
common.getLastName = common.getDisplayName;
/* function (cb) {
common.getDisplayName(function (err, userName) {
cb(err, userName);
});
};*/
var _onDisplayNameChanged = [];
2017-05-04 14:16:09 +00:00
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);
});
};
2016-08-30 16:09:53 +00:00
// STORAGE
2017-05-04 14:16:09 +00:00
common.forgetPad = function (href, cb) {
if (typeof(getStore().forgetPad) === "function") {
2017-11-13 15:32:40 +00:00
getStore().forgetPad(Hash.getRelativeHref(href), cb);
2017-06-08 15:52:00 +00:00
return;
2016-11-17 17:27:55 +00:00
}
2017-06-08 15:52:00 +00:00
cb ("store.forgetPad is not a function");
};
common.setPadTitle = function (name, padHref, cb) {
2017-06-21 16:04:35 +00:00
var href = typeof padHref === "string" ? padHref : window.location.href;
2017-11-13 15:32:40 +00:00
var parsed = Hash.parsePadUrl(href);
if (!parsed.hash) { return; }
href = parsed.getUrl({present: parsed.present});
2017-11-13 15:32:40 +00:00
//href = Hash.getRelativeHref(href);
2017-04-11 12:53:44 +00:00
// 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;
2017-06-08 15:52:00 +00:00
Object.keys(recent).forEach(function (id) {
var pad = recent[id];
2017-11-13 15:32:40 +00:00
var p = Hash.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) {
2016-11-17 17:27:55 +00:00
var data = makePad(href, name);
2017-06-08 15:52:00 +00:00
getStore().pushData(data, function (e, id) {
if (e) {
return void cb(e);
}
2017-06-08 15:52:00 +00:00
getStore().addPad(id, common.initialPath);
cb(err, recent);
});
return;
2017-04-11 12:53:44 +00:00
}
cb(err, recent);
});
2016-07-11 15:36:53 +00:00
};
2016-09-27 16:33:03 +00:00
/*
* Buttons
*/
common.renamePad = function (title, href, callback) {
2016-10-21 16:16:27 +00:00
if (title === null) { return; }
if (title.trim() === "") {
2017-11-13 15:32:40 +00:00
var parsed = Hash.parsePadUrl(href || window.location.href);
2017-11-21 10:09:59 +00:00
title = Hash.getDefaultName(parsed);
}
common.setPadTitle(title, href, function (err) {
if (err) {
console.log("unable to set pad title");
2017-06-13 14:15:04 +00:00
console.error(err);
return;
}
callback(null, title);
});
2016-10-21 16:16:27 +00:00
};
2017-03-15 14:55:55 +00:00
// Needed for the secure filepicker app
common.getSecureFilesList = function (query, cb) {
var store = common.getStore();
if (!store) { return void cb("Store is not ready"); }
var proxy = store.getProxy();
var fo = proxy.fo;
var list = {};
var hashes = [];
var types = query.types;
var where = query.where;
var filter = query.filter || {};
var isFiltered = function (type, data) {
var filtered;
var fType = filter.fileType || [];
if (type === 'file' && fType.length) {
if (!data.fileType) { return true; }
filtered = !fType.some(function (t) {
return data.fileType.indexOf(t) === 0;
});
}
return filtered;
};
2017-09-05 09:35:15 +00:00
fo.getFiles(where).forEach(function (id) {
var data = fo.getFileData(id);
2017-11-13 15:32:40 +00:00
var parsed = Hash.parsePadUrl(data.href);
2017-09-05 09:35:15 +00:00
if ((!types || types.length === 0 || types.indexOf(parsed.type) !== -1)
&& hashes.indexOf(parsed.hash) === -1) {
if (isFiltered(parsed.type, data)) { return; }
hashes.push(parsed.hash);
list[id] = data;
}
});
cb (null, list);
};
2017-06-15 09:04:55 +00:00
2017-03-15 14:55:55 +00:00
var getUserChannelList = common.getUserChannelList = function () {
var store = common.getStore();
var proxy = store.getProxy();
var fo = proxy.fo;
2017-03-16 13:43:57 +00:00
// start with your userHash...
2017-11-21 15:46:19 +00:00
var userHash = localStorage && localStorage[Constants.userHashKey];
2017-03-16 13:43:57 +00:00
if (!userHash) { return null; }
2017-11-13 15:32:40 +00:00
var userParsedHash = Hash.parseTypeHash('drive', userHash);
var userChannel = userParsedHash && userParsedHash.channel;
2017-03-16 13:43:57 +00:00
if (!userChannel) { return null; }
2017-06-08 15:52:00 +00:00
var list = fo.getFiles([fo.FILES_DATA]).map(function (id) {
2017-11-13 15:32:40 +00:00
return Hash.hrefToHexChannelId(fo.getFileData(id).href);
2017-06-08 15:52:00 +00:00
})
2017-04-10 15:42:35 +00:00
.filter(function (x) { return x; });
2017-03-16 13:43:57 +00:00
2017-06-28 08:59:29 +00:00
// Get the avatar
var profile = store.getProfile();
if (profile) {
2017-11-13 15:32:40 +00:00
var profileChan = profile.edit ? Hash.hrefToHexChannelId('/profile/#' + profile.edit) : null;
2017-06-28 08:59:29 +00:00
if (profileChan) { list.push(profileChan); }
2017-11-13 15:32:40 +00:00
var avatarChan = profile.avatar ? Hash.hrefToHexChannelId(profile.avatar) : null;
2017-06-28 08:59:29 +00:00
if (avatarChan) { list.push(avatarChan); }
}
2017-07-10 08:39:57 +00:00
if (getProxy().friends) {
var fList = Messaging.getFriendChannelsList(common);
2017-07-10 08:39:57 +00:00
list = list.concat(fList);
}
2017-11-13 15:32:40 +00:00
list.push(Util.base64ToHex(userChannel));
2017-03-16 13:43:57 +00:00
list.sort();
2017-03-15 14:55:55 +00:00
return list;
};
2017-04-07 14:33:14 +00:00
var getCanonicalChannelList = common.getCanonicalChannelList = function () {
2017-11-13 15:32:40 +00:00
return Util.deduplicateString(getUserChannelList()).sort();
2017-04-07 14:33:14 +00:00
};
2017-04-10 15:42:35 +00:00
var pinsReady = common.pinsReady = function () {
2017-11-23 11:28:49 +00:00
if (!LocalStore.isLoggedIn()) {
2017-04-11 12:53:44 +00:00
return false;
}
2017-04-10 15:42:35 +00:00
if (!PINNING_ENABLED) {
console.error('[PINNING_DISABLED]');
return false;
}
if (!rpc) {
console.error('RPC_NOT_READY');
2017-04-10 15:42:35 +00:00
return false;
}
return true;
};
2017-05-04 14:16:09 +00:00
common.arePinsSynced = function (cb) {
if (!pinsReady()) { return void cb ('RPC_NOT_READY'); }
2017-04-10 15:42:35 +00:00
var list = getCanonicalChannelList();
var local = Hash.hashChannelList(list);
2017-04-10 15:42:35 +00:00
rpc.getServerHash(function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash === local);
});
};
2017-05-04 14:16:09 +00:00
common.resetPins = function (cb) {
if (!pinsReady()) { return void cb ('RPC_NOT_READY'); }
2017-04-10 15:42:35 +00:00
var list = getCanonicalChannelList();
rpc.reset(list, function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash);
});
};
2017-05-04 14:16:09 +00:00
common.pinPads = function (pads, cb) {
if (!pinsReady()) { return void cb ('RPC_NOT_READY'); }
if (typeof(cb) !== 'function') {
console.error('expected a callback');
}
2017-04-11 12:53:44 +00:00
rpc.pin(pads, function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash);
});
};
2017-05-04 14:16:09 +00:00
common.unpinPads = function (pads, cb) {
if (!pinsReady()) { return void cb ('RPC_NOT_READY'); }
2017-04-11 12:53:44 +00:00
rpc.unpin(pads, function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash);
});
};
2017-05-04 14:16:09 +00:00
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);
});
2017-04-14 09:41:51 +00:00
};
2017-08-21 10:01:38 +00:00
// SFRAME: talk to anon_rpc from the iframe
common.anonRpcMsg = function (msg, data, cb) {
if (!msg) { return; }
if (!anon_rpc) { return void cb('ANON_RPC_NOT_READY'); }
anon_rpc.send(msg, data, cb);
};
2017-05-04 14:16:09 +00:00
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');
}
});
};
2017-05-11 14:12:44 +00:00
common.updatePinLimit = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
rpc.updatePinLimits(function (e, limit, plan, note) {
2017-05-15 16:03:12 +00:00
if (e) { return cb(e); }
common.account.limit = limit;
common.account.plan = plan;
common.account.note = note;
cb(e, limit, plan, note);
2017-05-15 16:03:12 +00:00
});
2017-05-11 14:12:44 +00:00
};
2017-05-04 14:16:09 +00:00
common.getPinLimit = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
var account = common.account;
2017-07-04 13:50:52 +00:00
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);
};
2017-05-04 14:16:09 +00:00
common.isOverPinLimit = function (cb) {
2017-11-23 11:28:49 +00:00
if (!LocalStore.isLoggedIn()) { return void cb(null, false); }
2017-04-28 10:12:17 +00:00
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) {
2017-05-02 15:14:53 +00:00
return void cb (null, true, data);
}
2017-05-02 15:14:53 +00:00
return void cb (null, false, data);
};
var todo = function (e, used) {
if (e) { return void cb(e); }
2017-06-09 13:33:03 +00:00
usage = used;
common.getPinLimit(andThen);
};
common.getPinnedUsage(todo);
};
2017-07-12 16:54:08 +00:00
common.clearOwnedChannel = function (channel, cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
rpc.clearOwnedChannel(channel, cb);
};
2017-05-18 10:36:12 +00:00
common.uploadComplete = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
2017-05-18 10:36:12 +00:00
rpc.uploadComplete(cb);
};
common.uploadStatus = function (size, cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
2017-05-18 10:36:12 +00:00
rpc.uploadStatus(size, cb);
};
common.uploadCancel = function (cb) {
if (!pinsReady()) { return void cb('RPC_NOT_READY'); }
2017-05-18 10:36:12 +00:00
rpc.uploadCancel(cb);
};
2017-08-21 13:20:38 +00:00
// Forget button
2017-11-09 17:17:49 +00:00
// TODO REFACTOR only used in sframe-common-outer
common.moveToTrash = function (cb, href) {
href = href || window.location.href;
2017-08-21 13:20:38 +00:00
common.forgetPad(href, function (err) {
if (err) {
console.log("unable to forget pad");
console.error(err);
cb(err, null);
return;
}
var n = getNetwork();
var r = getRealtime();
if (n && r) {
Realtime.whenRealtimeSyncs(r, function () {
2017-08-21 13:20:38 +00:00
n.disconnect();
cb();
});
} else {
cb();
}
});
};
2017-11-13 15:32:40 +00:00
2017-11-09 17:17:49 +00:00
// TODO REFACTOR only used in sframe-common-outer
common.saveAsTemplate = function (Cryptput, data, cb) {
2017-11-13 15:32:40 +00:00
var p = Hash.parsePadUrl(window.location.href);
2017-08-21 16:21:53 +00:00
if (!p.type) { return; }
2017-11-13 15:32:40 +00:00
var hash = Hash.createRandomHash();
2017-08-21 16:21:53 +00:00
var href = '/' + p.type + '/#' + hash;
Cryptput(hash, data.toSave, function (e) {
if (e) { throw new Error(e); }
common.addTemplate(makePad(href, data.title));
Realtime.whenRealtimeSyncs(getRealtime(), function () {
2017-08-21 16:21:53 +00:00
cb();
});
});
};
2016-09-27 16:33:03 +00:00
2017-08-21 12:41:56 +00:00
common.getShareHashes = function (secret, cb) {
if (!window.location.hash) {
2017-11-13 15:32:40 +00:00
var hashes = Hash.getHashes(secret.channel, secret);
2017-08-21 12:41:56 +00:00
return void cb(null, hashes);
}
common.getRecentPads(function (err, recent) {
2017-11-13 15:32:40 +00:00
var parsed = Hash.parsePadUrl(window.location.href);
if (!parsed.type || !parsed.hashData) { return void cb('E_INVALID_HREF'); }
2017-09-12 16:40:11 +00:00
if (parsed.type === 'file') { secret.channel = Util.base64ToHex(secret.channel); }
2017-11-13 15:32:40 +00:00
var hashes = Hash.getHashes(secret.channel, secret);
if (!hashes.editHash && !hashes.viewHash && parsed.hashData && !parsed.hashData.mode) {
// It means we're using an old hash
hashes.editHash = window.location.hash.slice(1);
}
// If we have a stronger version in drive, add it and add a redirect button
2017-11-13 15:32:40 +00:00
var stronger = recent && Hash.findStronger(null, recent);
if (stronger) {
2017-11-13 15:32:40 +00:00
var parsed2 = Hash.parsePadUrl(stronger);
2017-08-23 09:04:44 +00:00
hashes.editHash = parsed2.hash;
}
cb(null, hashes);
});
};
var CRYPTPAD_VERSION = 'cryptpad-version';
var updateLocalVersion = function () {
// Check for CryptPad updates
var urlArgs = Config.requireConf ? Config.requireConf.urlArgs : null;
if (!urlArgs) { return; }
var arr = /ver=([0-9.]+)(-[0-9]*)?/.exec(urlArgs);
var ver = arr[1];
if (!ver) { return; }
var verArr = ver.split('.');
verArr[2] = 0;
if (verArr.length !== 3) { return; }
var stored = localStorage[CRYPTPAD_VERSION] || '0.0.0';
var storedArr = stored.split('.');
storedArr[2] = 0;
var shouldUpdate = parseInt(verArr[0]) > parseInt(storedArr[0]) ||
(parseInt(verArr[0]) === parseInt(storedArr[0]) &&
parseInt(verArr[1]) > parseInt(storedArr[1]));
if (!shouldUpdate) { return; }
localStorage[CRYPTPAD_VERSION] = ver;
};
common.ready = (function () {
var env = {};
var initialized = false;
return function (f) {
if (initialized) {
2017-09-14 09:23:37 +00:00
return void setTimeout(function () { f(void 0, env); });
}
2017-11-21 15:46:19 +00:00
if (sessionStorage[Constants.newPadPathKey]) {
common.initialPath = sessionStorage[Constants.newPadPathKey];
delete sessionStorage[Constants.newPadPathKey];
2017-09-14 09:27:10 +00:00
}
2017-09-14 09:23:37 +00:00
var proxy;
var network;
var provideFeedback = function () {
2017-07-13 08:59:28 +00:00
if (Object.keys(proxy).length === 1) {
2017-11-23 11:28:49 +00:00
Feedback.send("FIRST_APP_USE", true);
2017-07-13 08:59:28 +00:00
}
if (typeof(window.Proxy) === 'undefined') {
2017-11-23 11:28:49 +00:00
Feedback.send("NO_PROXIES");
}
2017-08-29 09:49:10 +00:00
var shimPattern = /CRYPTPAD_SHIM/;
if (shimPattern.test(Array.isArray.toString())) {
2017-11-23 11:28:49 +00:00
Feedback.send("NO_ISARRAY");
}
2017-08-29 09:49:10 +00:00
if (shimPattern.test(Array.prototype.fill.toString())) {
2017-11-23 11:28:49 +00:00
Feedback.send("NO_ARRAYFILL");
2017-08-29 09:49:10 +00:00
}
if (typeof(Symbol) === 'undefined') {
2017-11-23 11:28:49 +00:00
Feedback.send('NO_SYMBOL');
}
2017-11-23 11:28:49 +00:00
Feedback.reportScreenDimensions();
Feedback.reportLanguage();
};
var initFeedback = function () {
// Initialize feedback
try {
var entry = Util.find(getProxy(), [
'settings',
'general',
'allowUserFeedback'
]);
Feedback.init(entry);
} catch (e) {
console.error(e);
Feedback.init(false);
}
provideFeedback();
2017-09-14 09:23:37 +00:00
};
2017-06-22 15:42:24 +00:00
2017-09-14 09:23:37 +00:00
Nthen(function (waitFor) {
Store.ready(waitFor(function (err, storeObj) {
store = common.store = env.store = storeObj;
Messaging.addDirectMessageHandler(common);
2017-09-14 09:23:37 +00:00
proxy = getProxy();
network = getNetwork();
network.on('disconnect', function () {
Realtime.setConnectionState(false);
});
network.on('reconnect', function () {
Realtime.setConnectionState(true);
});
2017-11-23 11:28:49 +00:00
initFeedback();
2017-09-14 09:23:37 +00:00
}), common);
}).nThen(function (waitFor) {
$(waitFor());
}).nThen(function (waitFor) {
// Load the new pad when the hash has changed
var oldHref = document.location.href;
window.onhashchange = function () {
var newHref = document.location.href;
2017-11-13 15:32:40 +00:00
var parsedOld = Hash.parsePadUrl(oldHref).hashData;
var parsedNew = Hash.parsePadUrl(newHref).hashData;
2017-09-14 09:23:37 +00:00
if (parsedOld && parsedNew && (
parsedOld.type !== parsedNew.type
|| parsedOld.channel !== parsedNew.channel
|| parsedOld.mode !== parsedNew.mode
|| parsedOld.key !== parsedNew.key)) {
if (!parsedOld.channel) { oldHref = newHref; return; }
document.location.reload();
return;
}
if (parsedNew) { oldHref = newHref; }
};
// Listen for login/logout in other tabs
window.addEventListener('storage', function (e) {
2017-11-21 15:46:19 +00:00
if (e.key !== Constants.userHashKey) { return; }
var o = e.oldValue;
var n = e.newValue;
if (!o && n) {
document.location.reload();
} else if (o && !n) {
2017-11-23 11:28:49 +00:00
LocalStore.logout();
if (getNetwork()) {
getNetwork().disconnect();
}
}
});
2017-11-23 11:28:49 +00:00
if (PINNING_ENABLED && LocalStore.isLoggedIn()) {
2017-09-14 09:23:37 +00:00
console.log("logged in. pads will be pinned");
var w0 = waitFor();
Pinpad.create(network, proxy, function (e, call) {
if (e) {
console.error(e);
return w0();
}
2017-09-14 09:23:37 +00:00
console.log('RPC handshake complete');
rpc = common.rpc = env.rpc = call;
2017-05-30 13:42:53 +00:00
2017-09-14 09:23:37 +00:00
common.getPinLimit(function (e, limit, plan, note) {
if (e) { return void console.error(e); }
common.account.limit = limit;
localStorage.plan = common.account.plan = plan;
common.account.note = note;
w0();
});
2017-09-14 09:23:37 +00:00
common.arePinsSynced(function (err, yes) {
if (!yes) {
common.resetPins(function (err) {
if (err) {
console.error("Pin Reset Error");
return console.error(err);
}
console.log('RESET DONE');
});
}
});
});
2017-09-14 09:23:37 +00:00
} else if (PINNING_ENABLED) {
console.log('not logged in. pads will not be pinned');
} else {
console.log('pinning disabled');
}
2017-09-14 09:23:37 +00:00
var w1 = waitFor();
require([
'/common/rpc.js',
], function (Rpc) {
Rpc.createAnonymous(network, function (e, call) {
if (e) {
console.error(e);
return void w1();
}
2017-09-14 09:23:37 +00:00
anon_rpc = common.anon_rpc = env.anon_rpc = call;
w1();
});
});
2017-09-14 09:23:37 +00:00
// Everything's ready, continue...
if($('#pad-iframe').length) {
var w2 = waitFor();
var $iframe = $('#pad-iframe');
var iframe = $iframe[0];
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
if (iframeDoc.readyState === 'complete') {
return void w2();
}
$iframe.load(w2); //cb);
}
}).nThen(function (waitFor) {
if (sessionStorage.createReadme) {
var w = waitFor();
require(['/common/cryptget.js'], function (Crypt) {
2017-11-13 15:32:40 +00:00
var hash = Hash.createRandomHash();
Crypt.put(hash, Messages.driveReadme, function (e) {
if (e) {
console.error("Error while creating the default pad:", e);
return void w();
}
var href = '/pad/#' + hash;
var data = {
href: href,
title: Messages.driveReadmeTitle,
2017-11-21 10:09:59 +00:00
atime: +new Date(),
ctime: +new Date()
};
common.getFO().pushData(data, function (e, id) {
if (e) {
console.error("Error while creating the default pad:", e);
return void w();
}
common.getFO().add(id);
w();
});
});
delete sessionStorage.createReadme;
});
}
2017-09-22 17:35:06 +00:00
}).nThen(function (waitFor) {
if (sessionStorage.migrateAnonDrive) {
var w = waitFor();
require(['/common/mergeDrive.js'], function (Merge) {
2017-11-23 11:28:49 +00:00
var hash = LocalStore.getFSHash();
2017-09-22 17:35:06 +00:00
Merge.anonDriveIntoUser(getStore().getProxy(), hash, function () {
delete sessionStorage.migrateAnonDrive;
w();
});
});
}
2017-09-14 09:23:37 +00:00
}).nThen(function () {
updateLocalVersion();
f(void 0, env);
if (typeof(window.onhashchange) === 'function') { window.onhashchange(); }
});
};
}());
2017-09-14 09:23:37 +00:00
// MAGIC that happens implicitly
$(function () {
Language.applyTranslation();
});
2016-05-28 11:13:54 +00:00
return common;
});