cryptpad/www/common/sframe-common.js

487 lines
17 KiB
JavaScript
Raw Normal View History

2017-08-17 14:28:54 +00:00
define([
2017-08-23 09:04:44 +00:00
'jquery',
2017-08-17 14:56:18 +00:00
'/bower_components/nthen/index.js',
2017-08-21 13:20:38 +00:00
'/customize/messages.js',
2017-08-17 14:28:54 +00:00
'/common/sframe-chainpad-netflux-inner.js',
2017-08-17 16:28:05 +00:00
'/common/sframe-channel.js',
'/common/sframe-common-title.js',
2017-11-09 17:17:49 +00:00
'/common/common-ui-elements.js',
2017-08-21 15:40:21 +00:00
'/common/sframe-common-history.js',
'/common/sframe-common-file.js',
2017-09-07 16:56:58 +00:00
'/common/sframe-common-codemirror.js',
'/common/metadata-manager.js',
2017-08-17 16:28:05 +00:00
2017-08-21 16:30:51 +00:00
'/customize/application_config.js',
'/common/common-realtime.js',
'/common/common-util.js',
2017-11-13 15:32:40 +00:00
'/common/common-hash.js',
2017-11-07 13:51:53 +00:00
'/common/common-thumbnail.js',
'/common/common-interface.js',
2017-11-23 11:28:49 +00:00
'/common/common-feedback.js',
2017-12-07 11:20:26 +00:00
'/common/common-language.js',
2017-11-07 13:51:53 +00:00
'/bower_components/localforage/dist/localforage.min.js'
2017-09-11 13:52:14 +00:00
], function (
$,
nThen,
Messages,
CpNfInner,
SFrameChannel,
Title,
UIElements,
2017-09-11 13:52:14 +00:00
History,
File,
CodeMirror,
2017-09-07 16:56:58 +00:00
MetadataMgr,
2017-09-11 13:52:14 +00:00
AppConfig,
CommonRealtime,
Util,
2017-11-13 15:32:40 +00:00
Hash,
2017-11-07 13:51:53 +00:00
Thumb,
UI,
2017-11-23 11:28:49 +00:00
Feedback,
2017-12-07 11:20:26 +00:00
Language,
2017-11-07 13:51:53 +00:00
localForage
2017-09-11 13:52:14 +00:00
) {
2017-08-17 14:28:54 +00:00
// Chainpad Netflux Inner
2017-08-17 14:56:18 +00:00
var funcs = {};
var ctx = {};
2017-08-21 15:40:21 +00:00
funcs.Messages = Messages;
var evRealtimeSynced = Util.mkEvent(true);
2017-08-17 14:56:18 +00:00
funcs.startRealtime = function (options) {
if (ctx.cpNfInner) { return ctx.cpNfInner; }
options.sframeChan = ctx.sframeChan;
options.metadataMgr = ctx.metadataMgr;
2017-08-17 14:56:18 +00:00
ctx.cpNfInner = CpNfInner.start(options);
ctx.cpNfInner.metadataMgr.onChangeLazy(options.onLocal);
ctx.cpNfInner.whenRealtimeSyncs(function () { evRealtimeSynced.fire(); });
2017-08-17 14:56:18 +00:00
return ctx.cpNfInner;
};
2017-09-06 16:26:10 +00:00
funcs.getMetadataMgr = function () { return ctx.metadataMgr; };
funcs.getSframeChannel = function () { return ctx.sframeChan; };
funcs.getAppConfig = function () { return AppConfig; };
2017-08-21 15:40:21 +00:00
2017-09-07 16:56:58 +00:00
funcs.isLoggedIn = function () {
2017-09-12 16:40:11 +00:00
return ctx.metadataMgr.getPrivateData().accountName;
2017-08-17 14:28:54 +00:00
};
2017-09-06 16:26:10 +00:00
// MISC
// Call the selected function with 'funcs' as a (new) first parameter
var callWithCommon = function (f) {
return function () {
[].unshift.call(arguments, funcs);
return f.apply(null, arguments);
};
};
// UI
funcs.createUserAdminMenu = callWithCommon(UIElements.createUserAdminMenu);
funcs.initFilePicker = callWithCommon(UIElements.initFilePicker);
funcs.openFilePicker = callWithCommon(UIElements.openFilePicker);
funcs.openTemplatePicker = callWithCommon(UIElements.openTemplatePicker);
funcs.displayMediatagImage = callWithCommon(UIElements.displayMediatagImage);
funcs.displayAvatar = callWithCommon(UIElements.displayAvatar);
funcs.createButton = callWithCommon(UIElements.createButton);
funcs.createUsageBar = callWithCommon(UIElements.createUsageBar);
funcs.updateTags = callWithCommon(UIElements.updateTags);
funcs.createLanguageSelector = callWithCommon(UIElements.createLanguageSelector);
funcs.createMarkdownToolbar = callWithCommon(UIElements.createMarkdownToolbar);
2018-02-27 16:38:29 +00:00
funcs.createHelpMenu = callWithCommon(UIElements.createHelpMenu);
2017-12-07 17:51:50 +00:00
funcs.getPadCreationScreen = callWithCommon(UIElements.getPadCreationScreen);
2018-01-12 08:48:40 +00:00
funcs.createNewPadModal = callWithCommon(UIElements.createNewPadModal);
funcs.onServerError = callWithCommon(UIElements.onServerError);
// Thumb
2017-11-07 13:51:53 +00:00
funcs.displayThumbnail = callWithCommon(Thumb.displayThumbnail);
2018-03-13 10:31:08 +00:00
funcs.addThumbnail = Thumb.addThumbnail;
2017-08-21 15:40:21 +00:00
// History
2017-09-06 16:26:10 +00:00
funcs.getHistory = callWithCommon(History.create);
2017-08-21 15:40:21 +00:00
2017-08-17 16:28:05 +00:00
// Title module
2017-09-06 16:26:10 +00:00
funcs.createTitle = callWithCommon(Title.create);
// Files
funcs.uploadFile = callWithCommon(File.uploadFile);
funcs.createFileManager = callWithCommon(File.create);
2017-09-12 16:40:11 +00:00
funcs.getMediatagScript = function () {
var origin = ctx.metadataMgr.getPrivateData().origin;
return '<script src="' + origin + '/common/media-tag-nacl.min.js"></script>';
};
2018-06-11 13:40:36 +00:00
funcs.getMediatagFromHref = function () {
2017-09-12 16:40:11 +00:00
var data = ctx.metadataMgr.getPrivateData();
2018-06-11 13:40:36 +00:00
var secret = Hash.getSecrets('file', data.availableHashes.fileHash, data.password);
2017-09-12 16:40:11 +00:00
if (secret.keys && secret.channel) {
2018-05-25 16:00:10 +00:00
var key = Hash.encodeBase64(secret.keys && secret.keys.cryptKey);
var hexFileName = secret.channel;
2017-09-12 16:40:11 +00:00
var origin = data.fileHost || data.origin;
2017-11-13 15:32:40 +00:00
var src = origin + Hash.getBlobPathFromHex(hexFileName);
2018-05-25 16:00:10 +00:00
return '<media-tag src="' + src + '" data-crypto-key="cryptpad:' + key + '">' +
2017-09-12 16:40:11 +00:00
'</media-tag>';
}
return;
};
2018-04-27 15:23:23 +00:00
funcs.getFileSize = function (channelId, cb) {
2017-09-13 14:19:26 +00:00
funcs.sendAnonRpcMsg("GET_FILE_SIZE", channelId, function (data) {
if (!data) { return void cb("No response"); }
if (data.error) { return void cb(data.error); }
if (data.response && data.response.length && typeof(data.response[0]) === 'number') {
return void cb(void 0, data.response[0]);
} else {
cb('INVALID_RESPONSE');
}
});
};
2017-08-17 16:28:05 +00:00
2017-09-07 16:56:58 +00:00
// CodeMirror
funcs.initCodeMirrorApp = callWithCommon(CodeMirror.create);
2017-08-17 16:28:05 +00:00
// Window
funcs.logout = function (cb) {
2017-09-06 16:26:10 +00:00
cb = cb || $.noop;
ctx.sframeChan.query('Q_LOGOUT', null, cb);
};
2017-08-29 09:20:02 +00:00
funcs.notify = function () {
ctx.sframeChan.event('EV_NOTIFY');
};
funcs.setTabTitle = function (newTitle) {
ctx.sframeChan.event('EV_SET_TAB_TITLE', newTitle);
};
2017-08-29 09:20:02 +00:00
funcs.setLoginRedirect = function (cb) {
2017-09-06 16:26:10 +00:00
cb = cb || $.noop;
ctx.sframeChan.query('Q_SET_LOGIN_REDIRECT', null, cb);
};
2017-09-07 16:56:58 +00:00
funcs.isPresentUrl = function (cb) {
ctx.sframeChan.query('Q_PRESENT_URL_GET_VALUE', null, cb);
};
funcs.setPresentUrl = function (value) {
ctx.sframeChan.event('EV_PRESENT_URL_SET_VALUE', value);
};
// Store
funcs.handleNewFile = function (waitFor) {
2018-04-10 14:38:31 +00:00
if (window.__CRYPTPAD_TEST__) { return; }
var priv = ctx.metadataMgr.getPrivateData();
if (priv.isNewFile) {
var c = (priv.settings.general && priv.settings.general.creation) || {};
var skip = !AppConfig.displayCreationScreen || (c.skip && !priv.forceCreationScreen);
// If this is a new file but we have a hash in the URL and pad creation screen is
// not displayed, then display an error...
if (priv.isDeleted && (!funcs.isLoggedIn() || skip)) {
UI.errorLoadingScreen(Messages.inactiveError, false, function () {
UI.addLoadingScreen();
return void funcs.createPad({}, waitFor());
});
return;
}
// Otherwise, if we don't display the screen, it means it is not a deleted pad
// so we can continue and start realtime...
if (!funcs.isLoggedIn() || skip) {
return void funcs.createPad(c, waitFor());
}
// If we display the pad creation screen, it will handle deleted pads directly
funcs.getPadCreationScreen(c, waitFor());
}
};
funcs.createPad = function (cfg, cb) {
ctx.sframeChan.query("Q_CREATE_PAD", {
owned: cfg.owned,
expire: cfg.expire,
password: cfg.password,
2018-03-13 10:31:08 +00:00
template: cfg.template,
templateId: cfg.templateId
}, cb);
};
2017-08-21 10:01:38 +00:00
funcs.sendAnonRpcMsg = function (msg, content, cb) {
ctx.sframeChan.query('Q_ANON_RPC_MESSAGE', {
msg: msg,
content: content
}, function (err, data) {
if (cb) { cb(data); }
});
};
2017-09-21 15:56:24 +00:00
funcs.getPinUsage = function (cb) {
cb = cb || $.noop;
ctx.sframeChan.query('Q_PIN_GET_USAGE', null, function (err, data) {
cb(err || data.error, data.data);
});
};
2017-08-21 10:01:38 +00:00
2017-08-21 10:24:33 +00:00
funcs.isOverPinLimit = function (cb) {
ctx.sframeChan.query('Q_GET_PIN_LIMIT_STATUS', null, function (err, data) {
cb(data.error, data.overLimit, data.limits);
});
};
2017-08-21 15:40:21 +00:00
funcs.getFullHistory = function (realtime, cb) {
ctx.sframeChan.query('Q_GET_FULL_HISTORY', null, function (err, messages) {
if (err) { return void console.error(err); }
if (!Array.isArray(messages)) { return; }
messages.forEach(function (m) {
realtime.message(m);
});
cb();
2017-08-21 15:40:21 +00:00
});
};
// href is optional here: if not provided, we use the href of the current tab
funcs.getPadAttribute = function (key, cb, href) {
2017-08-30 16:47:50 +00:00
ctx.sframeChan.query('Q_GET_PAD_ATTRIBUTE', {
key: key,
href: href
2017-08-30 16:47:50 +00:00
}, function (err, res) {
cb (err || res.error, res.data);
});
};
funcs.setPadAttribute = function (key, value, cb, href) {
cb = cb || $.noop;
2017-08-30 16:47:50 +00:00
ctx.sframeChan.query('Q_SET_PAD_ATTRIBUTE', {
key: key,
href: href,
2017-08-30 16:47:50 +00:00
value: value
}, cb);
};
funcs.getAttribute = function (key, cb) {
ctx.sframeChan.query('Q_GET_ATTRIBUTE', {
key: key
}, function (err, res) {
cb (err || res.error, res.data);
});
};
funcs.setAttribute = function (key, value, cb) {
cb = cb || $.noop;
ctx.sframeChan.query('Q_SET_ATTRIBUTE', {
key: key,
value: value
}, cb);
};
2017-11-07 13:51:53 +00:00
// Thumbnails
funcs.setThumbnail = function (key, value, cb) {
cb = cb || $.noop;
ctx.sframeChan.query('Q_THUMBNAIL_SET', {
key: key,
value: value
}, cb);
};
funcs.getThumbnail = function (key, cb) {
ctx.sframeChan.query('Q_THUMBNAIL_GET', {
key: key
}, function (err, res) {
cb (err || res.error, res.data);
});
};
2017-09-22 17:35:06 +00:00
funcs.sessionStorage = {
put: function (key, value, cb) {
ctx.sframeChan.query('Q_SESSIONSTORAGE_PUT', {
key: key,
value: value
}, cb);
}
};
2017-09-06 16:26:10 +00:00
funcs.isStrongestStored = function () {
var data = ctx.metadataMgr.getPrivateData();
2017-09-12 16:40:11 +00:00
if (data.availableHashes.fileHash) { return true; }
2017-09-06 16:26:10 +00:00
return !data.readOnly || !data.availableHashes.editHash;
2017-08-30 16:47:50 +00:00
};
2017-09-07 16:56:58 +00:00
funcs.setDisplayName = function (name, cb) {
cb = cb || $.noop;
ctx.sframeChan.query('Q_SETTINGS_SET_DISPLAY_NAME', name, cb);
};
2017-09-22 17:35:06 +00:00
funcs.mergeAnonDrive = function (cb) {
ctx.sframeChan.query('Q_MERGE_ANON_DRIVE', null, cb);
};
// Friends
var pendingFriends = [];
funcs.getPendingFriends = function () {
return pendingFriends.slice();
};
funcs.sendFriendRequest = function (netfluxId) {
ctx.sframeChan.query('Q_SEND_FRIEND_REQUEST', netfluxId, $.noop);
pendingFriends.push(netfluxId);
};
// Feedback
2017-09-06 16:26:10 +00:00
funcs.prepareFeedback = function (key) {
2017-08-22 16:18:58 +00:00
if (typeof(key) !== 'string') { return $.noop; }
2017-08-21 13:20:38 +00:00
2017-08-22 16:18:58 +00:00
var type = ctx.metadataMgr.getMetadata().type;
return function () {
2017-11-23 11:28:49 +00:00
Feedback.send((key + (type? '_' + type: '')).toUpperCase());
2017-08-22 16:18:58 +00:00
};
};
2017-08-21 13:20:38 +00:00
2017-09-06 16:26:10 +00:00
// RESTRICTED
// Filepicker app
2017-09-05 09:35:15 +00:00
funcs.getFilesList = function (types, cb) {
ctx.sframeChan.query('Q_GET_FILES_LIST', types, function (err, data) {
cb(err || data.error, data.data);
});
};
2017-08-18 16:43:04 +00:00
/* funcs.storeLinkToClipboard = function (readOnly, cb) {
ctx.sframeChan.query('Q_STORE_LINK_TO_CLIPBOARD', readOnly, function (err) {
if (cb) { cb(err); }
});
2017-09-06 16:26:10 +00:00
}; */
2017-08-30 15:57:10 +00:00
funcs.gotoURL = function (url) { ctx.sframeChan.event('EV_GOTO_URL', url); };
2017-09-22 17:35:06 +00:00
funcs.openURL = function (url) { ctx.sframeChan.event('EV_OPEN_URL', url); };
funcs.openUnsafeURL = function (url) {
var bounceHref = window.location.origin + '/bounce/#' + encodeURIComponent(url);
window.open(bounceHref);
};
2018-03-23 14:05:26 +00:00
funcs.fixLinks = function (domElement) {
var origin = ctx.metadataMgr.getPrivateData().origin;
$(domElement).find('a[target="_blank"]').click(function (e) {
e.preventDefault();
e.stopPropagation();
var href = $(this).attr('href');
var absolute = /^https?:\/\//i;
if (!absolute.test(href)) {
if (href.slice(0,1) !== '/') { href = '/' + href; }
href = origin + href;
}
funcs.openUnsafeURL(href);
});
$(domElement).find('a[target!="_blank"]').click(function (e) {
e.preventDefault();
e.stopPropagation();
funcs.gotoURL($(this).attr('href'));
});
return $(domElement)[0];
};
funcs.whenRealtimeSyncs = evRealtimeSynced.reg;
var logoutHandlers = [];
funcs.onLogout = function (h) {
if (typeof (h) !== "function") { return; }
if (logoutHandlers.indexOf(h) !== -1) { return; }
logoutHandlers.push(h);
};
2017-08-17 14:56:18 +00:00
Object.freeze(funcs);
return { create: function (cb) {
if (window.CryptPad_sframe_common) {
throw new Error("Sframe-common should only be created once");
}
window.CryptPad_sframe_common = true;
2017-08-17 14:56:18 +00:00
nThen(function (waitFor) {
2017-09-06 08:56:27 +00:00
SFrameChannel.create(window.parent, waitFor(function (sfc) { ctx.sframeChan = sfc; }), true);
2017-08-17 14:56:18 +00:00
// CpNfInner.start() should be here....
2017-11-30 16:21:58 +00:00
}).nThen(function (waitFor) {
2017-11-07 13:51:53 +00:00
localForage.clear();
2017-12-07 11:20:26 +00:00
Language.applyTranslation();
2017-11-07 13:51:53 +00:00
ctx.metadataMgr = MetadataMgr.create(ctx.sframeChan);
ctx.sframeChan.whenReg('EV_CACHE_PUT', function () {
if (Object.keys(window.cryptpadCache.updated).length) {
ctx.sframeChan.event('EV_CACHE_PUT', window.cryptpadCache.updated);
}
window.cryptpadCache._put = window.cryptpadCache.put;
window.cryptpadCache.put = function (k, v, cb) {
window.cryptpadCache._put(k, v, cb);
var x = {};
x[k] = v;
ctx.sframeChan.event('EV_CACHE_PUT', x);
};
});
2017-09-21 15:56:24 +00:00
ctx.sframeChan.whenReg('EV_LOCALSTORE_PUT', function () {
if (Object.keys(window.cryptpadStore.updated).length) {
ctx.sframeChan.event('EV_LOCALSTORE_PUT', window.cryptpadStore.updated);
}
window.cryptpadStore._put = window.cryptpadStore.put;
window.cryptpadStore.put = function (k, v, cb) {
window.cryptpadStore._put(k, v, cb);
var x = {};
x[k] = v;
ctx.sframeChan.event('EV_LOCALSTORE_PUT', x);
};
});
UI.addTooltips();
ctx.sframeChan.on('Q_INCOMING_FRIEND_REQUEST', function (confirmMsg, cb) {
UI.confirm(confirmMsg, cb, null, true);
});
ctx.sframeChan.on('EV_FRIEND_REQUEST', function (data) {
var i = pendingFriends.indexOf(data.netfluxId);
if (i !== -1) { pendingFriends.splice(i, 1); }
UI.log(data.logText);
});
ctx.sframeChan.on("EV_PAD_PASSWORD", function () {
UIElements.displayPasswordPrompt(funcs);
});
ctx.sframeChan.on('EV_LOADING_INFO', function (data) {
UI.updateLoadingProgress(data, true);
});
2018-06-11 14:52:26 +00:00
ctx.sframeChan.on('EV_NEW_VERSION', function () {
2018-06-11 16:18:39 +00:00
var $err = $('<div>').append(Messages.newVersionError);
$err.find('a').click(function () {
funcs.gotoURL();
});
2018-06-12 12:23:54 +00:00
UI.findOKButton().click();
2018-06-11 16:18:39 +00:00
UI.errorLoadingScreen($err, true, true);
2018-06-11 14:52:26 +00:00
});
2017-11-30 16:21:58 +00:00
ctx.metadataMgr.onReady(waitFor());
}).nThen(function () {
try {
var feedback = ctx.metadataMgr.getPrivateData().feedbackAllowed;
Feedback.init(feedback);
} catch (e) { Feedback.init(false); }
2018-03-21 17:27:20 +00:00
ctx.sframeChan.on('EV_LOADING_ERROR', function (err) {
if (err === 'DELETED') {
var msg = Messages.deletedError + '<br>' + Messages.errorRedirectToHome;
UI.errorLoadingScreen(msg, false, function () {
funcs.gotoURL('/drive/');
});
}
});
2018-03-21 17:27:20 +00:00
ctx.sframeChan.on('EV_LOGOUT', function () {
$(window).on('keyup', function (e) {
if (e.keyCode === 27) {
UI.removeLoadingScreen();
}
});
UI.addLoadingScreen({hideTips: true});
var origin = ctx.metadataMgr.getPrivateData().origin;
var href = origin + "/login/";
var onLogoutMsg = Messages._getKey('onLogout', ['<a href="' + href + '" target="_blank">', '</a>']);
UI.errorLoadingScreen(onLogoutMsg, true);
logoutHandlers.forEach(function (h) {
if (typeof (h) === "function") { h(); }
});
});
ctx.sframeChan.ready();
2017-08-17 14:56:18 +00:00
cb(funcs);
});
} };
2017-08-17 14:28:54 +00:00
});