diff --git a/www/common/outer/mailbox.js b/www/common/outer/mailbox.js index 2250257aa..e5b6179c7 100644 --- a/www/common/outer/mailbox.js +++ b/www/common/outer/mailbox.js @@ -137,10 +137,9 @@ proxy.mailboxes = { var dismiss = function (ctx, data, cId, cb) { var type = data.type; var hash = data.hash; - var m = Util.find(ctx, ['store', 'proxy', 'mailboxes', type]); - if (!m) { return void cb({error: 'NOT_FOUND'}); } var box = ctx.boxes[type]; if (!box) { return void cb({error: 'NOT_LOADED'}); } + var m = box.data || {}; // If the hash in in our history, get the index from the history: // - if the index is 0, we can change our lastKnownHash @@ -202,7 +201,16 @@ proxy.mailboxes = { }; - var openChannel = function (ctx, type, m, onReady) { + var leaveChannel = function (ctx, type, cb) { + var box = ctx.boxes[type]; + if (!box) { return void cb(); } + if (!box.cpNf || typeof(box.cpNf.stop) !== "function") { return void cb('EINVAL'); } + box.cpNf.stop(); + delete ctx.boxes[type]; + }; + var openChannel = function (ctx, type, m, onReady, opts) { + console.error(type, m, opts); + opts = opts || {}; var box = ctx.boxes[type] = { channel: m.channel, type: type, @@ -221,7 +229,8 @@ proxy.mailboxes = { console.error(e); } box.queue.push(msg); - } + }, + data: m }; if (!Crypto.Mailbox) { return void console.error("chainpad-crypto is outdated and doesn't support mailboxes."); @@ -235,7 +244,7 @@ proxy.mailboxes = { channel: m.channel, noChainPad: true, crypto: crypto, - owners: [ctx.store.proxy.edPublic], + owners: opts.owners || [ctx.store.proxy.edPublic], lastKnownHash: m.lastKnownHash }; cfg.onConnectionChange = function () {}; // Allow reconnections in chainpad-netflux @@ -357,7 +366,7 @@ proxy.mailboxes = { // Continue onReady(); }; - CpNetflux.start(cfg); + box.cpNf = CpNetflux.start(cfg); }; var initializeHistory = function (ctx) { @@ -480,11 +489,15 @@ proxy.mailboxes = { Object.keys(store.proxy.teams || {}).forEach(function (teamId) { var team = store.proxy.teams[teamId]; + if (!team) { return; } var teamMailbox = team.keys.mailbox || {}; if (!teamMailbox.channel) { return; } + var opts = { + owners: [Util.find(team, ['keys', 'drive', 'edPublic'])] + }; openChannel(ctx, 'team-'+teamId, teamMailbox, function () { //console.log('Mailbox team', teamId); - }); + }, opts); }); mailbox.post = function (box, type, content) { @@ -497,9 +510,12 @@ proxy.mailboxes = { }); }; - mailbox.open = function (key, m, cb, team) { + mailbox.open = function (key, m, cb, team, opts) { if (TYPES.indexOf(key) === -1 && !team) { return; } - openChannel(ctx, key, m, cb); + openChannel(ctx, key, m, cb, opts); + }; + mailbox.close = function (key, cb) { + leaveChannel(ctx, key, cb); }; mailbox.dismiss = function (data, cb) { diff --git a/www/common/outer/team.js b/www/common/outer/team.js index a0c785200..c495bc91c 100644 --- a/www/common/outer/team.js +++ b/www/common/outer/team.js @@ -126,6 +126,9 @@ define([ delete ctx.store.proxy.teams[teamId]; ctx.emit('LEAVE_TEAM', teamId, team.clients); ctx.updateMetadata(); + ctx.store.mailbox.close('team-'+teamId, function () { + // Close team mailbox + }); }; var getTeamChannelList = function (ctx, id) { @@ -494,8 +497,7 @@ define([ var roHash = Hash.getViewHashFromKeys(secret); var keyPair = Nacl.sign.keyPair(); // keyPair.secretKey , keyPair.publicKey - var curveSeed = Nacl.randomBytes(32); - var curvePair = Nacl.box.keyPair.fromSecretKey(curveSeed); + var curvePair = Nacl.box.keyPair(); var rosterSeed = Crypto.Team.createSeed(); var rosterKeys = Crypto.Team.deriveMemberKeys(rosterSeed, { @@ -612,7 +614,7 @@ define([ view: rosterKeys.viewKeyStr, } }; - ctx.store.proxy.teams[id] = { + var t = ctx.store.proxy.teams[id] = { owner: true, channel: secret.channel, hash: hash, @@ -629,6 +631,11 @@ define([ onReady(ctx, id, lm, roster, keys, cId, function () { Feedback.send('TEAM_CREATION'); + ctx.store.mailbox.open('team-'+id, t.keys.mailbox, function () { + // Team mailbox loaded + }, true, { + owners: t.keys.drive.edPublic + }); ctx.updateMetadata(); cb(); }); @@ -731,6 +738,11 @@ define([ team.rpc.removePins(waitFor(function (err) { if (err) { console.error(err); } })); + // Delete the mailbox + var mailboxChan = Util.find(teamData, ['keys', 'mailbox', 'channel']); + team.rpc.removeOwnedChannel(mailboxChan, waitFor(function (err) { + if (err) { console.error(err); } + })); // Delete the roster var rosterChan = Util.find(teamData, ['keys', 'roster', 'channel']); ctx.store.rpc.removeOwnedChannel(rosterChan, waitFor(function (err) { @@ -1577,15 +1589,12 @@ define([ }); }; - // XXX call mailbox.open when you create or join a team - // XXX close the mailbox hwne you leave the team var deriveMailbox = function (team) { if (!team) { return; } if (team.keys && team.keys.mailbox) { return team.keys.mailbox; } - var channel = team.channel; - if (!channel) { return; } - // XXX maybe use something else than channel? - var hash = Nacl.hash(Nacl.util.decodeUTF8(channel)); + var strSeed = Util.find(team, ['keys', 'roster', 'edit']); + if (!strSeed) { return; } + var hash = Nacl.hash(Nacl.util.decodeUTF8(strSeed)); var seed = hash.slice(0,32); var mailboxChannel = Util.uint8ArrayToHex(hash.slice(32,48)); var curvePair = Nacl.box.keyPair.fromSecretKey(seed); diff --git a/www/common/sframe-common-mailbox.js b/www/common/sframe-common-mailbox.js index eac59462e..bdf1ae81e 100644 --- a/www/common/sframe-common-mailbox.js +++ b/www/common/sframe-common-mailbox.js @@ -106,7 +106,7 @@ define([ // Call the onMessage handlers var isNotification = function (type) { - return type === "notificatons" || /^team-/.test(type); + return type === "notifications" || /^team-/.test(type); }; var pushMessage = function (data, handler) { var todo = function (f) { @@ -188,6 +188,7 @@ define([ onMessageHandlers.push(function (data, el) { var type = data.type; if (types.indexOf(type) === -1 && !(teams && /^team-/.test(type))) { return; } + console.log('okokok'); cfg.onMessage(data, el); }); } diff --git a/www/common/toolbar.js b/www/common/toolbar.js index e55cdcf82..3afa55d61 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -1027,6 +1027,7 @@ MessengerUI, Messages) { Common.mailbox.subscribe(['notifications', 'team'], { onMessage: function (data, el) { + console.log(data, el, div); if (el) { $(div).prepend(el); }