Fix friend migration and handle UPDATE_DATA mails

pull/1/head
yflory 6 years ago
parent 592544819e
commit 250d9f3bc6

@ -192,6 +192,7 @@ define([
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
// Migration 9: send our mailbox channel to existing friends // Migration 9: send our mailbox channel to existing friends
var migrateFriends = function () { var migrateFriends = function () {
var network = store.network;
var channels = {}; var channels = {};
var ctx = { var ctx = {
store: store store: store
@ -203,8 +204,8 @@ define([
if (!channel) { return; } if (!channel) { return; }
try { try {
channel.wc.leave(); channel.wc.leave();
delete channels[chan];
} catch (e) {} } catch (e) {}
delete channels[chan];
}; };
var onDirectMessage = function (msg, sender) { var onDirectMessage = function (msg, sender) {
@ -229,7 +230,7 @@ define([
// Channel is ready and we didn't receive their mailbox channel: send our channel // Channel is ready and we didn't receive their mailbox channel: send our channel
myData.channel = parsed.channel; myData.channel = parsed.channel;
var updateMsg = ['UPDATE', myData.curvePublic, +new Date(), myData]; var updateMsg = ['UPDATE', myData.curvePublic, +new Date(), myData];
var cryptMsg = channel.encrypt(JSON.stringify(updateMsg)); var cryptMsg = channels[parsed.channel].encrypt(JSON.stringify(updateMsg));
channels[parsed.channel].wc.bcast(cryptMsg).then(function () {}, function (err) { channels[parsed.channel].wc.bcast(cryptMsg).then(function () {}, function (err) {
console.error("Can't migrate this friend", channels[parsed.channel].friend, err); console.error("Can't migrate this friend", channels[parsed.channel].friend, err);
}); });
@ -255,10 +256,11 @@ define([
channel.friend.notifications = data.notifications channel.friend.notifications = data.notifications
myData.channel = chan; myData.channel = chan;
Mailbox.sendTo(ctx, 'UPDATE_DATA', myData, { Mailbox.sendTo(ctx, 'UPDATE_DATA', myData, {
channel: data.notitications, channel: data.notifications,
curvePublic: data.curvePublic curvePublic: data.curvePublic
}, function () { }, function (obj) {
console.log('friend migrated', friend); if (obj && obj.error) { return void console.error(obj); }
console.log('friend migrated', channel.friend);
}); });
close(chan); close(chan);
} }
@ -267,7 +269,9 @@ define([
network.on('message', function(msg, sender) { network.on('message', function(msg, sender) {
try { try {
onDirectMessage(msg, sender); onDirectMessage(msg, sender);
} catch (e) {} } catch (e) {
console.error(e);
}
}); });
var friends = userObject.friends || {}; var friends = userObject.friends || {};
@ -309,7 +313,7 @@ define([
if (version < 9) { if (version < 9) {
migrateFriends(); migrateFriends();
Feedback.send('Migrate-9', true); Feedback.send('Migrate-9', true);
userObject.version = version = 9; //userObject.version = version = 9;
} }
/*}).nThen(function (waitFor) { /*}).nThen(function (waitFor) {
// Test progress bar in the loading screen // Test progress bar in the loading screen

@ -144,8 +144,29 @@ define([
cb(true); cb(true);
}; };
handlers['UPDATE_DATA'] = function (ctx, box, data, cb) {
var msg = data.msg;
var curve = msg.author;
var friend = ctx.store.proxy.friends && ctx.store.proxy.friends[curve];
if (!friend || typeof msg.content !== "object") { return void cb(true); }
Object.keys(msg.content).forEach(function (key) {
friend[key] = msg.content[key];
});
cb(true);
};
return { return {
add: function (ctx, box, data, cb) { add: function (ctx, box, data, cb) {
/**
* data = {
msg: {
type: 'STRING',
author: 'curvePublicString',
content: {} (depend on the "type")
},
hash: 'string'
}
*/
if (!data.msg) { return void cb(true); } if (!data.msg) { return void cb(true); }
var type = data.msg.type; var type = data.msg.type;

Loading…
Cancel
Save