diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index b4425a8ac..050e28123 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -14,6 +14,7 @@ define([ '/common/outer/onlyoffice.js', '/common/outer/mailbox.js', '/common/outer/profile.js', + '/common/outer/team.js', '/common/outer/network-config.js', '/customize/application_config.js', @@ -24,7 +25,7 @@ define([ '/bower_components/nthen/index.js', '/bower_components/saferphore/index.js', ], function (Sortify, UserObject, ProxyManager, Migrate, Hash, Util, Constants, Feedback, Realtime, Messaging, Messenger, - Cursor, OnlyOffice, Mailbox, Profile, NetConfig, AppConfig, + Cursor, OnlyOffice, Mailbox, Profile, Team, NetConfig, AppConfig, Crypto, ChainPad, CpNetflux, Listmap, nThen, Saferphore) { var create = function () { @@ -1880,6 +1881,7 @@ define([ loadCursor(); loadOnlyOffice(); loadUniversal(Profile, 'profile', waitFor); + loadUniversal(Team, 'team', waitFor); cleanFriendRequests(); }).nThen(function () { arePinsSynced(function (err, yes) { diff --git a/www/common/outer/team.js b/www/common/outer/team.js new file mode 100644 index 000000000..d29b4e1da --- /dev/null +++ b/www/common/outer/team.js @@ -0,0 +1,125 @@ +define([ + '/common/common-util.js', + '/common/common-hash.js', + '/common/common-constants.js', + '/common/common-realtime.js', + '/bower_components/chainpad-listmap/chainpad-listmap.js', + '/bower_components/chainpad-crypto/crypto.js', + '/bower_components/chainpad/chainpad.dist.js', +], function (Util, Hash, Constants, Realtime, Listmap, Crypto, ChainPad) { + var Team = {}; + + var initializeTeams = function (ctx, cb) { + // XXX ? + cb(); + }; + + var openChannel = function (ctx, team, id, cb) { + // XXX team password? + var secret = Hash.getSecrets('team', team.href); + var crypto = Crypto.createEncryptor(secret.keys); + + var cfg = { + data: {}, + network: ctx.store.network, + channel: secret.channel, + crypto: crypto, + ChainPad: ChainPad, + metadata: { + validateKey: secret.keys.validateKey || undefined, + }, + userName: 'team', + classic: true + }; + var lm = Listmap.create(cfg); + lm.proxy.on('create', function () { + }).on('ready', function () { + ctx.teams[id] = { + listmap: lm, + clients: [] + }; + if (ctx.onReadyHandlers.length) { + ctx.onReadyHandlers.forEach(function (f) { + try { + 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) { + // Subscribe to new notifications + if (!id || !ctx.teams[id]) { + return void cb({error: 'EINVAL'}); + } + var clients = ctx.teams[id].clients; + var idx = clients.indexOf(cId); + if (idx === -1) { + clients.push(cId); + } + cb(); + }; + + // Remove a client from all the team they're subscribed to + var removeClient = function (ctx, cId) { + Object.keys(ctx.teams).forEach(function (id) { + var clients = ctx.teams[id].clients; + var idx = clients.indexOf(cId); + clients.splice(idx, 1); + }); + }; + + Team.init = function (cfg, waitFor, emit) { + var team = {}; + var store = cfg.store; + if (!store.loggedIn || !store.proxy.edPublic) { return; } + var ctx = { + store: store, + pinPads: cfg.pinPads, + updateMetadata: cfg.updateMetadata, + emit: emit, + onReadyHandlers: [], + teams: {} + }; + + var teams = store.proxy.teams = store.proxy.teams || {}; + + initializeTeams(ctx, waitFor(function (err) { + if (err) { return; } + openChannel(ctx); + })); + + Object.keys(teams).forEach(function (id) { + // XXX waitFor? + // only if we want to make sure teams are loaded before remore the loading screen + openChannel(ctx, teams[id], id, function () { + console.error('team '+id+' ready'); + }); + }); + + team.removeClient = function (clientId) { + removeClient(ctx, clientId); + }; + team.execCommand = function (clientId, obj, cb) { + console.log(obj); + var cmd = obj.cmd; + var data = obj.data; + if (cmd === 'SUBSCRIBE') { + // Only the team app will subscribe to events? + return void subscribe(ctx, data, clientId, cb); + } + }; + + return team; + }; + + return Team; +}); + + +