Commit 5b507ec7 authored by JC Brand's avatar JC Brand

muc: check that we're still connected if the tab becomes visible

Otherwise Converse might only realise we're no longer in the room when
the user tries to interact with it.
parent 7866247f
......@@ -43,7 +43,7 @@
enable_smacks: true,
muc_respect_autojoin: true,
message_archiving: 'always',
debug: true
loglevel: 'debug'
});
</script>
</body>
......
......@@ -926,6 +926,37 @@ async function getLoginCredentialsFromBrowser () {
}
}
_converse.saveWindowState = function (ev) {
// XXX: eventually we should be able to just use
// document.visibilityState (when we drop support for older
// browsers).
let state;
const event_map = {
'focus': "visible",
'focusin': "visible",
'pageshow': "visible",
'blur': "hidden",
'focusout': "hidden",
'pagehide': "hidden"
};
ev = ev || document.createEvent('Events');
if (ev.type in event_map) {
state = event_map[ev.type];
} else {
state = document.hidden ? "hidden" : "visible";
}
_converse.windowState = state;
/**
* Triggered when window state has changed.
* Used to determine when a user left the page and when came back.
* @event _converse#windowStateChanged
* @type { object }
* @property{ string } state - Either "hidden" or "visible"
* @example _converse.api.listen.on('windowStateChanged', obj => { ... });
*/
_converse.api.trigger('windowStateChanged', {state});
}
function registerGlobalEventHandlers () {
document.addEventListener("visibilitychange", _converse.saveWindowState);
......@@ -1158,37 +1189,6 @@ _converse.initialize = async function (settings, callback) {
}
};
this.saveWindowState = function (ev) {
// XXX: eventually we should be able to just use
// document.visibilityState (when we drop support for older
// browsers).
let state;
const event_map = {
'focus': "visible",
'focusin': "visible",
'pageshow': "visible",
'blur': "hidden",
'focusout': "hidden",
'pagehide': "hidden"
};
ev = ev || document.createEvent('Events');
if (ev.type in event_map) {
state = event_map[ev.type];
} else {
state = document.hidden ? "hidden" : "visible";
}
_converse.windowState = state;
/**
* Triggered when window state has changed.
* Used to determine when a user left the page and when came back.
* @event _converse#windowStateChanged
* @type { object }
* @property{ string } state - Either "hidden" or "visible"
* @example _converse.api.listen.on('windowStateChanged', obj => { ... });
*/
_converse.api.trigger('windowStateChanged', {state});
};
this.bindResource = async function () {
/**
* Synchronous event triggered before we send an IQ to bind the user's
......@@ -1252,7 +1252,7 @@ _converse.api = {
/**
* Terminates the connection.
*
* @method _converse.api.connection.disconnect
* @method _converse.api.connection.disconnectkjjjkk
* @memberOf _converse.api.connection
*/
disconnect () {
......
......@@ -2255,6 +2255,12 @@ converse.plugins.add('converse-muc', {
_converse.api.trigger('roomsAutoJoined');
}
async function onWindowStateChanged (data) {
if (data.state === 'visible' && _converse.api.connection.connected()) {
const rooms = await _converse.api.rooms.get();
rooms.forEach(room => room.rejoinIfNecessary());
}
}
/************************ BEGIN Event Handlers ************************/
_converse.api.listen.on('beforeTearDown', () => {
......@@ -2262,6 +2268,7 @@ converse.plugins.add('converse-muc', {
groupchats.forEach(gc => u.safeSave(gc, {'connection_status': converse.ROOMSTATUS.DISCONNECTED}));
});
_converse.api.listen.on('windowStateChanged', onWindowStateChanged);
_converse.api.listen.on('addClientFeatures', () => {
if (_converse.allow_muc) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment