cryptpad/www/common/sframe-common-outer.js

806 lines
34 KiB
JavaScript
Raw Normal View History

2017-08-30 09:10:57 +00:00
// Load #1, load as little as possible because we are in a race to get the loading screen up.
define([
'/bower_components/nthen/index.js',
'/api/config',
'jquery',
], function (nThen, ApiConfig, $) {
var common = {};
2017-09-12 16:40:11 +00:00
common.start = function (cfg) {
cfg = cfg || {};
var realtime = !cfg.noRealtime;
2017-08-30 09:10:57 +00:00
var secret;
var hashes;
2017-12-07 17:51:50 +00:00
var isNewFile;
2017-08-30 09:10:57 +00:00
var CpNfOuter;
var Cryptpad;
var Crypto;
var Cryptget;
var SFrameChannel;
2017-08-30 09:10:57 +00:00
var sframeChan;
var FilePicker;
var Messaging;
var Notifier;
2017-11-13 15:32:40 +00:00
var Utils = {};
2017-12-07 17:51:50 +00:00
var AppConfig;
2017-12-14 10:34:44 +00:00
var Test;
var password;
2018-04-27 09:54:23 +00:00
var initialPathInDrive;
2017-08-30 09:10:57 +00:00
nThen(function (waitFor) {
// Load #2, the loading screen is up so grab whatever you need...
require([
'/common/sframe-chainpad-netflux-outer.js',
'/common/cryptpad-common.js',
'/bower_components/chainpad-crypto/crypto.js',
'/common/cryptget.js',
'/common/sframe-channel.js',
'/filepicker/main.js',
'/common/common-messaging.js',
'/common/common-notifier.js',
2017-11-13 15:32:40 +00:00
'/common/common-hash.js',
'/common/common-util.js',
'/common/common-realtime.js',
2017-11-23 11:28:49 +00:00
'/common/common-constants.js',
'/common/common-feedback.js',
'/common/outer/local-store.js',
2017-12-07 17:51:50 +00:00
'/customize/application_config.js',
2017-12-14 10:34:44 +00:00
'/common/test.js',
], waitFor(function (_CpNfOuter, _Cryptpad, _Crypto, _Cryptget, _SFrameChannel,
2017-12-01 13:49:21 +00:00
_FilePicker, _Messaging, _Notifier, _Hash, _Util, _Realtime,
_Constants, _Feedback, _LocalStore, _AppConfig, _Test) {
2017-08-30 09:10:57 +00:00
CpNfOuter = _CpNfOuter;
Cryptpad = _Cryptpad;
Crypto = _Crypto;
Cryptget = _Cryptget;
SFrameChannel = _SFrameChannel;
FilePicker = _FilePicker;
Messaging = _Messaging;
Notifier = _Notifier;
2017-11-13 15:32:40 +00:00
Utils.Hash = _Hash;
Utils.Util = _Util;
Utils.Realtime = _Realtime;
2017-11-23 11:28:49 +00:00
Utils.Constants = _Constants;
Utils.Feedback = _Feedback;
Utils.LocalStore = _LocalStore;
2017-12-07 17:51:50 +00:00
AppConfig = _AppConfig;
2017-12-14 10:34:44 +00:00
Test = _Test;
if (localStorage.CRYPTPAD_URLARGS !== ApiConfig.requireConf.urlArgs) {
console.log("New version, flushing cache");
Object.keys(localStorage).forEach(function (k) {
if (k.indexOf('CRYPTPAD_CACHE|') !== 0) { return; }
delete localStorage[k];
});
localStorage.CRYPTPAD_URLARGS = ApiConfig.requireConf.urlArgs;
}
var cache = {};
2017-09-21 15:56:24 +00:00
var localStore = {};
Object.keys(localStorage).forEach(function (k) {
2017-09-21 15:56:24 +00:00
if (k.indexOf('CRYPTPAD_CACHE|') === 0) {
cache[k.slice(('CRYPTPAD_CACHE|').length)] = localStorage[k];
return;
}
if (k.indexOf('CRYPTPAD_STORE|') === 0) {
localStore[k.slice(('CRYPTPAD_STORE|').length)] = localStorage[k];
return;
}
});
2017-08-30 09:10:57 +00:00
SFrameChannel.create($('#sbox-iframe')[0].contentWindow, waitFor(function (sfc) {
sframeChan = sfc;
2017-09-21 15:56:24 +00:00
}), false, { cache: cache, localStore: localStore, language: Cryptpad.getLanguage() });
Cryptpad.loading.onDriveEvent.reg(function (data) {
if (sframeChan) { sframeChan.event('EV_LOADING_INFO', data); }
});
Cryptpad.ready(waitFor(function () {
if (sframeChan) {
sframeChan.event('EV_LOADING_INFO', {
state: -1
});
}
}), {
2017-12-05 17:09:43 +00:00
messenger: cfg.messaging,
driveEvents: cfg.driveEvents
2017-12-01 13:49:21 +00:00
});
2017-08-30 09:10:57 +00:00
}));
}).nThen(function (waitFor) {
2017-10-03 12:11:11 +00:00
$('#sbox-iframe').focus();
sframeChan.on('EV_CACHE_PUT', function (x) {
Object.keys(x).forEach(function (k) {
localStorage['CRYPTPAD_CACHE|' + k] = x[k];
});
});
2017-09-21 15:56:24 +00:00
sframeChan.on('EV_LOCALSTORE_PUT', function (x) {
Object.keys(x).forEach(function (k) {
if (typeof(x[k]) === "undefined") {
delete localStorage['CRYPTPAD_STORE|' + k];
return;
}
localStorage['CRYPTPAD_STORE|' + k] = x[k];
});
});
2017-11-30 09:33:09 +00:00
if (cfg.getSecrets) {
2017-11-30 17:22:26 +00:00
var w = waitFor();
2018-04-27 15:23:23 +00:00
// No password for drive, profile and todo
2017-11-30 09:33:09 +00:00
cfg.getSecrets(Cryptpad, Utils, waitFor(function (err, s) {
secret = s;
2017-11-30 17:22:26 +00:00
Cryptpad.getShareHashes(secret, function (err, h) {
hashes = h;
w();
});
2017-11-30 09:33:09 +00:00
}));
} else {
var parsed = Utils.Hash.parsePadUrl(window.location.href);
var todo = function () {
secret = Utils.Hash.getSecrets(parsed.type, void 0, password);
Cryptpad.getShareHashes(secret, waitFor(function (err, h) { hashes = h; }));
};
// Prompt the password here if we have a hash containing /p/
// or get it from the pad attributes
var needPassword = parsed.hashData && parsed.hashData.password;
if (needPassword) {
Cryptpad.getPadAttribute('password', waitFor(function (err, val) {
if (val) {
// We already know the password, use it!
password = val;
todo();
} else {
// Ask for the password and check if the pad exists
// If the pad doesn't exist, it means the password isn't correct
// or the pad has been deleted
var correctPassword = waitFor();
sframeChan.on('Q_PAD_PASSWORD_VALUE', function (data, cb) {
password = data;
var next = function (e, isNew) {
if (Boolean(isNew)) {
// Ask again in the inner iframe
// We should receive a new Q_PAD_PASSWORD_VALUE
cb(false);
} else {
todo();
correctPassword();
cb(true);
}
};
if (parsed.type === "file") {
// `isNewChannel` doesn't work for files (not a channel)
// `getFileSize` is not adapted to channels because of metadata
Cryptpad.getFileSize(window.location.href, password, function (e, size) {
next(e, size === 0);
});
return;
}
// Not a file, so we can use `isNewChannel`
Cryptpad.isNewChannel(window.location.href, password, next);
});
sframeChan.event("EV_PAD_PASSWORD");
}
}), parsed.getUrl());
return;
}
// If no password, continue...
todo();
2017-08-30 09:10:57 +00:00
}
2017-12-07 17:51:50 +00:00
}).nThen(function (waitFor) {
// Check if the pad exists on server
if (!window.location.hash) { isNewFile = true; return; }
if (realtime) {
Cryptpad.isNewChannel(window.location.href, password, waitFor(function (e, isNew) {
if (e) { return console.error(e); }
isNewFile = Boolean(isNew);
}));
}
2017-08-30 09:10:57 +00:00
}).nThen(function () {
var readOnly = secret.keys && !secret.keys.editKeyStr;
2017-12-12 11:06:31 +00:00
var isNewHash = true;
if (!secret.keys) {
isNewHash = false;
secret.keys = secret.key;
2017-12-12 13:45:25 +00:00
readOnly = false;
2017-12-12 11:06:31 +00:00
}
2018-04-27 15:23:23 +00:00
var parsed = Utils.Hash.parsePadUrl(window.location.href);
2017-08-30 09:10:57 +00:00
if (!parsed.type) { throw new Error(); }
2017-11-21 10:09:59 +00:00
var defaultTitle = Utils.Hash.getDefaultName(parsed);
2017-12-07 17:51:50 +00:00
var edPublic;
2018-01-12 08:48:40 +00:00
var forceCreationScreen = cfg.useCreationScreen &&
sessionStorage[Utils.Constants.displayPadCreationScreen];
delete sessionStorage[Utils.Constants.displayPadCreationScreen];
2017-08-30 09:10:57 +00:00
var updateMeta = function () {
//console.log('EV_METADATA_UPDATE');
2017-11-30 09:33:09 +00:00
var metaObj, isTemplate;
2017-08-30 09:10:57 +00:00
nThen(function (waitFor) {
2017-11-30 09:33:09 +00:00
Cryptpad.getMetadata(waitFor(function (err, m) {
if (err) { console.log(err); }
metaObj = m;
2017-12-07 17:51:50 +00:00
edPublic = metaObj.priv.edPublic; // needed to create an owned pad
2017-11-30 09:33:09 +00:00
}));
Cryptpad.isTemplate(window.location.href, waitFor(function (err, t) {
2017-08-30 09:10:57 +00:00
if (err) { console.log(err); }
2017-11-30 09:33:09 +00:00
isTemplate = t;
2017-08-30 09:10:57 +00:00
}));
}).nThen(function (/*waitFor*/) {
2017-11-30 14:01:17 +00:00
metaObj.doc = {
2017-11-30 09:33:09 +00:00
defaultTitle: defaultTitle,
type: parsed.type
};
var additionalPriv = {
accountName: Utils.LocalStore.getAccountName(),
origin: window.location.origin,
pathname: window.location.pathname,
fileHost: ApiConfig.fileHost,
readOnly: readOnly,
availableHashes: hashes,
isTemplate: isTemplate,
feedbackAllowed: Utils.Feedback.state,
isPresent: parsed.hashData && parsed.hashData.present,
isEmbed: parsed.hashData && parsed.hashData.embed,
accounts: {
donateURL: Cryptpad.donateURL,
upgradeURL: Cryptpad.upgradeURL
2017-12-07 17:51:50 +00:00
},
2017-12-11 11:19:44 +00:00
isNewFile: isNewFile,
isDeleted: isNewFile && window.location.hash.length > 0,
forceCreationScreen: forceCreationScreen,
2018-04-27 15:23:23 +00:00
password: password,
channel: secret.channel
2017-09-12 16:40:11 +00:00
};
2017-11-30 09:33:09 +00:00
for (var k in additionalPriv) { metaObj.priv[k] = additionalPriv[k]; }
2017-09-12 16:40:11 +00:00
if (cfg.addData) {
cfg.addData(metaObj.priv, Cryptpad);
}
2017-11-30 16:21:58 +00:00
2017-09-12 16:40:11 +00:00
sframeChan.event('EV_METADATA_UPDATE', metaObj);
2017-08-30 09:10:57 +00:00
});
};
2017-11-30 09:33:09 +00:00
Cryptpad.onMetadataChanged(updateMeta);
2017-08-30 09:10:57 +00:00
sframeChan.onReg('EV_METADATA_UPDATE', updateMeta);
2017-11-23 11:28:49 +00:00
Utils.LocalStore.onLogout(function () {
sframeChan.event('EV_LOGOUT');
});
2017-12-14 10:34:44 +00:00
Test.registerOuter(sframeChan);
// Put in the following function the RPC queries that should also work in filepicker
var addCommonRpc = function (sframeChan) {
sframeChan.on('Q_ANON_RPC_MESSAGE', function (data, cb) {
Cryptpad.anonRpcMsg(data.msg, data.content, function (err, response) {
cb({error: err, response: response});
});
});
sframeChan.on('Q_GET_PIN_LIMIT_STATUS', function (data, cb) {
Cryptpad.isOverPinLimit(function (e, overLimit, limits) {
cb({
error: e,
overLimit: overLimit,
limits: limits
});
});
});
sframeChan.on('Q_THUMBNAIL_GET', function (data, cb) {
2017-11-23 11:28:49 +00:00
Utils.LocalStore.getThumbnail(data.key, function (e, data) {
cb({
error: e,
data: data
});
});
});
sframeChan.on('Q_THUMBNAIL_SET', function (data, cb) {
2017-11-23 11:28:49 +00:00
Utils.LocalStore.setThumbnail(data.key, data.value, function (e) {
cb({error:e});
});
2017-08-30 09:10:57 +00:00
});
};
addCommonRpc(sframeChan);
2017-08-30 09:10:57 +00:00
var currentTitle;
var currentTabTitle;
var setDocumentTitle = function () {
if (!currentTabTitle) {
document.title = currentTitle || 'CryptPad';
return;
}
var title = currentTabTitle.replace(/\{title\}/g, currentTitle || 'CryptPad');
document.title = title;
};
2017-08-30 09:10:57 +00:00
sframeChan.on('Q_SET_PAD_TITLE_IN_DRIVE', function (newTitle, cb) {
currentTitle = newTitle;
setDocumentTitle();
2018-04-27 09:54:23 +00:00
var data = {
password: password,
title: newTitle,
channel: secret.channel,
path: initialPathInDrive // Where to store the pad if we don't have it in our drive
};
Cryptpad.setPadTitle(data, function (err) {
cb(err);
2017-08-30 09:10:57 +00:00
});
});
sframeChan.on('EV_SET_TAB_TITLE', function (newTabTitle) {
currentTabTitle = newTabTitle;
setDocumentTitle();
});
2017-08-30 09:10:57 +00:00
sframeChan.on('Q_SETTINGS_SET_DISPLAY_NAME', function (newName, cb) {
Cryptpad.setDisplayName(newName, function (err) {
2017-08-30 09:10:57 +00:00
if (err) {
console.log("Couldn't set username");
console.error(err);
cb('ERROR');
return;
}
2017-11-30 17:22:26 +00:00
Cryptpad.changeMetadata();
2017-08-30 09:10:57 +00:00
cb();
});
});
sframeChan.on('Q_LOGOUT', function (data, cb) {
2017-11-23 11:28:49 +00:00
Utils.LocalStore.logout(cb);
2017-08-30 09:10:57 +00:00
});
sframeChan.on('EV_NOTIFY', function () {
Notifier.notify();
2017-08-30 09:10:57 +00:00
});
sframeChan.on('Q_SET_LOGIN_REDIRECT', function (data, cb) {
sessionStorage.redirectTo = window.location.href;
cb();
});
sframeChan.on('Q_MOVE_TO_TRASH', function (data, cb) {
cb = cb || $.noop;
if (readOnly && hashes.editHash) {
var appPath = window.location.pathname;
Cryptpad.moveToTrash(cb, appPath + '#' + hashes.editHash);
return;
}
2017-08-30 09:10:57 +00:00
Cryptpad.moveToTrash(cb);
});
sframeChan.on('Q_SAVE_AS_TEMPLATE', function (data, cb) {
Cryptpad.saveAsTemplate(Cryptget.put, data, cb);
});
sframeChan.on('Q_SEND_FRIEND_REQUEST', function (netfluxId, cb) {
2017-12-15 15:19:22 +00:00
Cryptpad.inviteFromUserlist(netfluxId, cb);
2017-08-30 09:10:57 +00:00
});
2017-12-15 15:19:22 +00:00
Cryptpad.messaging.onFriendRequest.reg(function (confirmText, cb) {
2017-08-30 09:10:57 +00:00
sframeChan.query('Q_INCOMING_FRIEND_REQUEST', confirmText, function (err, data) {
cb(data);
});
2017-12-15 15:19:22 +00:00
});
Cryptpad.messaging.onFriendComplete.reg(function (data) {
2017-08-30 09:10:57 +00:00
sframeChan.event('EV_FRIEND_REQUEST', data);
2017-12-15 15:19:22 +00:00
});
2017-08-30 09:10:57 +00:00
sframeChan.on('Q_GET_FULL_HISTORY', function (data, cb) {
var crypto = Crypto.createEncryptor(secret.keys);
Cryptpad.getFullHistory({
channel: secret.channel,
validateKey: secret.keys.validateKey
}, function (encryptedMsgs) {
cb(encryptedMsgs.map(function (msg) {
// The 3rd parameter "true" means we're going to skip signature validation.
// We don't need it since the message is already validated serverside by hk
return crypto.decrypt(msg, true, true);
}));
});
2017-08-30 09:10:57 +00:00
});
2017-08-30 16:47:50 +00:00
sframeChan.on('Q_GET_PAD_ATTRIBUTE', function (data, cb) {
var href;
if (readOnly && hashes.editHash) {
// If we have a stronger hash, use it for pad attributes
href = window.location.pathname + '#' + hashes.editHash;
}
if (data.href) { href = data.href; }
2017-08-30 16:47:50 +00:00
Cryptpad.getPadAttribute(data.key, function (e, data) {
cb({
error: e,
data: data
});
}, href);
2017-08-30 16:47:50 +00:00
});
sframeChan.on('Q_SET_PAD_ATTRIBUTE', function (data, cb) {
var href;
if (readOnly && hashes.editHash) {
// If we have a stronger hash, use it for pad attributes
href = window.location.pathname + '#' + hashes.editHash;
}
if (data.href) { href = data.href; }
2017-08-30 16:47:50 +00:00
Cryptpad.setPadAttribute(data.key, data.value, function (e) {
cb({error:e});
}, href);
2017-08-30 16:47:50 +00:00
});
sframeChan.on('Q_GET_ATTRIBUTE', function (data, cb) {
Cryptpad.getAttribute(data.key, function (e, data) {
cb({
error: e,
data: data
});
});
});
sframeChan.on('Q_SET_ATTRIBUTE', function (data, cb) {
Cryptpad.setAttribute(data.key, data.value, function (e) {
cb({error:e});
});
});
2017-09-22 17:35:06 +00:00
sframeChan.on('Q_SESSIONSTORAGE_PUT', function (data, cb) {
sessionStorage[data.key] = data.value;
cb();
});
2017-09-07 16:56:58 +00:00
// Present mode URL
sframeChan.on('Q_PRESENT_URL_GET_VALUE', function (data, cb) {
2017-11-13 15:32:40 +00:00
var parsed = Utils.Hash.parsePadUrl(window.location.href);
2017-09-07 16:56:58 +00:00
cb(parsed.hashData && parsed.hashData.present);
});
sframeChan.on('EV_PRESENT_URL_SET_VALUE', function (data) {
2017-11-13 15:32:40 +00:00
var parsed = Utils.Hash.parsePadUrl(window.location.href);
2017-09-07 16:56:58 +00:00
window.location.href = parsed.getUrl({
embed: parsed.hashData.embed,
present: data
});
});
2017-08-30 16:47:50 +00:00
2017-09-07 16:56:58 +00:00
// File upload
var onFileUpload = function (sframeChan, data, cb) {
2017-11-09 17:17:49 +00:00
require(['/common/outer/upload.js'], function (Files) {
var sendEvent = function (data) {
sframeChan.event("EV_FILE_UPLOAD_STATE", data);
};
var updateProgress = function (progressValue) {
sendEvent({
progress: progressValue
});
};
var onComplete = function (href) {
sendEvent({
complete: true,
href: href
});
};
var onError = function (e) {
sendEvent({
error: e
});
};
var onPending = function (cb) {
sframeChan.query('Q_CANCEL_PENDING_FILE_UPLOAD', null, function (err, data) {
if (data) {
cb();
}
});
};
data.blob = Crypto.Nacl.util.decodeBase64(data.blob);
Files.upload(data, data.noStore, Cryptpad, updateProgress, onComplete, onError, onPending);
cb();
});
};
sframeChan.on('Q_UPLOAD_FILE', function (data, cb) {
onFileUpload(sframeChan, data, cb);
});
2017-09-07 16:56:58 +00:00
// File picker
var FP = {};
var initFilePicker = function (cfg) {
2018-04-13 16:52:55 +00:00
// cfg.hidden means pre-loading the filepicker while keeping it hidden.
// if cfg.hidden is true and the iframe already exists, do nothing
if (!FP.$iframe) {
var config = {};
config.onFilePicked = function (data) {
sframeChan.event('EV_FILE_PICKED', data);
};
config.onClose = function () {
FP.$iframe.hide();
};
config.onFileUpload = onFileUpload;
config.types = cfg;
config.addCommonRpc = addCommonRpc;
config.modules = {
Cryptpad: Cryptpad,
2017-11-23 11:28:49 +00:00
SFrameChannel: SFrameChannel,
Utils: Utils
};
FP.$iframe = $('<iframe>', {id: 'sbox-filePicker-iframe'}).appendTo($('body'));
FP.picker = FilePicker.create(config);
2018-04-13 16:52:55 +00:00
} else if (!cfg.hidden) {
FP.$iframe.show();
FP.picker.refresh(cfg);
}
if (cfg.hidden) {
FP.$iframe.hide();
return;
}
FP.$iframe.focus();
};
2017-09-05 09:35:15 +00:00
sframeChan.on('EV_FILE_PICKER_OPEN', function (data) {
initFilePicker(data);
});
sframeChan.on('Q_TEMPLATE_USE', function (href, cb) {
Cryptpad.useTemplate(href, Cryptget, cb);
});
sframeChan.on('Q_TEMPLATE_EXIST', function (type, cb) {
2017-11-30 09:33:09 +00:00
Cryptpad.listTemplates(type, function (err, templates) {
cb(templates.length > 0);
});
});
2018-04-27 15:23:23 +00:00
var getKey = function (href, channel) {
2018-03-13 10:31:08 +00:00
var parsed = Utils.Hash.parsePadUrl(href);
2018-04-27 15:23:23 +00:00
return 'thumbnail-' + parsed.type + '-' + channel;
2018-03-13 10:31:08 +00:00
};
sframeChan.on('Q_CREATE_TEMPLATES', function (type, cb) {
Cryptpad.getSecureFilesList({
types: [type],
where: ['template']
}, function (err, data) {
// NOTE: Never return data directly!
if (err) { return void cb({error: err}); }
var res = [];
nThen(function (waitFor) {
Object.keys(data).map(function (el) {
2018-04-27 15:23:23 +00:00
var k = getKey(data[el].href, data[el].channel);
2018-03-13 10:31:08 +00:00
Utils.LocalStore.getThumbnail(k, waitFor(function (e, thumb) {
res.push({
id: el,
name: data[el].filename || data[el].title || '?',
2018-04-13 16:52:55 +00:00
thumbnail: thumb,
used: data[el].used || 0
2018-03-13 10:31:08 +00:00
});
}));
});
}).nThen(function () {
cb({data: res});
});
});
});
sframeChan.on('EV_GOTO_URL', function (url) {
if (url) {
window.location.href = url;
} else {
window.location.reload();
}
});
2017-09-22 17:35:06 +00:00
sframeChan.on('EV_OPEN_URL', function (url) {
if (url) {
window.open(url);
}
});
2017-09-21 15:56:24 +00:00
sframeChan.on('Q_PIN_GET_USAGE', function (data, cb) {
Cryptpad.isOverPinLimit(function (err, overLimit, data) {
cb({
error: err,
data: data
});
});
});
sframeChan.on('Q_LANGUAGE_SET', function (data, cb) {
Cryptpad.setLanguage(data, cb);
});
sframeChan.on('Q_CONTACTS_CLEAR_OWNED_CHANNEL', function (channel, cb) {
Cryptpad.clearOwnedChannel(channel, cb);
});
sframeChan.on('Q_REMOVE_OWNED_CHANNEL', function (channel, cb) {
Cryptpad.removeOwnedChannel(channel, cb);
});
sframeChan.on('Q_GET_ALL_TAGS', function (data, cb) {
Cryptpad.listAllTags(function (err, tags) {
cb({
error: err,
tags: tags
});
});
});
2017-09-12 16:40:11 +00:00
if (cfg.addRpc) {
2017-11-13 15:32:40 +00:00
cfg.addRpc(sframeChan, Cryptpad, Utils);
2017-09-12 16:40:11 +00:00
}
2017-10-02 16:57:17 +00:00
if (cfg.messaging) {
sframeChan.on('Q_CONTACTS_GET_FRIEND_LIST', function (data, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.getFriendList(cb);
2017-10-02 16:57:17 +00:00
});
sframeChan.on('Q_CONTACTS_GET_MY_INFO', function (data, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.getMyInfo(cb);
2017-10-02 16:57:17 +00:00
});
sframeChan.on('Q_CONTACTS_GET_FRIEND_INFO', function (curvePublic, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.getFriendInfo(curvePublic, cb);
2017-10-23 10:33:50 +00:00
});
sframeChan.on('Q_CONTACTS_REMOVE_FRIEND', function (curvePublic, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.removeFriend(curvePublic, cb);
2017-10-02 16:57:17 +00:00
});
sframeChan.on('Q_CONTACTS_OPEN_FRIEND_CHANNEL', function (curvePublic, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.openFriendChannel(curvePublic, cb);
2017-10-02 16:57:17 +00:00
});
sframeChan.on('Q_CONTACTS_GET_STATUS', function (curvePublic, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.getFriendStatus(curvePublic, cb);
2017-10-02 16:57:17 +00:00
});
sframeChan.on('Q_CONTACTS_GET_MORE_HISTORY', function (opt, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.getMoreHistory(opt, cb);
2017-10-02 16:57:17 +00:00
});
sframeChan.on('Q_CONTACTS_SEND_MESSAGE', function (opt, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.sendMessage(opt, cb);
2017-10-02 16:57:17 +00:00
});
sframeChan.on('Q_CONTACTS_SET_CHANNEL_HEAD', function (opt, cb) {
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.setChannelHead(opt, cb);
2017-10-02 16:57:17 +00:00
});
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.onMessageEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_MESSAGE', data);
2017-10-02 16:57:17 +00:00
});
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.onJoinEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_JOIN', data);
2017-10-02 16:57:17 +00:00
});
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.onLeaveEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_LEAVE', data);
2017-10-02 16:57:17 +00:00
});
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.onUpdateEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_UPDATE', data);
2017-10-02 16:57:17 +00:00
});
2017-12-01 13:49:21 +00:00
Cryptpad.messenger.onFriendEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_FRIEND', data);
});
Cryptpad.messenger.onUnfriendEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_UNFRIEND', data);
2017-10-02 16:57:17 +00:00
});
}
2017-09-11 13:52:14 +00:00
2017-09-12 16:40:11 +00:00
2017-12-07 17:51:50 +00:00
// Join the netflux channel
var rtStarted = false;
var startRealtime = function (rtConfig) {
rtConfig = rtConfig || {};
2017-12-07 17:51:50 +00:00
rtStarted = true;
var replaceHash = function (hash) {
if (window.history && window.history.replaceState) {
if (!/^#/.test(hash)) { hash = '#' + hash; }
2018-03-13 10:31:08 +00:00
window.history.replaceState({}, window.document.title, hash);
2017-12-07 17:51:50 +00:00
if (typeof(window.onhashchange) === 'function') {
window.onhashchange();
}
return;
}
window.location.hash = hash;
};
2017-09-12 16:40:11 +00:00
2018-01-12 16:08:41 +00:00
var cpNfCfg = {
2017-12-07 17:51:50 +00:00
sframeChan: sframeChan,
channel: secret.channel,
padRpc: Cryptpad.padRpc,
validateKey: secret.keys.validateKey || undefined,
2017-12-12 11:06:31 +00:00
isNewHash: isNewHash,
2017-12-07 17:51:50 +00:00
readOnly: readOnly,
crypto: Crypto.createEncryptor(secret.keys),
onConnect: function () {
2017-12-07 17:51:50 +00:00
if (window.location.hash && window.location.hash !== '#') {
window.location = parsed.getUrl({
present: parsed.hashData.present,
embed: parsed.hashData.embed
});
return;
}
if (readOnly || cfg.noHash) { return; }
replaceHash(Utils.Hash.getEditHashFromKeys(secret));
2017-11-13 15:32:40 +00:00
}
};
2018-03-22 14:16:19 +00:00
nThen(function (waitFor) {
if (isNewFile && cfg.owned && !window.location.hash) {
Cryptpad.getMetadata(waitFor(function (err, m) {
cpNfCfg.owners = [m.priv.edPublic];
}));
} else if (isNewFile && !cfg.useCreationScreen && window.location.hash) {
console.log("new file with hash in the address bar in an app without pcs and which requires owners");
sframeChan.onReady(function () {
sframeChan.query("EV_LOADING_ERROR", "DELETED");
});
waitFor.abort();
2018-03-22 14:16:19 +00:00
}
}).nThen(function () {
Object.keys(rtConfig).forEach(function (k) {
cpNfCfg[k] = rtConfig[k];
});
CpNfOuter.start(cpNfCfg);
2017-12-07 17:51:50 +00:00
});
2017-11-13 15:32:40 +00:00
};
2017-12-07 17:51:50 +00:00
sframeChan.on('Q_CREATE_PAD', function (data, cb) {
if (!isNewFile || rtStarted) { return; }
// Create a new hash
password = data.password;
var newHash = Utils.Hash.createRandomHash(parsed.type, password);
secret = Utils.Hash.getSecrets(parsed.type, newHash, password);
2017-12-07 17:51:50 +00:00
// Update the hash in the address bar
var ohc = window.onhashchange;
window.onhashchange = function () {};
window.location.hash = newHash;
window.onhashchange = ohc;
ohc({reset: true});
// Update metadata values and send new metadata inside
2018-04-27 15:23:23 +00:00
parsed = Utils.Hash.parsePadUrl(window.location.href);
2017-12-07 17:51:50 +00:00
defaultTitle = Utils.Hash.getDefaultName(parsed);
hashes = Utils.Hash.getHashes(secret);
2017-12-07 17:51:50 +00:00
readOnly = false;
updateMeta();
var rtConfig = {};
if (data.owned) {
rtConfig.owners = [edPublic];
2017-08-30 09:10:57 +00:00
}
2017-12-11 11:19:44 +00:00
if (data.expire) {
rtConfig.expire = data.expire;
2017-12-11 11:19:44 +00:00
}
2018-03-13 10:31:08 +00:00
nThen(function(waitFor) {
if (data.templateId) {
if (data.templateId === -1) {
2018-04-27 09:54:23 +00:00
initialPathInDrive = ['template'];
2018-03-13 10:31:08 +00:00
return;
}
Cryptpad.getPadData(data.templateId, waitFor(function (err, d) {
data.template = d.href;
}));
}
}).nThen(function () {
if (data.template) {
// Pass rtConfig to useTemplate because Cryptput will create the file and
// we need to have the owners and expiration time in the first line on the
// server
var cryptputCfg = $.extend(true, {}, rtConfig, {password: password});
2018-03-13 10:31:08 +00:00
Cryptpad.useTemplate(data.template, Cryptget, function () {
startRealtime();
cb();
}, cryptputCfg);
2018-03-13 10:31:08 +00:00
return;
}
// Start realtime outside the iframe and callback
startRealtime(rtConfig);
cb();
});
2017-08-30 09:10:57 +00:00
});
2017-12-07 17:51:50 +00:00
sframeChan.ready();
Utils.Feedback.reportAppUsage();
2018-04-10 14:38:31 +00:00
if (!realtime && !Test.testing) { return; }
if (isNewFile && cfg.useCreationScreen && !Test.testing) { return; }
//if (isNewFile && Utils.LocalStore.isLoggedIn()
// && AppConfig.displayCreationScreen && cfg.useCreationScreen) { return; }
2017-12-07 17:51:50 +00:00
startRealtime();
2017-08-30 09:10:57 +00:00
});
};
return common;
});