Commit 119966d9 authored by JC Brand's avatar JC Brand

Set flag to indicate user session is active

We then use this flag to determine whether we should use the values from
sessionStorage.

This appears to fix the problem I originally tried to fix in 607d7986.

When "cloning" a tab (e.g. via middle-click), the `active` flag will be
set and we'll create a new empty user session, otherwise it'll be false
and we can re-use the user session.
parent 8d9d0a1e
...@@ -506,12 +506,15 @@ _converse.initConnection = function () { ...@@ -506,12 +506,15 @@ _converse.initConnection = function () {
async function initUserSession (jid) { async function initUserSession (jid) {
const bare_jid = Strophe.getBareJidFromJid(jid); const bare_jid = Strophe.getBareJidFromJid(jid).toLowerCase();
const id = `converse.session-${bare_jid}`; const id = `converse.session-${bare_jid}`;
if (!_converse.session || _converse.session.get('id') !== id) { if (!_converse.session || _converse.session.get('id') !== id) {
_converse.session = new Backbone.Model({id}); _converse.session = new Backbone.Model({id});
_converse.session.browserStorage = new BrowserStorage.session(id); _converse.session.browserStorage = new BrowserStorage.session(id);
await new Promise(r => _converse.session.fetch({'success': r, 'error': r})); await new Promise(r => _converse.session.fetch({'success': r, 'error': r}));
if (_converse.session.get('active')) {
_converse.session.clear();
}
/** /**
* Triggered once the user's session has been initialized. The session is a * Triggered once the user's session has been initialized. The session is a
* cache which stores information about the user's current session. * cache which stores information about the user's current session.
...@@ -523,7 +526,9 @@ async function initUserSession (jid) { ...@@ -523,7 +526,9 @@ async function initUserSession (jid) {
} }
async function setUserJID (jid) { async function setUserJID (jid) {
jid = jid.toLowerCase(); await initUserSession(jid);
jid = _converse.session.get('jid') || jid;
if (!Strophe.getResourceFromJid(jid)) { if (!Strophe.getResourceFromJid(jid)) {
jid = jid.toLowerCase() + _converse.generateResource(); jid = jid.toLowerCase() + _converse.generateResource();
// Set JID on the connection object so that when we call // Set JID on the connection object so that when we call
...@@ -531,7 +536,6 @@ async function setUserJID (jid) { ...@@ -531,7 +536,6 @@ async function setUserJID (jid) {
// and sent to the XMPP server. // and sent to the XMPP server.
_converse.connection.jid = jid; _converse.connection.jid = jid;
} }
await initUserSession(jid);
_converse.jid = jid; _converse.jid = jid;
_converse.bare_jid = Strophe.getBareJidFromJid(jid); _converse.bare_jid = Strophe.getBareJidFromJid(jid);
_converse.resource = Strophe.getResourceFromJid(jid); _converse.resource = Strophe.getResourceFromJid(jid);
...@@ -540,7 +544,8 @@ async function setUserJID (jid) { ...@@ -540,7 +544,8 @@ async function setUserJID (jid) {
'jid': jid, 'jid': jid,
'bare_jid': _converse.bare_jid, 'bare_jid': _converse.bare_jid,
'resource': _converse.resource, 'resource': _converse.resource,
'domain': _converse.domain 'domain': _converse.domain,
'active': true
}); });
/** /**
* Triggered whenever the user's JID has been updated * Triggered whenever the user's JID has been updated
...@@ -822,6 +827,11 @@ _converse.initialize = async function (settings, callback) { ...@@ -822,6 +827,11 @@ _converse.initialize = async function (settings, callback) {
window.addEventListener('mousemove', _converse.onUserActivity); window.addEventListener('mousemove', _converse.onUserActivity);
const options = {'once': true, 'passive': true}; const options = {'once': true, 'passive': true};
window.addEventListener(_converse.unloadevent, _converse.onUserActivity, options); window.addEventListener(_converse.unloadevent, _converse.onUserActivity, options);
window.addEventListener(_converse.unloadevent, () => {
if (_converse.session) {
_converse.session.save('active', false);
}
});
_converse.everySecondTrigger = window.setInterval(_converse.onEverySecond, 1000); _converse.everySecondTrigger = window.setInterval(_converse.onEverySecond, 1000);
}; };
......
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