cryptpad/www/common/outer/async-store.js

1285 lines
46 KiB
JavaScript
Raw Normal View History

2017-11-30 09:33:09 +00:00
define([
2018-03-19 13:04:44 +00:00
'json.sortify',
2017-11-30 09:33:09 +00:00
'/common/userObject.js',
'/common/migrate-user-object.js',
'/common/common-hash.js',
'/common/common-util.js',
'/common/common-constants.js',
'/common/common-feedback.js',
'/common/common-realtime.js',
2017-11-30 14:01:17 +00:00
'/common/common-messaging.js',
2017-12-01 13:49:21 +00:00
'/common/common-messenger.js',
'/common/outer/chainpad-netflux-worker.js',
2017-11-30 09:33:09 +00:00
'/common/outer/network-config.js',
'/customize/application_config.js',
2017-11-30 09:33:09 +00:00
'/bower_components/chainpad-crypto/crypto.js?v=0.1.5',
'/bower_components/chainpad/chainpad.dist.js',
'/bower_components/chainpad-listmap/chainpad-listmap.js',
2018-03-21 17:27:20 +00:00
'/bower_components/nthen/index.js',
'/bower_components/saferphore/index.js',
2018-03-19 13:04:44 +00:00
], function (Sortify, UserObject, Migrate, Hash, Util, Constants, Feedback, Realtime, Messaging, Messenger,
CpNfWorker, NetConfig, AppConfig,
2018-03-21 17:27:20 +00:00
Crypto, ChainPad, Listmap, nThen, Saferphore) {
2017-11-30 09:33:09 +00:00
var Store = {};
2017-11-30 14:01:17 +00:00
var postMessage = function () {};
2017-11-30 09:33:09 +00:00
var storeHash;
var store = window.CryptPad_AsyncStore = {};
2017-11-30 09:33:09 +00:00
var onSync = function (cb) {
Realtime.whenRealtimeSyncs(store.realtime, cb);
};
Store.get = function (key, cb) {
2017-11-30 17:22:26 +00:00
cb(Util.find(store.proxy, key));
2017-11-30 09:33:09 +00:00
};
Store.set = function (data, cb) {
var path = data.key.slice();
var key = path.pop();
var obj = Util.find(store.proxy, path);
if (!obj || typeof(obj) !== "object") { return void cb({error: 'INVALID_PATH'}); }
2017-11-30 14:01:17 +00:00
if (typeof data.value === "undefined") {
delete obj[key];
} else {
obj[key] = data.value;
}
2017-11-30 09:33:09 +00:00
onSync(cb);
};
Store.hasSigningKeys = function () {
if (!store.proxy) { return; }
return typeof(store.proxy.edPrivate) === 'string' &&
typeof(store.proxy.edPublic) === 'string';
};
Store.hasCurveKeys = function () {
if (!store.proxy) { return; }
return typeof(store.proxy.curvePrivate) === 'string' &&
typeof(store.proxy.curvePublic) === 'string';
};
var getUserChannelList = function () {
// start with your userHash...
var userHash = storeHash;
if (!userHash) { return null; }
2018-04-27 15:23:23 +00:00
// No password for drive
var secret = Hash.getSecrets('drive', userHash);
var userChannel = secret.channel;
2017-11-30 09:33:09 +00:00
if (!userChannel) { return null; }
// Get the list of pads' channel ID in your drive
// This list is filtered so that it doesn't include pad owned by other users (you should
// not pin these pads)
var files = store.userObject.getFiles([store.userObject.FILES_DATA]);
var edPublic = store.proxy.edPublic;
var list = files.map(function (id) {
var d = store.userObject.getFileData(id);
if (d.owners && d.owners.length && edPublic &&
d.owners.indexOf(edPublic) === -1) { return; }
2018-04-27 15:23:23 +00:00
return d.channel;
2017-11-30 09:33:09 +00:00
})
.filter(function (x) { return x; });
// Get the avatar
var profile = store.proxy.profile;
if (profile) {
var profileChan = profile.edit ? Hash.hrefToHexChannelId('/profile/#' + profile.edit, null) : null;
2017-11-30 09:33:09 +00:00
if (profileChan) { list.push(profileChan); }
var avatarChan = profile.avatar ? Hash.hrefToHexChannelId(profile.avatar, null) : null;
2018-05-17 09:46:16 +00:00
if (avatarChan) { list.push(Util.base64ToHex(avatarChan)); }
2017-11-30 09:33:09 +00:00
}
2017-11-30 14:01:17 +00:00
if (store.proxy.friends) {
var fList = Messaging.getFriendChannelsList(store.proxy);
2017-11-30 09:33:09 +00:00
list = list.concat(fList);
2017-11-30 14:01:17 +00:00
}
2017-11-30 09:33:09 +00:00
2018-04-27 15:23:23 +00:00
list.push(userChannel);
2017-11-30 09:33:09 +00:00
list.sort();
return list;
};
2018-01-25 16:54:21 +00:00
var getExpirableChannelList = function () {
var list = [];
store.userObject.getFiles([store.userObject.FILES_DATA]).forEach(function (id) {
var data = store.userObject.getFileData(id);
var edPublic = store.proxy.edPublic;
// Push channels owned by someone else or channel that should have expired
// because of the expiration time
if ((data.owners && data.owners.length && data.owners.indexOf(edPublic) === -1) ||
(data.expire && data.expire < (+new Date()))) {
2018-04-27 15:23:23 +00:00
list.push(data.channel);
2018-01-25 16:54:21 +00:00
}
});
return list;
2017-11-30 09:33:09 +00:00
};
2018-01-25 16:54:21 +00:00
var getCanonicalChannelList = function (expirable) {
var list = expirable ? getExpirableChannelList() : getUserChannelList();
return Util.deduplicateString(list).sort();
};
2017-11-30 09:33:09 +00:00
//////////////////////////////////////////////////////////////////
/////////////////////// RPC //////////////////////////////////////
//////////////////////////////////////////////////////////////////
Store.pinPads = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
if (typeof(cb) !== 'function') {
console.error('expected a callback');
}
2017-11-30 14:01:17 +00:00
store.rpc.pin(data, function (e, hash) {
2017-11-30 09:33:09 +00:00
if (e) { return void cb({error: e}); }
cb({hash: hash});
});
};
Store.unpinPads = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
2017-11-30 14:01:17 +00:00
store.rpc.unpin(data, function (e, hash) {
2017-11-30 09:33:09 +00:00
if (e) { return void cb({error: e}); }
cb({hash: hash});
});
};
var account = {};
Store.getPinnedUsage = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
store.rpc.getFileListSize(function (err, bytes) {
if (typeof(bytes) === 'number') {
account.usage = bytes;
}
cb({bytes: bytes});
});
};
// Update for all users from accounts and return current user limits
2017-11-30 14:01:17 +00:00
Store.updatePinLimit = function (data, cb) {
2017-11-30 09:33:09 +00:00
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
store.rpc.updatePinLimits(function (e, limit, plan, note) {
if (e) { return void cb({error: e}); }
account.limit = limit;
account.plan = plan;
account.note = note;
cb(account);
});
};
// Get current user limits
2017-11-30 14:01:17 +00:00
Store.getPinLimit = function (data, cb) {
2017-11-30 09:33:09 +00:00
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
var ALWAYS_REVALIDATE = true;
if (ALWAYS_REVALIDATE || typeof(account.limit) !== 'number' ||
typeof(account.plan) !== 'string' ||
typeof(account.note) !== 'string') {
return void store.rpc.getLimit(function (e, limit, plan, note) {
if (e) { return void cb({error: e}); }
account.limit = limit;
account.plan = plan;
account.note = note;
cb(account);
});
}
cb(account);
};
Store.clearOwnedChannel = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
2017-12-01 13:49:21 +00:00
store.rpc.clearOwnedChannel(data, function (err) {
cb({error:err});
2017-11-30 17:22:26 +00:00
});
2017-11-30 09:33:09 +00:00
};
Store.removeOwnedChannel = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
store.rpc.removeOwnedChannel(data, function (err) {
cb({error:err});
});
};
2018-01-25 16:54:21 +00:00
var arePinsSynced = function (cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
var list = getCanonicalChannelList(false);
var local = Hash.hashChannelList(list);
store.rpc.getServerHash(function (e, hash) {
if (e) { return void cb(e); }
cb(null, hash === local);
});
};
var resetPins = function (cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
var list = getCanonicalChannelList(false);
store.rpc.reset(list, function (e, hash) {
if (e) { return void cb(e); }
cb(null, hash);
});
};
2017-11-30 09:33:09 +00:00
Store.uploadComplete = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
2017-11-30 17:22:26 +00:00
store.rpc.uploadComplete(function (err, res) {
if (err) { return void cb({error:err}); }
cb(res);
});
2017-11-30 09:33:09 +00:00
};
Store.uploadStatus = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
2017-11-30 17:22:26 +00:00
store.rpc.uploadStatus(data.size, function (err, res) {
if (err) { return void cb({error:err}); }
cb(res);
});
2017-11-30 09:33:09 +00:00
};
Store.uploadCancel = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
2017-11-30 17:22:26 +00:00
store.rpc.uploadCancel(function (err, res) {
if (err) { return void cb({error:err}); }
cb(res);
});
2017-11-30 14:01:17 +00:00
};
2017-11-30 17:22:26 +00:00
Store.uploadChunk = function (data, cb) {
store.rpc.send.unauthenticated('UPLOAD', data.chunk, function (e, msg) {
cb({
error: e,
msg: msg
});
});
};
2017-11-30 09:33:09 +00:00
Store.initRpc = function (data, cb) {
2017-11-30 14:01:17 +00:00
require(['/common/pinpad.js'], function (Pinpad) {
2017-11-30 09:33:09 +00:00
Pinpad.create(store.network, store.proxy, function (e, call) {
if (e) { return void cb({error: e}); }
store.rpc = call;
2017-11-30 14:01:17 +00:00
Store.getPinLimit(null, function (obj) {
if (obj.error) { console.error(obj.error); }
account.limit = obj.limit;
account.plan = obj.plan;
account.note = obj.note;
cb(obj);
2017-11-30 09:33:09 +00:00
});
2017-11-30 14:01:17 +00:00
arePinsSynced(function (err, yes) {
2017-11-30 09:33:09 +00:00
if (!yes) {
2017-11-30 14:01:17 +00:00
resetPins(function (err) {
2017-11-30 09:33:09 +00:00
if (err) { return console.error(err); }
console.log('RESET DONE');
});
}
});
});
});
};
//////////////////////////////////////////////////////////////////
////////////////// ANON RPC //////////////////////////////////////
//////////////////////////////////////////////////////////////////
Store.anonRpcMsg = function (data, cb) {
if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); }
2017-11-30 14:01:17 +00:00
store.anon_rpc.send(data.msg, data.data, function (err, res) {
if (err) { return void cb({error: err}); }
cb(res);
});
2017-11-30 09:33:09 +00:00
};
Store.getFileSize = function (data, cb) {
if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); }
var channelId = Hash.hrefToHexChannelId(data.href, data.password);
2017-11-30 09:33:09 +00:00
store.anon_rpc.send("GET_FILE_SIZE", channelId, function (e, response) {
if (e) { return void cb({error: e}); }
if (response && response.length && typeof(response[0]) === 'number') {
return void cb({size: response[0]});
} else {
cb({error: 'INVALID_RESPONSE'});
}
});
};
Store.isNewChannel = function (data, cb) {
if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); }
var channelId = Hash.hrefToHexChannelId(data.href, data.password);
store.anon_rpc.send("IS_NEW_CHANNEL", channelId, function (e, response) {
if (e) { return void cb({error: e}); }
if (response && response.length && typeof(response[0]) === 'boolean') {
return void cb({
isNew: response[0]
});
} else {
cb({error: 'INVALID_RESPONSE'});
}
});
};
2017-11-30 09:33:09 +00:00
Store.getMultipleFileSize = function (data, cb) {
if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); }
if (!Array.isArray(data.files)) {
return void cb({error: 'INVALID_FILE_LIST'});
}
store.anon_rpc.send('GET_MULTIPLE_FILE_SIZE', data.files, function (e, res) {
if (e) { return void cb({error: e}); }
if (res && res.length && typeof(res[0]) === 'object') {
cb({size: res[0]});
} else {
cb({error: 'UNEXPECTED_RESPONSE'});
}
});
};
2018-01-29 11:40:09 +00:00
Store.getDeletedPads = function (data, cb) {
if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); }
var list = getCanonicalChannelList(true);
if (!Array.isArray(list)) {
return void cb({error: 'INVALID_FILE_LIST'});
}
store.anon_rpc.send('GET_DELETED_PADS', list, function (e, res) {
if (e) { return void cb({error: e}); }
if (res && res.length && Array.isArray(res[0])) {
cb(res[0]);
} else {
cb({error: 'UNEXPECTED_RESPONSE'});
}
});
};
2017-11-30 09:33:09 +00:00
Store.initAnonRpc = function (data, cb) {
require([
'/common/rpc.js',
], function (Rpc) {
Rpc.createAnonymous(store.network, function (e, call) {
if (e) { return void cb({error: e}); }
store.anon_rpc = call;
cb();
});
});
};
//////////////////////////////////////////////////////////////////
/////////////////////// Store ////////////////////////////////////
//////////////////////////////////////////////////////////////////
// Get the metadata for sframe-common-outer
Store.getMetadata = function (data, cb) {
2018-04-03 11:35:06 +00:00
var disableThumbnails = Util.find(store.proxy, ['settings', 'general', 'disableThumbnails']);
2017-11-30 09:33:09 +00:00
var metadata = {
// "user" is shared with everybody via the userlist
user: {
name: store.proxy[Constants.displayNameKey] || "",
2017-11-30 09:33:09 +00:00
uid: store.proxy.uid,
avatar: Util.find(store.proxy, ['profile', 'avatar']),
profile: Util.find(store.proxy, ['profile', 'view']),
curvePublic: store.proxy.curvePublic,
},
// "priv" is not shared with other users but is needed by the apps
priv: {
edPublic: store.proxy.edPublic,
2018-01-23 11:09:54 +00:00
friends: store.proxy.friends || {},
2017-11-30 09:33:09 +00:00
settings: store.proxy.settings,
2018-04-03 11:35:06 +00:00
thumbnails: disableThumbnails === false
2017-11-30 09:33:09 +00:00
}
};
cb(JSON.parse(JSON.stringify(metadata)));
};
2017-12-01 13:49:21 +00:00
var makePad = function (href, title) {
var now = +new Date();
return {
href: href,
atime: now,
ctime: now,
title: title || Hash.getDefaultName(Hash.parsePadUrl(href)),
};
};
Store.addPad = function (data, cb) {
if (!data.href) { return void cb({error:'NO_HREF'}); }
var pad = makePad(data.href, data.title);
if (data.owners) { pad.owners = data.owners; }
if (data.expire) { pad.expire = data.expire; }
if (data.password) { pad.password = data.password; }
2018-04-27 15:23:23 +00:00
if (data.channel) { pad.channel = data.channel; }
2017-12-01 13:49:21 +00:00
store.userObject.pushData(pad, function (e, id) {
if (e) { return void cb({error: "Error while adding a template:"+ e}); }
var path = data.path || ['root'];
store.userObject.add(id, path);
onSync(cb);
});
2017-11-30 09:33:09 +00:00
};
2018-03-21 17:27:20 +00:00
var getOwnedPads = function () {
var list = [];
store.userObject.getFiles([store.userObject.FILES_DATA]).forEach(function (id) {
var data = store.userObject.getFileData(id);
var edPublic = store.proxy.edPublic;
// Push channels owned by someone else or channel that should have expired
// because of the expiration time
if (data.owners && data.owners.length === 1 && data.owners.indexOf(edPublic) !== -1) {
2018-04-27 15:23:23 +00:00
list.push(data.channel);
2018-03-21 17:27:20 +00:00
}
});
if (store.proxy.todo) {
// No password for todo
list.push(Hash.hrefToHexChannelId('/todo/#' + store.proxy.todo, null));
}
if (store.proxy.profile && store.proxy.profile.edit) {
2018-04-27 15:23:23 +00:00
// No password for profile
list.push(Hash.hrefToHexChannelId('/profile/#' + store.proxy.profile.edit, null));
}
2018-03-21 17:27:20 +00:00
return list;
};
var removeOwnedPads = function (waitFor) {
// Delete owned pads
var ownedPads = getOwnedPads();
var sem = Saferphore.create(10);
ownedPads.forEach(function (c) {
var w = waitFor();
sem.take(function (give) {
Store.removeOwnedChannel(c, give(function (obj) {
if (obj && obj.error) { console.error(obj.error); }
w();
}));
});
});
};
2018-03-19 13:04:44 +00:00
Store.deleteAccount = function (data, cb) {
2018-03-21 17:27:20 +00:00
var edPublic = store.proxy.edPublic;
2018-04-27 15:23:23 +00:00
// No password for drive
2018-03-19 13:04:44 +00:00
var secret = Hash.getSecrets('drive', storeHash);
2018-03-20 14:09:31 +00:00
Store.anonRpcMsg({
msg: 'GET_METADATA',
data: secret.channel
}, function (data) {
var metadata = data[0];
// Owned drive
if (metadata && metadata.owners && metadata.owners.length === 1 &&
metadata.owners.indexOf(edPublic) !== -1) {
2018-03-21 17:27:20 +00:00
nThen(function (waitFor) {
var token = Math.floor(Math.random()*Number.MAX_SAFE_INTEGER);
store.proxy[Constants.tokenKey] = token;
postMessage("DELETE_ACCOUNT", token, waitFor());
}).nThen(function (waitFor) {
removeOwnedPads(waitFor);
}).nThen(function (waitFor) {
// Delete Pin Store
store.rpc.removePins(waitFor(function (err) {
if (err) { console.error(err); }
}));
}).nThen(function (waitFor) {
// Delete Drive
Store.removeOwnedChannel(secret.channel, waitFor());
}).nThen(function () {
store.network.disconnect();
cb({
state: true
});
});
2018-03-20 14:09:31 +00:00
return;
}
// Not owned drive
2018-03-21 17:27:20 +00:00
var toSign = {
intent: 'Please delete my account.'
};
toSign.drive = secret.channel;
toSign.edPublic = edPublic;
var signKey = Crypto.Nacl.util.decodeBase64(store.proxy.edPrivate);
2018-03-20 14:09:31 +00:00
var proof = Crypto.Nacl.sign.detached(Crypto.Nacl.util.decodeUTF8(Sortify(toSign)), signKey);
var check = Crypto.Nacl.sign.detached.verify(Crypto.Nacl.util.decodeUTF8(Sortify(toSign)),
proof,
Crypto.Nacl.util.decodeBase64(edPublic));
if (!check) { console.error('signed message failed verification'); }
2018-03-20 14:09:31 +00:00
var proofTxt = Crypto.Nacl.util.encodeBase64(proof);
cb({
proof: proofTxt,
toSign: JSON.parse(Sortify(toSign))
});
2018-03-19 13:04:44 +00:00
});
};
2017-11-30 09:33:09 +00:00
/**
* add a "What is CryptPad?" pad in the drive
* data
* - driveReadme
* - driveReadmeTitle
*/
Store.createReadme = function (data, cb) {
require(['/common/cryptget.js'], function (Crypt) {
var hash = Hash.createRandomHash('pad');
2017-11-30 09:33:09 +00:00
Crypt.put(hash, data.driveReadme, function (e) {
if (e) {
return void cb({ error: "Error while creating the default pad:"+ e});
}
var href = '/pad/#' + hash;
2018-04-27 15:23:23 +00:00
var channel = Hash.hrefToHexChannelId(href, null);
2017-11-30 09:33:09 +00:00
var fileData = {
href: href,
2018-04-27 15:23:23 +00:00
channel: channel,
2017-11-30 09:33:09 +00:00
title: data.driveReadmeTitle,
};
2018-04-27 16:04:21 +00:00
Store.addPad(fileData, cb);
2017-11-30 09:33:09 +00:00
});
});
};
/**
* Merge the anonymous drive into the user drive at registration
* data
* - anonHash
*/
Store.migrateAnonDrive = function (data, cb) {
require(['/common/mergeDrive.js'], function (Merge) {
var hash = data.anonHash;
Merge.anonDriveIntoUser(store, hash, cb);
});
};
var getAttributeObject = function (attr) {
if (typeof attr === "string") {
console.error('DEPRECATED: use setAttribute with an array, not a string');
return {
2017-11-30 14:01:17 +00:00
obj: store.proxy.settings,
2017-11-30 09:33:09 +00:00
key: attr
};
}
2017-11-30 14:01:17 +00:00
if (!Array.isArray(attr)) { return void console.error("Attribute must be string or array"); }
if (attr.length === 0) { return void console.error("Attribute can't be empty"); }
var obj = store.proxy.settings;
2017-11-30 09:33:09 +00:00
attr.forEach(function (el, i) {
if (i === attr.length-1) { return; }
if (!obj[el]) {
obj[el] = {};
}
2017-11-30 14:01:17 +00:00
else if (typeof obj[el] !== "object") { return void console.error("Wrong attribute"); }
2017-11-30 09:33:09 +00:00
obj = obj[el];
});
return {
obj: obj,
key: attr[attr.length-1]
};
};
// Set the display name (username) in the proxy
Store.setDisplayName = function (value, cb) {
store.proxy[Constants.displayNameKey] = value;
onSync(cb);
};
2017-12-01 13:49:21 +00:00
// Reset the drive part of the userObject (from settings)
Store.resetDrive = function (data, cb) {
2018-03-21 17:27:20 +00:00
nThen(function (waitFor) {
removeOwnedPads(waitFor);
2018-03-21 17:53:11 +00:00
}).nThen(function () {
2018-03-21 17:27:20 +00:00
store.proxy.drive = store.fo.getStructure();
onSync(cb);
});
2017-12-01 13:49:21 +00:00
};
2017-11-30 09:33:09 +00:00
/**
* Settings & pad attributes
* data
* - href (String)
* - attr (Array)
* - value (String)
*/
Store.setPadAttribute = function (data, cb) {
2017-11-30 14:01:17 +00:00
store.userObject.setPadAttribute(data.href, data.attr, data.value, function () {
2017-11-30 09:33:09 +00:00
onSync(cb);
});
};
Store.getPadAttribute = function (data, cb) {
2017-11-30 14:01:17 +00:00
store.userObject.getPadAttribute(data.href, data.attr, function (err, val) {
2017-11-30 09:33:09 +00:00
if (err) { return void cb({error: err}); }
cb(val);
});
};
Store.setAttribute = function (data, cb) {
try {
var object = getAttributeObject(data.attr);
object.obj[object.key] = data.value;
} catch (e) { return void cb({error: e}); }
onSync(cb);
};
Store.getAttribute = function (data, cb) {
var object;
try {
object = getAttributeObject(data.attr);
} catch (e) { return void cb({error: e}); }
cb(object.obj[object.key]);
};
// Tags
Store.listAllTags = function (data, cb) {
cb(store.userObject.getTagsList());
2017-11-30 09:33:09 +00:00
};
// Templates
Store.getTemplates = function (data, cb) {
2017-11-30 14:01:17 +00:00
var templateFiles = store.userObject.getFiles(['template']);
2017-11-30 09:33:09 +00:00
var res = [];
templateFiles.forEach(function (f) {
2017-11-30 14:01:17 +00:00
var data = store.userObject.getFileData(f);
2017-11-30 09:33:09 +00:00
res.push(JSON.parse(JSON.stringify(data)));
});
2017-11-30 14:01:17 +00:00
cb(res);
2017-11-30 09:33:09 +00:00
};
2018-04-13 16:52:55 +00:00
Store.incrementTemplateUse = function (href) {
store.userObject.getPadAttribute(href, 'used', function (err, data) {
// This is a not critical function, abort in case of error to make sure we won't
// create any issue with the user object or the async store
if (err) { return; }
var used = typeof data === "number" ? ++data : 1;
store.userObject.setPadAttribute(href, 'used', used);
});
};
2017-11-30 09:33:09 +00:00
// Pads
Store.moveToTrash = function (data, cb) {
var href = Hash.getRelativeHref(data.href);
store.userObject.forget(href);
onSync(cb);
};
Store.setPadTitle = function (data, cb) {
var title = data.title;
var href = data.href;
2018-04-27 09:54:23 +00:00
var channel = data.channel;
2017-11-30 09:33:09 +00:00
var p = Hash.parsePadUrl(href);
var h = p.hashData;
if (AppConfig.disableAnonymousStore && !store.loggedIn) { return void cb(); }
var owners;
2018-04-27 15:23:23 +00:00
if (Store.channel && Store.channel.wc && channel === Store.channel.wc.id) {
owners = Store.channel.data.owners || undefined;
}
var expire;
2018-04-27 15:23:23 +00:00
if (Store.channel && Store.channel.wc && channel === Store.channel.wc.id) {
expire = +Store.channel.data.expire || undefined;
}
2017-11-30 09:33:09 +00:00
var allPads = Util.find(store.proxy, ['drive', 'filesData']) || {};
var isStronger;
// If we don't find the new channel in our existing pads, we'll have to add the pads
// to filesData
var contains;
// Update all pads that use the same channel but with a weaker hash
// Edit > Edit (present) > View > View (present)
for (var id in allPads) {
2017-11-30 14:01:17 +00:00
var pad = allPads[id];
2017-11-30 09:33:09 +00:00
if (!pad.href) { continue; }
var p2 = Hash.parsePadUrl(pad.href);
var h2 = p2.hashData;
// Different types, proceed to the next one
// No hash data: corrupted pad?
if (p.type !== p2.type || !h2) { continue; }
2018-04-27 09:54:23 +00:00
// Different channel: continue
if (pad.channel !== channel) { continue; }
2017-11-30 09:33:09 +00:00
var shouldUpdate = p.hash.replace(/\/$/, '') === p2.hash.replace(/\/$/, '');
// If the hash is different but represents the same channel, check if weaker or stronger
2018-04-27 09:54:23 +00:00
if (!shouldUpdate && h.version !== 0) {
2017-11-30 09:33:09 +00:00
// We had view & now we have edit, update
if (h2.mode === 'view' && h.mode === 'edit') { shouldUpdate = true; }
// Same mode and we had present URL, update
else if (h.mode === h2.mode && h2.present) { shouldUpdate = true; }
// If we're here it means we have a weaker URL:
// update the date but keep the existing hash
else {
pad.atime = +new Date();
contains = true;
continue;
}
}
if (shouldUpdate) {
contains = true;
pad.atime = +new Date();
pad.title = title;
2018-01-25 16:54:21 +00:00
pad.owners = owners;
pad.expire = expire;
2017-11-30 09:33:09 +00:00
// If the href is different, it means we have a stronger one
if (href !== pad.href) { isStronger = true; }
pad.href = href;
}
}
if (isStronger) {
// If we have a stronger url, remove the possible weaker from the trash.
// If all of the weaker ones were in the trash, add the stronger to ROOT
2017-11-30 14:01:17 +00:00
store.userObject.restoreHref(href);
2017-11-30 09:33:09 +00:00
}
// Add the pad if it does not exist in our drive
if (!contains) {
Store.addPad({
href: href,
2018-04-27 09:54:23 +00:00
channel: channel,
2017-11-30 16:21:58 +00:00
title: title,
owners: owners,
expire: expire,
2018-04-27 09:54:23 +00:00
password: data.password,
path: data.path
2017-11-30 09:33:09 +00:00
}, cb);
return;
}
onSync(cb);
};
// Filepicker app
Store.getSecureFilesList = function (query, cb) {
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;
};
store.userObject.getFiles(where).forEach(function (id) {
var data = store.userObject.getFileData(id);
var parsed = Hash.parsePadUrl(data.href);
if ((!types || types.length === 0 || types.indexOf(parsed.type) !== -1) &&
hashes.indexOf(parsed.hash) === -1 &&
!isFiltered(parsed.type, data)) {
hashes.push(parsed.hash);
list[id] = data;
}
});
cb(list);
};
2018-03-13 10:31:08 +00:00
Store.getPadData = function (id, cb) {
cb(store.userObject.getFileData(id));
};
2017-11-30 09:33:09 +00:00
2017-12-01 13:49:21 +00:00
// Messaging (manage friends from the userlist)
2017-11-30 14:01:17 +00:00
var getMessagingCfg = function () {
return {
proxy: store.proxy,
realtime: store.realtime,
network: store.network,
updateMetadata: function () {
postMessage("UPDATE_METADATA");
},
pinPads: Store.pinPads,
2017-12-15 15:19:22 +00:00
friendComplete: function (data) {
postMessage("EV_FRIEND_COMPLETE", data);
2017-11-30 14:01:17 +00:00
},
2017-12-15 15:19:22 +00:00
friendRequest: function (data, cb) {
postMessage("Q_FRIEND_REQUEST", data, cb);
2017-11-30 14:01:17 +00:00
},
};
};
Store.inviteFromUserlist = function (data, cb) {
var messagingCfg = getMessagingCfg();
Messaging.inviteFromUserlist(messagingCfg, data, cb);
};
2017-11-30 09:33:09 +00:00
2017-12-01 13:49:21 +00:00
// Messenger
// Get hashes for the share button
Store.getStrongerHash = function (data, cb) {
var allPads = Util.find(store.proxy, ['drive', 'filesData']) || {};
// If we have a stronger version in drive, add it and add a redirect button
2018-04-27 15:23:23 +00:00
var stronger = Hash.findStronger(data.href, data.channel, allPads);
2017-12-01 13:49:21 +00:00
if (stronger) {
2018-04-27 15:23:23 +00:00
var parsed2 = Hash.parsePadUrl(stronger.href);
2017-12-01 13:49:21 +00:00
return void cb(parsed2.hash);
}
cb();
};
Store.messenger = {
getFriendList: function (data, cb) {
store.messenger.getFriendList(function (e, keys) {
cb({
error: e,
data: keys,
});
});
},
getMyInfo: function (data, cb) {
store.messenger.getMyInfo(function (e, info) {
cb({
error: e,
data: info,
});
});
},
getFriendInfo: function (data, cb) {
store.messenger.getFriendInfo(data, function (e, info) {
cb({
error: e,
data: info,
});
});
},
removeFriend: function (data, cb) {
store.messenger.removeFriend(data, function (e, info) {
cb({
error: e,
data: info,
});
});
},
openFriendChannel: function (data, cb) {
store.messenger.openFriendChannel(data, function (e) {
cb({ error: e, });
});
},
getFriendStatus: function (data, cb) {
store.messenger.getStatus(data, function (e, online) {
cb({
error: e,
data: online,
});
});
},
getMoreHistory: function (data, cb) {
store.messenger.getMoreHistory(data.curvePublic, data.sig, data.count, function (e, history) {
cb({
error: e,
data: history,
});
});
},
sendMessage: function (data, cb) {
store.messenger.sendMessage(data.curvePublic, data.content, function (e) {
cb({
error: e,
});
});
},
setChannelHead: function (data, cb) {
store.messenger.setChannelHead(data.curvePublic, data.sig, function (e) {
cb({
error: e
});
});
}
};
//////////////////////////////////////////////////////////////////
/////////////////////// PAD //////////////////////////////////////
//////////////////////////////////////////////////////////////////
// TODO with sharedworker
// channel will be an object storing the webchannel associated to each browser tab
var channel = Store.channel = {
queue: [],
data: {}
};
Store.joinPad = function (data, cb) {
var conf = {
onReady: function (padData) {
channel.data = padData || {};
postMessage("PAD_READY");
}, // post EV_PAD_READY
onMessage: function (user, m, validateKey) {
postMessage("PAD_MESSAGE", {
user: user,
msg: m,
validateKey: validateKey
});
}, // post EV_PAD_MESSAGE
onJoin: function (m) {
postMessage("PAD_JOIN", m);
}, // post EV_PAD_JOIN
onLeave: function (m) {
postMessage("PAD_LEAVE", m);
}, // post EV_PAD_LEAVE
onDisconnect: function () {
postMessage("PAD_DISCONNECT");
}, // post EV_PAD_DISCONNECT
2018-02-13 17:20:13 +00:00
onError: function (err) {
postMessage("PAD_ERROR", err);
}, // post EV_PAD_ERROR
channel: data.channel,
validateKey: data.validateKey,
2017-12-07 17:51:50 +00:00
owners: data.owners,
password: data.password,
expire: data.expire,
network: store.network,
readOnly: data.readOnly,
onConnect: function (wc, sendMessage) {
channel.sendMessage = sendMessage;
channel.wc = wc;
channel.queue.forEach(function (data) {
sendMessage(data.message);
});
cb({
myID: wc.myID,
id: wc.id,
members: wc.members
});
}
};
CpNfWorker.start(conf);
};
Store.sendPadMsg = function (data, cb) {
if (!channel.wc) { channel.queue.push(data); }
channel.sendMessage(data, cb);
};
// TODO
// GET_FULL_HISTORY from sframe-common-outer
Store.getFullHistory = function (data, cb) {
var network = store.network;
var hkn = network.historyKeeper;
//var crypto = Crypto.createEncryptor(data.keys);
// Get the history messages and send them to the iframe
var parse = function (msg) {
try {
return JSON.parse(msg);
} catch (e) {
return null;
}
};
var msgs = [];
var onMsg = function (msg) {
var parsed = parse(msg);
if (parsed[0] === 'FULL_HISTORY_END') {
cb(msgs);
return;
}
if (parsed[0] !== 'FULL_HISTORY') { return; }
if (parsed[1] && parsed[1].validateKey) { // First message
return;
}
if (parsed[1][3] !== data.channel) { return; }
msg = parsed[1][4];
if (msg) {
msg = msg.replace(/cp\|(([A-Za-z0-9+\/=]+)\|)?/, '');
//var decryptedMsg = crypto.decrypt(msg, true);
msgs.push(msg);
}
};
network.on('message', onMsg);
network.sendto(hkn, JSON.stringify(['GET_FULL_HISTORY', data.channel, data.validateKey]));
};
// TODO with sharedworker
// when the tab is closed, leave the pad
// Drive
Store.userObjectCommand = function (cmdData, cb) {
if (!cmdData || !cmdData.cmd) { return; }
var data = cmdData.data;
switch (cmdData.cmd) {
case 'move':
store.userObject.move(data.paths, data.newPath, cb); break;
case 'restore':
store.userObject.restore(data.path, cb); break;
case 'addFolder':
store.userObject.addFolder(data.path, data.name, cb); break;
case 'delete':
store.userObject.delete(data.paths, cb, data.nocheck, data.isOwnPadRemoved); break;
case 'emptyTrash':
store.userObject.emptyTrash(cb); break;
case 'rename':
store.userObject.rename(data.path, data.newName, cb); break;
default:
cb();
}
};
//////////////////////////////////////////////////////////////////
/////////////////////// Init /////////////////////////////////////
//////////////////////////////////////////////////////////////////
2017-11-30 09:33:09 +00:00
var onReady = function (returned, cb) {
var proxy = store.proxy;
var userObject = store.userObject = UserObject.init(proxy.drive, {
2017-11-30 16:21:58 +00:00
pinPads: Store.pinPads,
unpinPads: Store.unpinPads,
removeOwnedChannel: Store.removeOwnedChannel,
edPublic: store.proxy.edPublic,
loggedIn: store.loggedIn,
log: function (msg) {
postMessage("DRIVE_LOG", msg);
}
2017-11-30 09:33:09 +00:00
});
2018-05-03 15:59:22 +00:00
nThen(function (waitFor) {
postMessage('LOADING_DRIVE', {
state: 2
});
2018-05-03 15:59:22 +00:00
userObject.migrate(waitFor());
}).nThen(function (waitFor) {
Migrate(proxy, waitFor(), function (version, progress) {
postMessage('LOADING_DRIVE', {
state: 2,
progress: progress
});
});
2018-05-03 15:59:22 +00:00
}).nThen(function () {
postMessage('LOADING_DRIVE', {
state: 3
});
2017-11-30 14:01:17 +00:00
userObject.fixFiles();
2017-11-30 09:33:09 +00:00
var requestLogin = function () {
postMessage("REQUEST_LOGIN");
};
if (store.loggedIn) {
/* This isn't truly secure, since anyone who can read the user's object can
set their local loginToken to match that in the object. However, it exposes
a UI that will work most of the time. */
// every user object should have a persistent, random number
if (typeof(proxy.loginToken) !== 'number') {
proxy[Constants.tokenKey] = Math.floor(Math.random()*Number.MAX_SAFE_INTEGER);
}
returned[Constants.tokenKey] = proxy[Constants.tokenKey];
2017-11-30 14:01:17 +00:00
if (store.data.localToken && store.data.localToken !== proxy[Constants.tokenKey]) {
2017-11-30 09:33:09 +00:00
// the local number doesn't match that in
// the user object, request that they reauthenticate.
return void requestLogin();
}
}
if (!proxy.settings || !proxy.settings.general ||
typeof(proxy.settings.general.allowUserFeedback) !== 'boolean') {
proxy.settings = proxy.settings || {};
proxy.settings.general = proxy.settings.general || {};
proxy.settings.general.allowUserFeedback = true;
}
returned.feedback = proxy.settings.general.allowUserFeedback;
if (typeof(cb) === 'function') { cb(returned); }
if (typeof(proxy.uid) !== 'string' || proxy.uid.length !== 32) {
// even anonymous users should have a persistent, unique-ish id
console.log('generating a persistent identifier');
proxy.uid = Hash.createChannelId();
}
// if the user is logged in, but does not have signing keys...
if (store.loggedIn && (!Store.hasSigningKeys() ||
!Store.hasCurveKeys())) {
return void requestLogin();
}
proxy.on('change', [Constants.displayNameKey], function (o, n) {
if (typeof(n) !== "string") { return; }
postMessage("UPDATE_METADATA");
});
proxy.on('change', ['profile'], function () {
// Trigger userlist update when the avatar has changed
postMessage("UPDATE_METADATA");
});
proxy.on('change', ['friends'], function () {
// Trigger userlist update when the friendlist has changed
postMessage("UPDATE_METADATA");
});
2017-11-30 14:01:17 +00:00
proxy.on('change', ['settings'], function () {
postMessage("UPDATE_METADATA");
});
2017-11-30 09:33:09 +00:00
proxy.on('change', [Constants.tokenKey], function () {
2017-11-30 17:22:26 +00:00
postMessage("UPDATE_TOKEN", { token: proxy[Constants.tokenKey] });
2017-11-30 09:33:09 +00:00
});
2018-05-03 15:59:22 +00:00
});
2017-11-30 09:33:09 +00:00
};
var connect = function (data, cb) {
var hash = data.userHash || data.anonHash || Hash.createRandomHash('drive');
2017-11-30 09:33:09 +00:00
storeHash = hash;
if (!hash) {
throw new Error('[Store.init] Unable to find or create a drive hash. Aborting...');
}
2018-04-27 15:23:23 +00:00
// No password for drive
2017-11-30 09:33:09 +00:00
var secret = Hash.getSecrets('drive', hash);
var listmapConfig = {
data: {},
websocketURL: NetConfig.getWebsocketURL(),
channel: secret.channel,
readOnly: false,
validateKey: secret.keys.validateKey || undefined,
crypto: Crypto.createEncryptor(secret.keys),
userName: 'fs',
2018-05-03 16:17:18 +00:00
logLevel: 1,
2017-11-30 09:33:09 +00:00
ChainPad: ChainPad,
classic: true,
};
2017-12-05 17:09:43 +00:00
var rt = window.rt = Listmap.create(listmapConfig);
2017-11-30 14:01:17 +00:00
store.proxy = rt.proxy;
2017-11-30 09:33:09 +00:00
store.loggedIn = typeof(data.userHash) !== "undefined";
var returned = {};
2017-11-30 09:33:09 +00:00
rt.proxy.on('create', function (info) {
2017-11-30 14:01:17 +00:00
store.realtime = info.realtime;
store.network = info.network;
2017-11-30 09:33:09 +00:00
if (!data.userHash) {
returned.anonHash = Hash.getEditHashFromKeys(secret);
2017-11-30 09:33:09 +00:00
}
}).on('ready', function () {
if (store.userObject) { return; } // the store is already ready, it is a reconnection
if (!rt.proxy.drive || typeof(rt.proxy.drive) !== 'object') { rt.proxy.drive = {}; }
var drive = rt.proxy.drive;
// Creating a new anon drive: import anon pads from localStorage
if ((!drive[Constants.oldStorageKey] || !Array.isArray(drive[Constants.oldStorageKey]))
&& !drive['filesData']) {
drive[Constants.oldStorageKey] = [];
}
postMessage('LOADING_DRIVE', { state: 1 });
2017-11-30 09:33:09 +00:00
// Drive already exist: return the existing drive, don't load data from legacy store
onReady(returned, cb);
})
.on('change', ['drive', 'migrate'], function () {
var path = arguments[2];
var value = arguments[1];
if (path[0] === 'drive' && path[1] === "migrate" && value === 1) {
rt.network.disconnect();
rt.realtime.abort();
2017-12-05 17:09:43 +00:00
postMessage('NETWORK_DISCONNECT');
2017-11-30 09:33:09 +00:00
}
});
rt.proxy.on('disconnect', function () {
postMessage('NETWORK_DISCONNECT');
});
rt.proxy.on('reconnect', function (info) {
postMessage('NETWORK_RECONNECT', {myId: info.myId});
});
2017-11-30 09:33:09 +00:00
};
/**
* Data:
* - userHash or anonHash
* Todo in cb
* - LocalStore.setFSHash if needed
* - sessionStorage.User_Hash
* - stuff with tokenKey
* Event to outer
* - requestLogin
*/
var initialized = false;
Store.init = function (data, callback) {
if (initialized) {
return void callback({
2017-12-01 17:29:59 +00:00
state: 'ALREADY_INIT',
returned: store.returned
2017-11-30 09:33:09 +00:00
});
}
initialized = true;
2017-11-30 14:01:17 +00:00
postMessage = function (cmd, d, cb) {
2017-11-30 09:33:09 +00:00
setTimeout(function () {
2017-12-01 13:49:21 +00:00
data.query(cmd, d, cb); // TODO temporary, will be replaced by webworker channel
2017-11-30 09:33:09 +00:00
});
};
2017-11-30 14:01:17 +00:00
store.data = data;
2017-11-30 09:33:09 +00:00
connect(data, function (ret) {
if (Object.keys(store.proxy).length === 1) {
Feedback.send("FIRST_APP_USE", true);
}
2017-12-01 17:29:59 +00:00
store.returned = ret;
2017-11-30 09:33:09 +00:00
callback(ret);
2017-11-30 14:01:17 +00:00
var messagingCfg = getMessagingCfg();
Messaging.addDirectMessageHandler(messagingCfg);
2017-12-05 17:09:43 +00:00
// Send events whenever there is a change or a removal in the drive
if (data.driveEvents) {
store.proxy.on('change', [], function (o, n, p) {
postMessage('DRIVE_CHANGE', {
old: o,
new: n,
path: p
2017-12-05 17:10:53 +00:00
});
2017-12-05 17:09:43 +00:00
});
store.proxy.on('remove', [], function (o, p) {
postMessage('DRIVE_REMOVE', {
old: o,
path: p
2017-12-05 17:10:53 +00:00
});
2017-12-05 17:09:43 +00:00
});
}
2017-12-01 13:49:21 +00:00
if (data.messenger) {
2017-12-05 17:10:53 +00:00
var messenger = store.messenger = Messenger.messenger(store);
2017-12-01 13:49:21 +00:00
messenger.on('message', function (message) {
postMessage('CONTACTS_MESSAGE', message);
});
messenger.on('join', function (curvePublic, channel) {
postMessage('CONTACTS_JOIN', {
curvePublic: curvePublic,
channel: channel,
});
});
messenger.on('leave', function (curvePublic, channel) {
postMessage('CONTACTS_LEAVE', {
curvePublic: curvePublic,
channel: channel,
});
});
messenger.on('update', function (info, curvePublic) {
postMessage('CONTACTS_UPDATE', {
curvePublic: curvePublic,
info: info,
});
});
messenger.on('friend', function (curvePublic) {
postMessage('CONTACTS_FRIEND', {
curvePublic: curvePublic,
});
});
messenger.on('unfriend', function (curvePublic) {
postMessage('CONTACTS_UNFRIEND', {
curvePublic: curvePublic,
});
});
}
2017-11-30 09:33:09 +00:00
});
};
Store.disconnect = function () {
if (!store.network) { return; }
store.network.disconnect();
};
return Store;
});