New Idle timer; messages not marked read if user is idle

This commit is contained in:
Scott Nonnenberg 2019-09-19 15:16:46 -07:00
parent b77246a7e0
commit 8ccb89310b
11 changed files with 55 additions and 55 deletions

View file

@ -40,6 +40,43 @@
false
);
// Idle timer - you're active for ACTIVE_TIMEOUT after one of these events
const ACTIVE_TIMEOUT = 15 * 1000;
const ACTIVE_EVENTS = [
'click',
'keypress',
'mousedown',
'mousemove',
// 'scroll', // this is triggered by Timeline re-renders, can't use
'touchstart',
'wheel',
];
const LISTENER_DEBOUNCE = 5 * 1000;
let activeHandlers = [];
let activeTimestamp = Date.now();
window.resetActiveTimer = _.throttle(() => {
const previouslyActive = window.isActive();
activeTimestamp = Date.now();
if (!previouslyActive) {
activeHandlers.forEach(handler => handler());
}
}, LISTENER_DEBOUNCE);
ACTIVE_EVENTS.forEach(name => {
document.addEventListener(name, window.resetActiveTimer, true);
});
window.isActive = () => {
const now = Date.now();
return now <= activeTimestamp + ACTIVE_TIMEOUT;
};
window.registerForActive = handler => activeHandlers.push(handler);
window.unregisterForActive = handler => {
activeHandlers = activeHandlers.filter(item => item !== handler);
};
// Load these images now to ensure that they don't flicker on first use
window.Signal.EmojiLib.preloadImages();
const images = [];
@ -712,7 +749,7 @@
}
});
window.addEventListener('focus', () => Whisper.Notifications.clear());
window.registerForActive(() => Whisper.Notifications.clear());
window.addEventListener('unload', () => Whisper.Notifications.fastClear());
Whisper.events.on('showConversation', (id, messageId) => {

View file

@ -1,14 +0,0 @@
// eslint-disable-next-line func-names
(function() {
'use strict';
let windowFocused = false;
window.addEventListener('blur', () => {
windowFocused = false;
});
window.addEventListener('focus', () => {
windowFocused = true;
});
window.isFocused = () => windowFocused;
})();

View file

@ -334,7 +334,7 @@
this.id,
[model.getReduxData()],
isNewMessage,
document.hasFocus()
window.isActive()
);
}

View file

@ -3,7 +3,6 @@
/* global drawAttention: false */
/* global i18n: false */
/* global isFocused: false */
/* global Signal: false */
/* global storage: false */
/* global Whisper: false */
@ -54,7 +53,7 @@
}
const { isEnabled } = this;
const isAppFocused = isFocused();
const isAppFocused = window.isActive();
const isAudioNotificationEnabled =
storage.get('audio-notification') || false;
const isAudioNotificationSupported = Settings.isAudioNotificationSupported();

View file

@ -166,7 +166,7 @@
if (!$.contains(this.el, this.inboxView.el)) {
this.openView(this.inboxView);
}
window.focus(); // FIXME
return Promise.resolve();
},
onEmpty() {

View file

@ -442,7 +442,7 @@
id,
models.map(model => model.getReduxData()),
isNewMessage,
document.hasFocus()
window.isActive()
);
} catch (error) {
setMessagesLoading(conversationId, true);
@ -493,7 +493,7 @@
id,
models.map(model => model.getReduxData()),
isNewMessage,
document.hasFocus()
window.isActive()
);
} catch (error) {
setMessagesLoading(conversationId, false);
@ -502,10 +502,8 @@
finish();
}
};
const markMessageRead = async (messageId, forceFocus) => {
// We need a forceFocus parameter because the BrowserWindow focus event fires
// before the document realizes that it has focus.
if (!document.hasFocus() && !forceFocus) {
const markMessageRead = async messageId => {
if (!window.isActive()) {
return;
}