Previously we would consider a user "present" and "last seen" if the browser window was visible. This has many edge cases, you could be considered present and around for days just by having a window open and no screensaver on. Instead we now also check that you either clicked, transitioned around app or scrolled the page in the last minute in combination with window visibility This will lead to more reliable notifications via email and reduce load of message bus for cases where a user walks away from the terminal
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
// for android we test webkit
|
|
const hiddenProperty =
|
|
document.hidden !== undefined
|
|
? "hidden"
|
|
: document.webkitHidden !== undefined
|
|
? "webkitHidden"
|
|
: undefined;
|
|
|
|
const MAX_UNSEEN_TIME = 60000;
|
|
|
|
let seenUserTime = Date.now();
|
|
|
|
export default function() {
|
|
const now = Date.now();
|
|
|
|
if (seenUserTime + MAX_UNSEEN_TIME < now) {
|
|
return false;
|
|
}
|
|
|
|
if (hiddenProperty !== undefined) {
|
|
return !document[hiddenProperty];
|
|
} else {
|
|
return document && document.hasFocus;
|
|
}
|
|
}
|
|
|
|
export function seenUser() {
|
|
seenUserTime = Date.now();
|
|
}
|
|
|
|
// We could piggieback on the Scroll mixin, but it is not applied
|
|
// consistently to all pages
|
|
//
|
|
// We try to keep this as cheap as possible by performing absolute minimal
|
|
// amount of work when the event handler is fired
|
|
//
|
|
// An alternative would be to use a timer that looks at the scroll position
|
|
// however this will not work as message bus can issue page updates and scroll
|
|
// page around when user is not present
|
|
//
|
|
// We avoid tracking mouse move which would be very expensive
|
|
|
|
$(document).bind("touchmove.discourse-track-presence", seenUser);
|
|
$(document).bind("click.discourse-track-presence", seenUser);
|
|
$(window).bind("scroll.discourse-track-presence", seenUser);
|