Fix issues with the team mailbox

pull/1/head
yflory 5 years ago
parent bcfa09f7bc
commit 24c7ea985d

@ -137,10 +137,9 @@ proxy.mailboxes = {
var dismiss = function (ctx, data, cId, cb) { var dismiss = function (ctx, data, cId, cb) {
var type = data.type; var type = data.type;
var hash = data.hash; 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]; var box = ctx.boxes[type];
if (!box) { return void cb({error: 'NOT_LOADED'}); } 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 hash in in our history, get the index from the history:
// - if the index is 0, we can change our lastKnownHash // - 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] = { var box = ctx.boxes[type] = {
channel: m.channel, channel: m.channel,
type: type, type: type,
@ -221,7 +229,8 @@ proxy.mailboxes = {
console.error(e); console.error(e);
} }
box.queue.push(msg); box.queue.push(msg);
} },
data: m
}; };
if (!Crypto.Mailbox) { if (!Crypto.Mailbox) {
return void console.error("chainpad-crypto is outdated and doesn't support mailboxes."); return void console.error("chainpad-crypto is outdated and doesn't support mailboxes.");
@ -235,7 +244,7 @@ proxy.mailboxes = {
channel: m.channel, channel: m.channel,
noChainPad: true, noChainPad: true,
crypto: crypto, crypto: crypto,
owners: [ctx.store.proxy.edPublic], owners: opts.owners || [ctx.store.proxy.edPublic],
lastKnownHash: m.lastKnownHash lastKnownHash: m.lastKnownHash
}; };
cfg.onConnectionChange = function () {}; // Allow reconnections in chainpad-netflux cfg.onConnectionChange = function () {}; // Allow reconnections in chainpad-netflux
@ -357,7 +366,7 @@ proxy.mailboxes = {
// Continue // Continue
onReady(); onReady();
}; };
CpNetflux.start(cfg); box.cpNf = CpNetflux.start(cfg);
}; };
var initializeHistory = function (ctx) { var initializeHistory = function (ctx) {
@ -480,11 +489,15 @@ proxy.mailboxes = {
Object.keys(store.proxy.teams || {}).forEach(function (teamId) { Object.keys(store.proxy.teams || {}).forEach(function (teamId) {
var team = store.proxy.teams[teamId]; var team = store.proxy.teams[teamId];
if (!team) { return; }
var teamMailbox = team.keys.mailbox || {}; var teamMailbox = team.keys.mailbox || {};
if (!teamMailbox.channel) { return; } if (!teamMailbox.channel) { return; }
var opts = {
owners: [Util.find(team, ['keys', 'drive', 'edPublic'])]
};
openChannel(ctx, 'team-'+teamId, teamMailbox, function () { openChannel(ctx, 'team-'+teamId, teamMailbox, function () {
//console.log('Mailbox team', teamId); //console.log('Mailbox team', teamId);
}); }, opts);
}); });
mailbox.post = function (box, type, content) { 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; } 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) { mailbox.dismiss = function (data, cb) {

@ -126,6 +126,9 @@ define([
delete ctx.store.proxy.teams[teamId]; delete ctx.store.proxy.teams[teamId];
ctx.emit('LEAVE_TEAM', teamId, team.clients); ctx.emit('LEAVE_TEAM', teamId, team.clients);
ctx.updateMetadata(); ctx.updateMetadata();
ctx.store.mailbox.close('team-'+teamId, function () {
// Close team mailbox
});
}; };
var getTeamChannelList = function (ctx, id) { var getTeamChannelList = function (ctx, id) {
@ -494,8 +497,7 @@ define([
var roHash = Hash.getViewHashFromKeys(secret); var roHash = Hash.getViewHashFromKeys(secret);
var keyPair = Nacl.sign.keyPair(); // keyPair.secretKey , keyPair.publicKey var keyPair = Nacl.sign.keyPair(); // keyPair.secretKey , keyPair.publicKey
var curveSeed = Nacl.randomBytes(32); var curvePair = Nacl.box.keyPair();
var curvePair = Nacl.box.keyPair.fromSecretKey(curveSeed);
var rosterSeed = Crypto.Team.createSeed(); var rosterSeed = Crypto.Team.createSeed();
var rosterKeys = Crypto.Team.deriveMemberKeys(rosterSeed, { var rosterKeys = Crypto.Team.deriveMemberKeys(rosterSeed, {
@ -612,7 +614,7 @@ define([
view: rosterKeys.viewKeyStr, view: rosterKeys.viewKeyStr,
} }
}; };
ctx.store.proxy.teams[id] = { var t = ctx.store.proxy.teams[id] = {
owner: true, owner: true,
channel: secret.channel, channel: secret.channel,
hash: hash, hash: hash,
@ -629,6 +631,11 @@ define([
onReady(ctx, id, lm, roster, keys, cId, function () { onReady(ctx, id, lm, roster, keys, cId, function () {
Feedback.send('TEAM_CREATION'); 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(); ctx.updateMetadata();
cb(); cb();
}); });
@ -731,6 +738,11 @@ define([
team.rpc.removePins(waitFor(function (err) { team.rpc.removePins(waitFor(function (err) {
if (err) { console.error(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 // Delete the roster
var rosterChan = Util.find(teamData, ['keys', 'roster', 'channel']); var rosterChan = Util.find(teamData, ['keys', 'roster', 'channel']);
ctx.store.rpc.removeOwnedChannel(rosterChan, waitFor(function (err) { 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) { var deriveMailbox = function (team) {
if (!team) { return; } if (!team) { return; }
if (team.keys && team.keys.mailbox) { return team.keys.mailbox; } if (team.keys && team.keys.mailbox) { return team.keys.mailbox; }
var channel = team.channel; var strSeed = Util.find(team, ['keys', 'roster', 'edit']);
if (!channel) { return; } if (!strSeed) { return; }
// XXX maybe use something else than channel? var hash = Nacl.hash(Nacl.util.decodeUTF8(strSeed));
var hash = Nacl.hash(Nacl.util.decodeUTF8(channel));
var seed = hash.slice(0,32); var seed = hash.slice(0,32);
var mailboxChannel = Util.uint8ArrayToHex(hash.slice(32,48)); var mailboxChannel = Util.uint8ArrayToHex(hash.slice(32,48));
var curvePair = Nacl.box.keyPair.fromSecretKey(seed); var curvePair = Nacl.box.keyPair.fromSecretKey(seed);

@ -106,7 +106,7 @@ define([
// Call the onMessage handlers // Call the onMessage handlers
var isNotification = function (type) { var isNotification = function (type) {
return type === "notificatons" || /^team-/.test(type); return type === "notifications" || /^team-/.test(type);
}; };
var pushMessage = function (data, handler) { var pushMessage = function (data, handler) {
var todo = function (f) { var todo = function (f) {
@ -188,6 +188,7 @@ define([
onMessageHandlers.push(function (data, el) { onMessageHandlers.push(function (data, el) {
var type = data.type; var type = data.type;
if (types.indexOf(type) === -1 && !(teams && /^team-/.test(type))) { return; } if (types.indexOf(type) === -1 && !(teams && /^team-/.test(type))) { return; }
console.log('okokok');
cfg.onMessage(data, el); cfg.onMessage(data, el);
}); });
} }

@ -1027,6 +1027,7 @@ MessengerUI, Messages) {
Common.mailbox.subscribe(['notifications', 'team'], { Common.mailbox.subscribe(['notifications', 'team'], {
onMessage: function (data, el) { onMessage: function (data, el) {
console.log(data, el, div);
if (el) { if (el) {
$(div).prepend(el); $(div).prepend(el);
} }

Loading…
Cancel
Save