DEV: long poll for 20 extra minutes when user stops interacting

DEV: long poll for 20 extra minutes when user stops interacting

We have no way of detecting if a browser window is behind another window or off screen on a virtual desktop.

In some cases we may want events to be delivered quicker to the browser. Specifically a user may still have a window in view but is not interacting.

This gives users 20 minutes of extra “long polling time” prior to shifting to short polling.

diff --git a/app/assets/javascripts/discourse/initializers/message-bus.js b/app/assets/javascripts/discourse/initializers/message-bus.js
index 61905e4..f732065 100644
--- a/app/assets/javascripts/discourse/initializers/message-bus.js
+++ b/app/assets/javascripts/discourse/initializers/message-bus.js
@@ -2,6 +2,8 @@
 import userPresent from "discourse/lib/user-presence";
 import { handleLogoff } from "discourse/lib/ajax";
 
+const LONG_POLL_AFTER_UNSEEN_TIME = 1200000; // 20 minutes
+
 function ajax(opts) {
   if (opts.complete) {
     const oldComplete = opts.complete;
@@ -31,7 +33,8 @@ export default {
       siteSettings = container.lookup("site-settings:main");
 
     messageBus.alwaysLongPoll = Discourse.Environment === "development";
-    messageBus.shouldLongPollCallback = userPresent;
+    messageBus.shouldLongPollCallback = () =>
+      userPresent(LONG_POLL_AFTER_UNSEEN_TIME);
 
     // we do not want to start anything till document is complete
     messageBus.stop();
diff --git a/app/assets/javascripts/discourse/lib/user-presence.js b/app/assets/javascripts/discourse/lib/user-presence.js
index 67d3329..7d6e02d 100644
--- a/app/assets/javascripts/discourse/lib/user-presence.js
+++ b/app/assets/javascripts/discourse/lib/user-presence.js
@@ -10,10 +10,11 @@ const MAX_UNSEEN_TIME = 60000;
 
 let seenUserTime = Date.now();
 
-export default function() {
+export default function(maxUnseenTime) {
+  maxUnseenTime = maxUnseenTime === undefined ? MAX_UNSEEN_TIME : maxUnseenTime;
   const now = Date.now();
 
-  if (seenUserTime + MAX_UNSEEN_TIME < now) {
+  if (seenUserTime + maxUnseenTime < now) {
     return false;
   }
 

GitHub sha: bed3f7f6