Synchronize open channel on client on reconnection

This commit is contained in:
Pavel Djundik 2019-10-17 13:53:29 +03:00
parent ee4de6a871
commit 1ca16816c2
2 changed files with 24 additions and 8 deletions

View File

@ -79,15 +79,19 @@ socket.on("auth", function(data) {
for (const network of vueApp.networks) {
for (const chan of network.channels) {
for (const msg of chan.messages) {
if (msg.id > lastMessage) {
lastMessage = msg.id;
if (chan.messages.length > 0) {
const id = chan.messages[chan.messages.length - 1].id;
if (lastMessage < id) {
lastMessage = id;
}
}
}
}
socket.emit("auth", {user, token, lastMessage});
const openChannel = (vueApp.activeChannel && vueApp.activeChannel.channel.id) || null;
socket.emit("auth", {user, token, lastMessage, openChannel});
}
}

View File

@ -331,11 +331,23 @@ function indexRequest(req, res) {
);
}
function initializeClient(socket, client, token, lastMessage) {
function initializeClient(socket, client, token, lastMessage, openChannel) {
socket.emit("authorized");
client.clientAttach(socket.id, token);
// Client sends currently active channel on reconnect,
// pass it into `open` directly so it is verified and updated if necessary
if (openChannel) {
client.open(socket.id, openChannel);
// If client provided channel passes checks, use it. if client has invalid
// channel open (or windows like settings) then use last known server active channel
openChannel = client.attachedClients[socket.id].openChannel || client.lastActiveChannel;
} else {
openChannel = client.lastActiveChannel;
}
if (Helper.config.fileUpload.enable) {
new Uploader(socket);
}
@ -624,9 +636,9 @@ function initializeClient(socket, client, token, lastMessage) {
socket.emit("init", {
applicationServerKey: manager.webPush.vapidKeys.publicKey,
pushSubscription: client.config.sessions[token],
active: client.lastActiveChannel,
active: openChannel,
networks: client.networks.map((network) =>
network.getFilteredClone(client.lastActiveChannel, lastMessage)
network.getFilteredClone(openChannel, lastMessage)
),
token: tokenToSend,
});
@ -705,7 +717,7 @@ function performAuthentication(data) {
let token = null;
const finalInit = () => {
initializeClient(socket, client, token, data.lastMessage || -1);
initializeClient(socket, client, token, data.lastMessage || -1, data.openChannel);
if (!Helper.config.public) {
client.manager.updateUser(client.name, {