REFACTOR: Remove Discourse.SiteSettings from utilities

This involves a little refactoring of how our `defaultHomepage()` works.
It previously would check the meta tag / site settings each time it was
called but now it only checks once on application boot.
This commit is contained in:
Robin Ward 2020-07-23 13:43:29 -04:00
parent 4def5a8339
commit c4d5c0c91b
4 changed files with 44 additions and 34 deletions

View File

@ -1,4 +1,5 @@
import DiscourseURL from "discourse/lib/url";
import { initializeDefaultHomepage } from "discourse/lib/utilities";
export default {
name: "url-redirects",
@ -6,7 +7,6 @@ export default {
initialize(container) {
const currentUser = container.lookup("current-user:main");
if (currentUser) {
const username = currentUser.get("username");
DiscourseURL.rewrite(
@ -15,6 +15,10 @@ export default {
);
}
// Initialize default homepage
let siteSettings = container.lookup("site-settings:main");
initializeDefaultHomepage(siteSettings);
DiscourseURL.rewrite(/^\/u\/([^\/]+)\/?$/, "/u/$1/summary", {
exceptions: [
"/u/account-created",

View File

@ -4,7 +4,7 @@ import toMarkdown from "discourse/lib/to-markdown";
import Handlebars from "handlebars";
import { default as getURL, getURLWithCDN } from "discourse-common/lib/get-url";
const homepageSelector = "meta[name=discourse_current_homepage]";
let _defaultHomepage;
export function translateSize(size) {
switch (size) {
@ -213,23 +213,24 @@ export function setCaretPosition(ctrl, pos) {
}
}
export function defaultHomepage() {
let homepage = null;
let elem = _.first($(homepageSelector));
if (elem) {
homepage = elem.content;
export function initializeDefaultHomepage(siteSettings) {
let homepage;
let sel = document.querySelector("meta[name='discourse_current_homepage']");
if (sel) {
homepage = sel.getAttribute("content");
}
if (!homepage) {
homepage = Discourse.SiteSettings.top_menu.split("|")[0].split(",")[0];
homepage = siteSettings.top_menu.split("|")[0].split(",")[0];
}
return homepage;
setDefaultHomepage(homepage);
}
export function defaultHomepage() {
return _defaultHomepage;
}
export function setDefaultHomepage(homepage) {
let elem = _.first($(homepageSelector));
if (elem) {
elem.content = homepage;
}
_defaultHomepage = homepage;
}
export function determinePostReplaceSelection({
@ -328,7 +329,7 @@ export function clipboardHelpers(e, opts) {
const canUploadImage =
canUpload && files.filter(f => f.type.match("^image/"))[0];
const canPasteHtml =
Discourse.SiteSettings.enable_rich_text_paste &&
opts.siteSettings.enable_rich_text_paste &&
types.includes("text/html") &&
!canUploadImage;

View File

@ -1,7 +1,6 @@
import { acceptance } from "helpers/qunit-helpers";
acceptance("Login redirect", {});
acceptance("Login redirect");
QUnit.test("redirects login to default homepage", async function(assert) {
await visit("/login");
assert.equal(
@ -9,8 +8,15 @@ QUnit.test("redirects login to default homepage", async function(assert) {
"discovery.latest",
"it works when latest is the homepage"
);
this.siteSettings.top_menu = "categories|latest|top|hot";
});
acceptance("Login redirect - categories default", {
settings: {
top_menu: "categories|latest|top|hot"
}
});
QUnit.test("when site setting is categories", async function(assert) {
await visit("/login");
assert.equal(
currentPath(),

View File

@ -6,6 +6,7 @@ import {
avatarUrl,
getRawSize,
avatarImg,
initializeDefaultHomepage,
defaultHomepage,
setDefaultHomepage,
caretRowCol,
@ -132,17 +133,12 @@ QUnit.test("avatarImg", assert => {
setDevicePixelRatio(oldRatio);
});
QUnit.test("defaultHomepage", function(assert) {
this.siteSettings.top_menu = "latest|top|hot";
assert.equal(
defaultHomepage(),
"latest",
"default homepage is the first item in the top_menu site setting"
);
var meta = document.createElement("meta");
QUnit.test("defaultHomepage via meta tag", function(assert) {
let meta = document.createElement("meta");
meta.name = "discourse_current_homepage";
meta.content = "hot";
document.body.appendChild(meta);
initializeDefaultHomepage(this.siteSettings);
assert.equal(
defaultHomepage(),
"hot",
@ -151,18 +147,21 @@ QUnit.test("defaultHomepage", function(assert) {
document.body.removeChild(meta);
});
QUnit.test("setDefaultHomepage", assert => {
var meta = document.createElement("meta");
meta.name = "discourse_current_homepage";
meta.content = "hot";
document.body.appendChild(meta);
setDefaultHomepage("top");
QUnit.test("defaultHomepage via site settings", function(assert) {
this.siteSettings.top_menu = "top|latest|hot";
initializeDefaultHomepage(this.siteSettings);
assert.equal(
meta.content,
defaultHomepage(),
"top",
"default homepage set by setDefaultHomepage"
"default homepage is the first item in the top_menu site setting"
);
document.body.removeChild(meta);
});
QUnit.test("setDefaultHomepage", function(assert) {
initializeDefaultHomepage(this.siteSettings);
assert.equal(defaultHomepage(), "latest");
setDefaultHomepage("top");
assert.equal(defaultHomepage(), "top");
});
QUnit.test("caretRowCol", assert => {