FIX: Long poll if window becomes active (#13825)

FIX: Long poll if window becomes active (#13825)

This commit fixes two bugs. The first one is that onPresenceChange was called with invalid arguments and it did not register a callback. The second bug is that it triggered the wrong visibilitychange event. The function it tried to call does not exist in all versions of MessageBus. It is safer to trigger an event instead because that exists in all versions.

diff --git a/app/assets/javascripts/discourse/app/initializers/message-bus.js b/app/assets/javascripts/discourse/app/initializers/message-bus.js
index 2309ba0..b7964f6 100644
--- a/app/assets/javascripts/discourse/app/initializers/message-bus.js
+++ b/app/assets/javascripts/discourse/app/initializers/message-bus.js
@@ -56,11 +56,7 @@ export default {
     // When 20 minutes pass we stop long polling due to "shouldLongPollCallback".
     onPresenceChange({
       unseenTime: LONG_POLL_AFTER_UNSEEN_TIME,
-      callback: () => {
-        if (messageBus.onVisibilityChange) {
-          messageBus.onVisibilityChange();
-        }
-      },
+      callback: () => document.dispatchEvent(new Event("visibilitychange")),
     });
 
     if (siteSettings.login_required && !user) {
diff --git a/app/assets/javascripts/discourse/app/lib/user-presence.js b/app/assets/javascripts/discourse/app/lib/user-presence.js
index 081fff4..92f1711 100644
--- a/app/assets/javascripts/discourse/app/lib/user-presence.js
+++ b/app/assets/javascripts/discourse/app/lib/user-presence.js
@@ -44,11 +44,11 @@ export function seenUser() {
 }
 
 // register a callback for cases where presence changed
-export function onPresenceChange(maxUnseenTime, callback) {
-  if (maxUnseenTime < MIN_DELTA) {
+export function onPresenceChange({ unseenTime, callback }) {
+  if (unseenTime < MIN_DELTA) {
     throw "unseenTime is too short";
   }
-  callbacks.push({ unseenTime: maxUnseenTime, callback: callback });
+  callbacks.push({ unseenTime, callback });
 }
 
 // We could piggieback on the Scroll mixin, but it is not applied

GitHub sha: 2c1080924499641611ce31ee097ce9b3bbcaaf79

This commit appears in #13825 which was approved by eviltrout. It was merged by nbianca.