DEV: Consolidate message-bus ajax implementation (#26646)

Previously it was split across multiple functions, and had multiple logic branches. This commit consolidates it into a single `mbAjax` function
This commit is contained in:
David Taylor 2024-04-16 11:16:04 +01:00 committed by GitHub
parent 1fea2bf1c5
commit 1be488e4fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 30 deletions

View File

@ -7,17 +7,27 @@ import getURL from "discourse-common/lib/get-url";
const LONG_POLL_AFTER_UNSEEN_TIME = 1200000; // 20 minutes
function ajax(opts) {
if (opts.complete) {
const oldComplete = opts.complete;
opts.complete = function (xhr, stat) {
handleLogoff(xhr);
oldComplete(xhr, stat);
};
} else {
opts.complete = handleLogoff;
function mbAjax(messageBus, opts) {
opts.headers ||= {};
if (messageBus.baseUrl !== "/") {
const key = document.querySelector(
"meta[name=shared_session_key]"
)?.content;
opts.headers["X-Shared-Session-Key"] = key;
}
if (userPresent()) {
opts.headers["Discourse-Present"] = "true";
}
const oldComplete = opts.complete;
opts.complete = function (xhr, stat) {
handleLogoff(xhr);
oldComplete?.(xhr, stat);
};
return $.ajax(opts);
}
@ -78,28 +88,9 @@ export default {
messageBus.enableChunkedEncoding = siteSettings.enable_chunked_encoding;
if (messageBus.baseUrl !== "/") {
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.ajax = (opts) => mbAjax(messageBus, opts);
messageBus.baseUrl = getURL("/");
}
messageBus.baseUrl = getURL("/");
if (user) {
messageBus.callbackInterval = siteSettings.polling_interval;