Don't rejoin a dropped channel on reconnect and leave chat channel

pull/1/head
yflory 6 years ago
parent 2a86db2001
commit 498703384f

@ -585,6 +585,7 @@ define([
id: data.channel, id: data.channel,
isFriendChat: data.isFriendChat, isFriendChat: data.isFriendChat,
isPadChat: data.isPadChat, isPadChat: data.isPadChat,
padChan: data.padChan,
sending: false, sending: false,
encryptor: encryptor, encryptor: encryptor,
messages: [], messages: [],
@ -655,6 +656,7 @@ define([
console.error(err); console.error(err);
}); });
network.on('reconnect', function () { network.on('reconnect', function () {
if (channel && channel.stopped) { return; }
if (!channels[data.channel]) { return; } if (!channels[data.channel]) { return; }
network.join(data.channel).then(onOpen, function (err) { network.join(data.channel).then(onOpen, function (err) {
console.error(err); console.error(err);
@ -910,6 +912,7 @@ define([
var cryptKey = keys.viewKeyStr ? Crypto.b64AddSlashes(keys.viewKeyStr) : data.secret.key; var cryptKey = keys.viewKeyStr ? Crypto.b64AddSlashes(keys.viewKeyStr) : data.secret.key;
var encryptor = Crypto.createEncryptor(cryptKey); var encryptor = Crypto.createEncryptor(cryptKey);
var chanData = { var chanData = {
padChan: data.secret && data.secret.channel,
encryptor: encryptor, encryptor: encryptor,
channel: data.channel, channel: data.channel,
isPadChat: true, isPadChat: true,
@ -931,6 +934,17 @@ define([
emit('RECONNECT'); emit('RECONNECT');
}); });
messenger.leavePad = function (padChan) {
// Leave chat and prevent reconnect when we leave a pad
Object.keys(channels).some(function (chatChan) {
var channel = channels[chatChan];
if (channel.padChan !== padChan) { return; }
if (channel.wc) { channel.wc.leave(); }
channel.stopped = true;
return true;
});
};
messenger.execCommand = function (obj, cb) { messenger.execCommand = function (obj, cb) {
var cmd = obj.cmd; var cmd = obj.cmd;
var data = obj.data; var data = obj.data;

@ -985,7 +985,7 @@ define([
/////////////////////// PAD ////////////////////////////////////// /////////////////////// PAD //////////////////////////////////////
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
var channels = Store.channels = {}; var channels = Store.channels = store.channels = {};
Store.joinPad = function (clientId, data) { Store.joinPad = function (clientId, data) {
var isNew = typeof channels[data.channel] === "undefined"; var isNew = typeof channels[data.channel] === "undefined";
@ -1093,12 +1093,12 @@ define([
}); });
} }
}; };
CpNfWorker.start(conf); channel.cpNf = CpNfWorker.start(conf);
}; };
Store.leavePad = function (clientId, data, cb) { Store.leavePad = function (clientId, data, cb) {
var channel = channels[data.channel]; var channel = channels[data.channel];
if (!channel || !channel.wc) { return void cb ({error: 'EINVAL'}); } if (!channel || !channel.cpNf) { return void cb ({error: 'EINVAL'}); }
channel.wc.leave(); channel.cpNf.stop();
delete channels[data.channel]; delete channels[data.channel];
cb(); cb();
}; };
@ -1278,9 +1278,12 @@ define([
var dropChannel = function (chanId) { var dropChannel = function (chanId) {
if (!Store.channels[chanId]) { return; } if (!Store.channels[chanId]) { return; }
if (Store.channels[chanId].wc) { if (Store.channels[chanId].cpNf) {
Store.channels[chanId].wc.leave(''); Store.channels[chanId].cpNf.stop();
} }
store.messenger.leavePad(chanId);
delete Store.channels[chanId]; delete Store.channels[chanId];
}; };
Store._removeClient = function (clientId) { Store._removeClient = function (clientId) {

@ -45,6 +45,7 @@ define([], function () {
conf = undefined; conf = undefined;
var initializing = true; var initializing = true;
var stopped = false;
var lastKnownHash; var lastKnownHash;
var messageFromOuter = function () {}; var messageFromOuter = function () {};
@ -239,6 +240,12 @@ define([], function () {
onOpen(wc, network, firstConnection); onOpen(wc, network, firstConnection);
}, function(err) { }, function(err) {
console.error(err); console.error(err);
if (onError) {
onError({
type: err && (err.type || err),
loaded: !initializing
});
}
}); });
}; };
@ -250,11 +257,13 @@ define([], function () {
}); });
network.on('reconnect', function () { network.on('reconnect', function () {
if (stopped) { return; }
initializing = true; initializing = true;
connectTo(network, false); connectTo(network, false);
}); });
network.on('message', function (msg, sender) { // Direct message network.on('message', function (msg, sender) { // Direct message
if (stopped) { return; }
var wchan = findChannelById(network.webChannels, channel); var wchan = findChannelById(network.webChannels, channel);
if (wchan) { if (wchan) {
onMsg(sender, msg, wchan, network, true); onMsg(sender, msg, wchan, network, true);
@ -262,6 +271,14 @@ define([], function () {
}); });
connectTo(network, true); connectTo(network, true);
return {
stop: function () {
var wchan = findChannelById(network.webChannels, channel);
if (wchan) { wchan.leave(''); }
stopped = true;
}
};
}; };
return { return {

Loading…
Cancel
Save