Create a team

pull/1/head
yflory 6 years ago
parent 798296489a
commit 1b31d8f7c6

@ -60,7 +60,7 @@ define([
} }
}; };
var onSync = function (teamId, cb) { var onSync = Store.onSync = function (teamId, cb) {
var s = getStore(teamId); var s = getStore(teamId);
if (!s) { return void cb({ error: 'ENOTFOUND' }); } if (!s) { return void cb({ error: 'ENOTFOUND' }); }
nThen(function (waitFor) { nThen(function (waitFor) {
@ -735,7 +735,7 @@ define([
store.proxy[Constants.displayNameKey] = value; store.proxy[Constants.displayNameKey] = value;
broadcast([clientId], "UPDATE_METADATA"); broadcast([clientId], "UPDATE_METADATA");
Messaging.updateMyData(store); Messaging.updateMyData(store);
onSync(cb); onSync(null, cb);
}; };
// Reset the drive part of the userObject (from settings) // Reset the drive part of the userObject (from settings)
@ -747,7 +747,7 @@ define([
sendDriveEvent('DRIVE_CHANGE', { sendDriveEvent('DRIVE_CHANGE', {
path: ['drive', 'filesData'] path: ['drive', 'filesData']
}, clientId); }, clientId);
onSync(cb); onSync(null, cb);
}); });
}; };
@ -830,7 +830,7 @@ define([
var object = getAttributeObject(data.attr); var object = getAttributeObject(data.attr);
object.obj[object.key] = data.value; object.obj[object.key] = data.value;
} catch (e) { return void cb({error: e}); } } catch (e) { return void cb({error: e}); }
onSync(function () { onSync(null, function () {
cb(); cb();
broadcast([], "UPDATE_METADATA"); broadcast([], "UPDATE_METADATA");
}); });
@ -1209,6 +1209,7 @@ define([
var loadUniversal = function (Module, type, waitFor) { var loadUniversal = function (Module, type, waitFor) {
if (store.modules[type]) { return; } if (store.modules[type]) { return; }
store.modules[type] = Module.init({ store.modules[type] = Module.init({
Store: Store,
store: store, store: store,
updateMetadata: function () { updateMetadata: function () {
broadcast([], "UPDATE_METADATA"); broadcast([], "UPDATE_METADATA");
@ -1286,7 +1287,7 @@ define([
store.mailbox.open('supportadmin', box, function () { store.mailbox.open('supportadmin', box, function () {
console.log('ready'); console.log('ready');
}); });
onSync(cb); onSync(null, cb);
}; };
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
@ -1970,7 +1971,7 @@ define([
}; };
if (!proxy.settings) { proxy.settings = {}; } if (!proxy.settings) { proxy.settings = {}; }
var manager = store.manager = ProxyManager.create(proxy.drive, { var manager = store.manager = ProxyManager.create(proxy.drive, {
onSync: onSync, onSync: function (cb) { onSync(null, cb); },
edPublic: proxy.edPublic, edPublic: proxy.edPublic,
pin: pin, pin: pin,
unpin: unpin, unpin: unpin,

@ -4,16 +4,21 @@ define([
'/common/common-constants.js', '/common/common-constants.js',
'/common/common-realtime.js', '/common/common-realtime.js',
'/common/proxy-manager.js',
'/common/outer/sharedfolder.js', '/common/outer/sharedfolder.js',
'/bower_components/chainpad-listmap/chainpad-listmap.js', '/bower_components/chainpad-listmap/chainpad-listmap.js',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad/chainpad.dist.js', '/bower_components/chainpad/chainpad.dist.js',
'/bower_components/nthen/index.js',
'/bower_components/tweetnacl/nacl-fast.min.js',
], function (Util, Hash, Constants, Realtime, ], function (Util, Hash, Constants, Realtime,
SF, ProxyManager, SF,
Listmap, Crypto, ChainPad) { Listmap, Crypto, ChainPad, nThen) {
var Team = {}; var Team = {};
var Nacl = window.nacl;
var initializeTeams = function (ctx, cb) { var initializeTeams = function (ctx, cb) {
// XXX ? // XXX ?
cb(); cb();
@ -28,28 +33,146 @@ define([
// TODO: pin or unpin document added to a shared folder from someone who is not a member of the team // TODO: pin or unpin document added to a shared folder from someone who is not a member of the team
}; };
var onReady = function (ctx, team, id, cb) { var initRpc = function (ctx, team, data, cb) {
if (team.rpc) { return void cb(); }
if (!data.edPrivate || !data.edPublic) { return void cb('EFORBIDDEN'); }
require(['/common/pinpad.js'], function (Pinpad) {
Pinpad.create(ctx.store.network, data, function (e, call) {
if (e) { return void cb(e); }
team.rpc = call;
// XXX get pin limit?
});
});
};
var onReady = function (ctx, id, lm, keys, cb) {
// XXX // XXX
// sanity check: do we have all the required keys? // sanity check: do we have all the required keys?
// initialize team rpc with pin, unpin, ... // [x] initialize team rpc with pin, unpin, ...
// team.rpc = rpc // [x] team.rpc = rpc
// load manager with userObject // [x] load manager with userObject
// team.manager =... team.userObject = .... // team.manager =... team.userObject = ....
// load shared folders // [ ] load members pad
// register event for these folders // [ ] load shared folders
// ~resetPins for the team? // [ ] register listmap 'change' events for the drive and the shared folders
// getPinLimit // [ ] ~resetPins for the team?
// [ ] getPinLimit
var proxy = lm.proxy;
var team = {
id: id,
proxy: proxy,
listmap: lm,
clients: [],
realtime: lm.realtime,
handleSharedFolder: function (sfId, rt) { handleSharedFolder(ctx, id, sfId, rt); },
sharedFolders: {}, // equivalent of store.sharedFolders in async-store
};
team.sendEvent = function (q, data, sender) {
ctx.emit(q, data, team.clients.filter(function (cId) {
return cId !== sender;
}));
};
var pin = function (data, cb) { return void cb({error: 'EFORBIDDEN'}); };
var unpin = function (data, cb) { return void cb({error: 'EFORBIDDEN'}); };
var removeOwnedChannel = function (data, cb) {};
nThen(function (waitFor) {
if (!keys.edPrivate) { return; }
initRpc(ctx, team, keys, waitFor(function (err) {
if (err) { return; }
pin = function (data, cb) {
if (!team.rpc) { return void cb({error: 'TEAM_RPC_NOT_READY'}); }
if (typeof(cb) !== 'function') { console.error('expected a callback'); }
team.rpc.pin(data, function (e, hash) {
if (e) { return void cb({error: e}); }
cb({hash: hash});
});
};
unpin = function (data, cb) {
if (!team.rpc) { return void cb({error: 'TEAM_RPC_NOT_READY'}); }
if (typeof(cb) !== 'function') { console.error('expected a callback'); }
team.rpc.unpin(data, function (e, hash) {
if (e) { return void cb({error: e}); }
cb({hash: hash});
});
};
}));
}).nThen(function (waitFor) {
var loadSharedFolder = function (id, data, cb) {
SF.load({
network: ctx.store.network,
store: team
}, id, data, cb);
};
var manager = team.manager = ProxyManager.create(proxy.drive, {
onSync: function (cb) { ctx.Store.onSync(id, cb); },
edPublic: proxy.edPublic,
pin: pin,
unpin: unpin,
loadSharedFolder: loadSharedFolder,
settings: {
drive: Util.find(ctx.store, ['proxy', 'settings', 'drive'])
}
}, {
outer: true,
removeOwnedChannel: function (channel, cb) {
var data;
if (typeof(channel) === "object") {
channel.teamId = id;
data = channel;
} else {
data = {
channel: channel,
teamId: id
};
}
ctx.Store.removeOwnedChannel('', data, cb);
},
edPublic: proxy.edPublic,
loggedIn: true,
log: function (msg) {
// broadcast to all drive apps
team.sendEvent("DRIVE_LOG", msg);
}
});
team.userObject = manager.user.userObject;
team.userObject.fixFiles();
}).nThen(function (waitFor) {
// XXX
// Load shared folders
// Load members pad
console.log('ok');
}).nThen(function () {
ctx.teams[id] = team; ctx.teams[id] = team;
if (ctx.onReadyHandlers[id]) {
ctx.onReadyHandlers[id].forEach(function (obj) {
// Callback and subscribe the client to new notifications
if (typeof (obj.cb) === "function") { obj.cb(); }
if (!obj.cId) { return; }
var idx = team.clients.indexOf(obj.cId);
if (idx === -1) {
team.clients.push(obj.cId);
}
});
}
delete ctx.onReadyHandlers[id];
console.log(cb);
cb(); cb();
});
}; };
var openChannel = function (ctx, teamData, id, cb) { var openChannel = function (ctx, teamData, id, cb) {
// XXX team password? var secret = Hash.getSecrets('team', teamData.hash, teamData.password);
var secret = Hash.getSecrets('team', teamData.href);
var crypto = Crypto.createEncryptor(secret.keys); var crypto = Crypto.createEncryptor(secret.keys);
var cfg = { var cfg = {
data: {}, data: {},
readOnly: Boolean(secret.keys.signKey),
network: ctx.store.network, network: ctx.store.network,
channel: secret.channel, channel: secret.channel,
crypto: crypto, crypto: crypto,
@ -63,48 +186,100 @@ define([
var lm = Listmap.create(cfg); var lm = Listmap.create(cfg);
lm.proxy.on('create', function () { lm.proxy.on('create', function () {
}).on('ready', function () { }).on('ready', function () {
var sendEvent = function (type, data, sender) { onReady(ctx, id, lm, teamData.keys, cb);
type = type; });
data = data;
sender = sender;
// XXX emit UPDATE event to the inner iframe
// don't send the event back to the sender
// types are DRIVE_CHANGE, DRIVE_REMOVE and DRIVE_LOG
}; };
var team = { var createTeam = function (ctx, data, cId, _cb) {
id: id, var cb = Util.once(_cb);
proxy: lm.proxy,
listmap: lm, console.log(data);
clients: [],
manager: undefined, // XXX var teams = ctx.store.teams;
userObject: undefined, // XXX var password = Hash.createChannelId();
realtime: lm.realtime, var hash = Hash.createRandomHash('team', password);
handleSharedFolder: function (sfId, rt) { handleSharedFolder(ctx, id, sfId, rt); }, var secret = Hash.getSecrets('team', hash, password);
sharedFolders: {}, // equivalent of store.sharedFolders in async-store var keyPair = Nacl.sign.keyPair(); // keyPair.secretKey , keyPair.publicKey
sendEvent: sendEvent
var membersSecret = Hash.getSecrets('members');
var membersHashes = Hash.getHashes(secret);
var config = {
network: ctx.store.network,
channel: secret.channel,
data: {},
validateKey: secret.keys.validateKey, // derived validation key
crypto: Crypto.createEncryptor(secret.keys),
logLevel: 1,
classic: true,
ChainPad: ChainPad,
owners: [ctx.store.proxy.edPublic]
};
nThen(function (waitFor) {
console.log('pin..');
ctx.pinPads([secret.channel, membersSecret.channel], waitFor(function (obj) {
if (obj && obj.error) {
waitFor.abort();
return void cb(obj);
}
}));
// XXX initialize the members channel with yourself, and mark it as owned!
}).nThen(function () {
console.log('init proxy');
var lm = Listmap.create(config);
var proxy = lm.proxy;
proxy.on('ready', function () {
console.log('ready');
// Store keys in our drive
var id = Util.createRandomInteger();
var keys = {
edPrivate: keyPair.secretKey,
edPublic: keyPair.publicKey
};
ctx.store.proxy.teams[id] = {
hash: hash,
password: password,
keys: keys,
members: membersHashes.editHash,
name: data.name
};
// Initialize the team drive
proxy.drive = {};
// Create metadata
proxy.metadata = {
name: name,
members: membersHashes.viewHash,
}; };
// Add rpc key
proxy.edPublic = keyPair.publicKey;
onReady(ctx, team, id, function () { onReady(ctx, id, lm, {
// TODO edPrivate: keyPair.secretKey,
edPublic: keyPair.publicKey
}, function () {
cb(); cb();
}); });
if (ctx.onReadyHandlers.length) { }).on('error', function (info) {
ctx.onReadyHandlers.forEach(function (f) { if (info && typeof (info.loaded) !== "undefined" && !info.loaded) {
try { cb({error:'ECONNECT'});
f(lm.proxy);
} catch (e) { console.error(e); }
});
ctx.onReadyHandlers = [];
} }
}).on('change', [], function () { });
// XXX team app event
//ctx.emit('UPDATE', lm.proxy, ctx.clients);
}); });
}; };
var subscribe = function (ctx, id, cId, cb) { var subscribe = function (ctx, id, cId, cb) {
// Subscribe to new notifications // If the team is loading, as ourselves in the list
if (ctx.onReadyHandlers[id]) {
var _idx = ctx.onReadyHandlers[id].indexOf(cId);
if (_idx === -1) {
ctx.onReadyHandlers[id].push({
cId: cId,
cb: cb
});
}
return;
}
// Otherwise, subscribe to new notifications
if (!id || !ctx.teams[id]) { if (!id || !ctx.teams[id]) {
return void cb({error: 'EINVAL'}); return void cb({error: 'EINVAL'});
} }
@ -119,9 +294,16 @@ define([
// Remove a client from all the team they're subscribed to // Remove a client from all the team they're subscribed to
var removeClient = function (ctx, cId) { var removeClient = function (ctx, cId) {
Object.keys(ctx.teams).forEach(function (id) { Object.keys(ctx.teams).forEach(function (id) {
// Remove from the subscribers
var clients = ctx.teams[id].clients; var clients = ctx.teams[id].clients;
var idx = clients.indexOf(cId); var idx = clients.indexOf(cId);
clients.splice(idx, 1); if (idx !== -1) { clients.splice(idx, 1); }
// And remove from the onReady handlers in case they haven't finished loading
if (ctx.onReadyHandlers[id]) {
var idx2 = ctx.onReadyHandlers.indexOf(cId);
if (idx2 !== -1) { ctx.onReadyHandlers.splice(idx2, 1); }
}
}); });
}; };
@ -131,10 +313,10 @@ define([
if (!store.loggedIn || !store.proxy.edPublic) { return; } if (!store.loggedIn || !store.proxy.edPublic) { return; }
var ctx = { var ctx = {
store: store, store: store,
Store: cfg.Store,
pinPads: cfg.pinPads, pinPads: cfg.pinPads,
updateMetadata: cfg.updateMetadata,
emit: emit, emit: emit,
onReadyHandlers: [], onReadyHandlers: {},
teams: {} teams: {}
}; };
@ -145,7 +327,7 @@ define([
})); }));
Object.keys(teams).forEach(function (id) { Object.keys(teams).forEach(function (id) {
// only if we want to make sure teams are loaded before remore the loading screen ctx.onReadyHandlers[id] = [];
openChannel(ctx, teams[id], id, waitFor(function () { openChannel(ctx, teams[id], id, waitFor(function () {
console.error('team '+id+' ready'); console.error('team '+id+' ready');
})); }));
@ -169,7 +351,10 @@ define([
return void subscribe(ctx, data, clientId, cb); return void subscribe(ctx, data, clientId, cb);
} }
if (cmd === 'LIST_TEAMS') { if (cmd === 'LIST_TEAMS') {
return void cb(ctx.teams); return void cb(store.proxy.teams);
}
if (cmd === 'CREATE_TEAM') {
return void createTeam(ctx, data, clientId, cb);
} }
}; };

@ -98,7 +98,7 @@ define([
var $div = $('<div>', {'class': 'cp-team-' + key + ' cp-sidebarlayout-element'}); var $div = $('<div>', {'class': 'cp-team-' + key + ' cp-sidebarlayout-element'});
getter(common, function (content) { getter(common, function (content) {
$div.append(content); $div.append(content);
}); }, $div);
return $div; return $div;
}; };
}; };
@ -110,12 +110,68 @@ define([
]); ]);
}); });
makeBlock('list', function (common, cb) { var refreshList = function (common, cb) {
var content = []; var content = [];
var sframeChan = common.getSframeChannel(); content.push(h('h3', 'Your teams'));
APP.module.execCommand('LIST_TEAMS', null, function (obj) { APP.module.execCommand('LIST_TEAMS', null, function (obj) {
if (!obj) { return; }
if (obj.error) { return void console.error(obj.error); }
var lis = [];
Object.keys(obj).forEach(function (id) {
var team = obj[id];
var a = h('a', 'Open');
lis.push(h('li', h('ul', [
h('li', 'Name: ' + team.name), // XXX
h('li', 'ID: ' + id), // XXX
h('li', a) // XXX
])));
$(a).click(function () {
console.log('okok'); // XXX
});
});
content.push(h('ul', lis));
cb(content);
});
return content;
};
makeBlock('list', function (common, cb) {
refreshList(common, cb)
});
makeBlock('create', function (common, cb) {
var content = [];
content.push(h('h3', 'Create a team')); // XXX
content.push(h('label', 'Team name')); // XXX
var input = h('input', {type:'text'});
content.push(input);
var button = h('button.btn.btn-success', 'Create'); // XXX
content.push(h('br'));
content.push(h('br'));
content.push(button);
var state = false;
$(button).click(function () {
if (state) { return; }
var name = $(input).val();
if (!name.trim()) { return; }
state = true;
UI.confirm('Are you sure?', function (yes) {
if (!yes) {
state = false;
return;
}
APP.module.execCommand('CREATE_TEAM', {
name: name
}, function () {
var $div = $('div.cp-team-list').empty();
refreshList(common, function (content) {
state = false;
$div.append(content);
$('div.cp-team-cat-list').click();
});
});
});
}); });
cb(content);
}); });
// Sidebar layout // Sidebar layout
@ -138,9 +194,9 @@ define([
var active = team ? 'drive' : 'general'; var active = team ? 'drive' : 'general';
Object.keys(categories).forEach(function (key) { Object.keys(categories).forEach(function (key) {
var $category = $('<div>', {'class': 'cp-sidebarlayout-category'}).appendTo($categories); var $category = $('<div>', {'class': 'cp-sidebarlayout-category cp-team-cat-'+key}).appendTo($categories);
if (key === 'general') { $category.append($('<span>', {'class': 'fa fa-info-circle'})); } if (key === 'general') { $category.append($('<span>', {'class': 'fa fa-info-circle'})); }
if (key === 'list') { $category.append($('<span>', {'class': 'fa fa-list'})); } if (key === 'list') { $category.append($('<span>', {'class': 'fa fa-list cp-team-cat-list'})); }
if (key === 'create') { $category.append($('<span>', {'class': 'fa fa-plus-circle'})); } if (key === 'create') { $category.append($('<span>', {'class': 'fa fa-plus-circle'})); }
if (key === 'back') { $category.append($('<span>', {'class': 'fa fa-arrow-left'})); } if (key === 'back') { $category.append($('<span>', {'class': 'fa fa-arrow-left'})); }
if (key === 'members') { $category.append($('<span>', {'class': 'fa fa-users'})); } if (key === 'members') { $category.append($('<span>', {'class': 'fa fa-users'})); }

@ -96,14 +96,11 @@ define([
Cryptpad.onNetworkReconnect.reg(function (data) { Cryptpad.onNetworkReconnect.reg(function (data) {
sframeChan.event('EV_NETWORK_RECONNECT', data); sframeChan.event('EV_NETWORK_RECONNECT', data);
}); });
Cryptpad.drive.onLog.reg(function (msg) { Cryptpad.universal.onEvent.reg(function (obj) {
sframeChan.event('EV_DRIVE_LOG', msg); // Intercept events for the team drive and send them the required way
}); if (obj.type !== 'team' ||
Cryptpad.drive.onChange.reg(function (data) { ['DRIVE_CHANGE', 'DRIVE_LOG', 'DRIVE_REMOVE'].indexOf(obj.data.ev) === -1) { return; }
sframeChan.event('EV_DRIVE_CHANGE', data); sframeChan.event(obj.data.ev, obj.data.data);
});
Cryptpad.drive.onRemove.reg(function (data) {
sframeChan.event('EV_DRIVE_REMOVE', data);
}); });
}; };
SFCommonO.start({ SFCommonO.start({

Loading…
Cancel
Save