DEV: Fix presence testing in legacy ember environment (#14596)

DEV: Fix presence testing in legacy ember environment (#14596)

The legacy testing environment will remove the User.current() value before disposing of controllers/components. Presence often involves making HTTP calls during disposal of components, so this can cause issues.

Production, and the modern Ember-CLI environment, do not require this hack, so it is behind an isTesting() && isLegacyEmber() check.

diff --git a/app/assets/javascripts/discourse/app/services/presence.js b/app/assets/javascripts/discourse/app/services/presence.js
index 85ae22b..ac1ba6d 100644
--- a/app/assets/javascripts/discourse/app/services/presence.js
+++ b/app/assets/javascripts/discourse/app/services/presence.js
@@ -5,7 +5,7 @@ import { ajax } from "discourse/lib/ajax";
 import { cancel, debounce, later, next, once, throttle } from "@ember/runloop";
 import Session from "discourse/models/session";
 import { Promise } from "rsvp";
-import { isTesting } from "discourse-common/config/environment";
+import { isLegacyEmber, isTesting } from "discourse-common/config/environment";
 import User from "discourse/models/user";
 
 const PRESENCE_INTERVAL_S = 30;
@@ -464,8 +464,14 @@ export default class PresenceService extends Service {
         }
       });
     } catch (e) {
-      // Updating server failed. Put the failed events
-      // back in the queue for next time
+      if (e.jqXHR?.status === 403 && isTesting() && isLegacyEmber()) {
+        // Legacy testing environment will remove the User.current() value before disposing of controllers/components.
+        // Presence often involves making HTTP calls during disposal of components, so this can cause issues.
+        // Modern Ember-CLI environment does not require this hack
+        return;
+      }
+
+      // Put the failed events back in the queue for next time
       this._queuedEvents.unshift(...queue);
       if (e.jqXHR?.status === 429) {
         // Rate limited. No need to raise, we'll try again later

GitHub sha: bc68da24cd878dbd760f6a121c6899ae6a288f85

This commit appears in #14596 which was approved by markvanlan. It was merged by davidtaylorhq.