cryptpad/www/profile/main.js

83 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-10-30 17:49:28 +00:00
// Load #1, load as little as possible because we are in a race to get the loading screen up.
2017-06-27 16:58:20 +00:00
define([
2017-10-30 17:49:28 +00:00
'/bower_components/nthen/index.js',
'/api/config',
2017-12-01 15:05:20 +00:00
'/common/dom-ready.js',
2017-11-13 15:32:40 +00:00
'/common/sframe-common-outer.js',
2020-11-04 10:22:31 +00:00
], function (nThen, ApiConfig, DomReady, SFCommonO) {
2017-10-30 17:49:28 +00:00
// Loaded in load #2
nThen(function (waitFor) {
2017-12-01 15:05:20 +00:00
DomReady.onReady(waitFor());
2017-10-30 17:49:28 +00:00
}).nThen(function (waitFor) {
2020-11-04 10:22:31 +00:00
SFCommonO.initIframe(waitFor);
2017-10-30 17:49:28 +00:00
}).nThen(function (/*waitFor*/) {
2017-11-30 09:33:09 +00:00
var getSecrets = function (Cryptpad, Utils, cb) {
2017-11-13 15:32:40 +00:00
var Hash = Utils.Hash;
2017-10-30 17:49:28 +00:00
// 1st case: visiting someone else's profile with hash in the URL
if (window.location.hash) {
2018-04-27 15:23:23 +00:00
// No password for profiles
2017-11-30 09:33:09 +00:00
return void cb(null, Hash.getSecrets('profile', window.location.hash.slice(1)));
2017-06-28 08:59:29 +00:00
}
2017-11-30 09:33:09 +00:00
nThen(function (waitFor) {
// 2nd case: visiting our own existing profile
Cryptpad.getProfileEditUrl(waitFor(function (hash) {
waitFor.abort();
return void cb(null, Hash.getSecrets('profile', hash));
2017-11-30 09:33:09 +00:00
}));
}).nThen(function () {
if (!Utils.LocalStore.isLoggedIn()) {
// Unregistered users can't create a profile
window.location.href = '/drive/';
return void cb();
}
2018-04-27 15:23:23 +00:00
// No password for profile
var hash = Hash.createRandomHash('profile');
2017-11-30 09:33:09 +00:00
var secret = Hash.getSecrets('profile', hash);
Cryptpad.pinPads([secret.channel], function (e) {
if (e) {
if (e === 'E_OVER_LIMIT') {
// TODO
}
return;
//return void UI.log(Messages._getKey('profile_error', [e])) // TODO
}
var profile = {};
profile.edit = Utils.Hash.getEditHashFromKeys(secret);
profile.view = Utils.Hash.getViewHashFromKeys(secret);
2017-11-30 09:33:09 +00:00
Cryptpad.setNewProfile(profile);
});
cb(null, secret);
2017-06-28 14:59:35 +00:00
});
2017-06-27 16:58:20 +00:00
};
var addData = function (meta, Cryptad, user) {
meta.isOwnProfile = !window.location.hash ||
window.location.hash.slice(1) === user.profile;
};
2017-11-13 15:32:40 +00:00
var addRpc = function (sframeChan, Cryptpad, Utils) {
2017-10-30 17:49:28 +00:00
// Adding a new avatar from the profile: pin it and store it in the object
sframeChan.on('Q_PROFILE_AVATAR_ADD', function (data, cb) {
var chanId = Utils.Hash.hrefToHexChannelId(data, null);
2017-10-30 17:49:28 +00:00
Cryptpad.pinPads([chanId], function (e) {
if (e) { return void cb(e); }
2017-11-30 14:01:17 +00:00
Cryptpad.setAvatar(data, cb);
2017-10-30 17:49:28 +00:00
});
2017-06-28 14:59:35 +00:00
});
2017-10-30 17:49:28 +00:00
// Removing the avatar from the profile: unpin it
sframeChan.on('Q_PROFILE_AVATAR_REMOVE', function (data, cb) {
var chanId = Utils.Hash.hrefToHexChannelId(data, null);
2017-11-30 14:01:17 +00:00
Cryptpad.unpinPads([chanId], function () {
Cryptpad.setAvatar(undefined, cb);
2017-10-30 17:49:28 +00:00
});
});
};
SFCommonO.start({
getSecrets: getSecrets,
noHash: true, // Don't add the hash in the URL if it doesn't already exist
addRpc: addRpc,
addData: addData,
2018-03-22 14:16:19 +00:00
owned: true
2017-06-27 16:58:20 +00:00
});
});
});