This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/initializers/message-bus.js
Sam Saffron 25f1f23288
FEATURE: Stricter rules for user presence
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
2020-03-26 17:36:52 +11:00

92 lines
2.7 KiB
JavaScript

// Initialize the message bus to receive messages.
import userPresent from "discourse/lib/user-presence";
import { handleLogoff } from "discourse/lib/ajax";
function ajax(opts) {
if (opts.complete) {
const oldComplete = opts.complete;
opts.complete = function(xhr, stat) {
handleLogoff(xhr);
oldComplete(xhr, stat);
};
} else {
opts.complete = handleLogoff;
}
return $.ajax(opts);
}
export default {
name: "message-bus",
after: "inject-objects",
initialize(container) {
// We don't use the message bus in testing
if (Discourse.testing) {
return;
}
const messageBus = container.lookup("message-bus:main"),
user = container.lookup("current-user:main"),
siteSettings = container.lookup("site-settings:main");
messageBus.alwaysLongPoll = Discourse.Environment === "development";
messageBus.shouldLongPollCallback = userPresent;
// we do not want to start anything till document is complete
messageBus.stop();
if (siteSettings.login_required && !user) {
// Endpoint is not available in this case, so don't try
return;
}
// jQuery ready is called on "interactive" we want "complete"
// Possibly change to document.addEventListener('readystatechange',...
// but would only stop a handful of interval, message bus being delayed by
// 500ms on load is fine. stuff that needs to catch up correctly should
// pass in a position
const interval = setInterval(() => {
if (document.readyState === "complete") {
clearInterval(interval);
messageBus.start();
}
}, 500);
messageBus.callbackInterval = siteSettings.anon_polling_interval;
messageBus.backgroundCallbackInterval =
siteSettings.background_polling_interval;
messageBus.baseUrl =
siteSettings.long_polling_base_url.replace(/\/$/, "") + "/";
if (messageBus.baseUrl !== "/") {
// zepto compatible, 1 param only
messageBus.ajax = function(opts) {
opts.headers = opts.headers || {};
opts.headers["X-Shared-Session-Key"] = $(
"meta[name=shared_session_key]"
).attr("content");
if (userPresent()) {
opts.headers["Discourse-Present"] = "true";
}
return ajax(opts);
};
} else {
messageBus.ajax = function(opts) {
opts.headers = opts.headers || {};
if (userPresent()) {
opts.headers["Discourse-Present"] = "true";
}
return ajax(opts);
};
messageBus.baseUrl = Discourse.getURL("/");
}
if (user) {
messageBus.callbackInterval = siteSettings.polling_interval;
messageBus.enableLongPolling = true;
}
}
};