From c4d5c0c91b9b2335032318c8739b154be2aed8fa Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 23 Jul 2020 13:43:29 -0400 Subject: [PATCH] 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. --- .../app/initializers/url-redirects.js | 6 +++- .../discourse/app/lib/utilities.js | 27 +++++++-------- .../acceptance/login-redirect-test.js | 12 +++++-- test/javascripts/lib/utilities-test.js | 33 +++++++++---------- 4 files changed, 44 insertions(+), 34 deletions(-) diff --git a/app/assets/javascripts/discourse/app/initializers/url-redirects.js b/app/assets/javascripts/discourse/app/initializers/url-redirects.js index 69bdff1ee4a..819c1d112b7 100644 --- a/app/assets/javascripts/discourse/app/initializers/url-redirects.js +++ b/app/assets/javascripts/discourse/app/initializers/url-redirects.js @@ -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", diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.js index f105b3bca03..f9e073dc7a3 100644 --- a/app/assets/javascripts/discourse/app/lib/utilities.js +++ b/app/assets/javascripts/discourse/app/lib/utilities.js @@ -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; diff --git a/test/javascripts/acceptance/login-redirect-test.js b/test/javascripts/acceptance/login-redirect-test.js index 752549307f3..aef58f7228e 100644 --- a/test/javascripts/acceptance/login-redirect-test.js +++ b/test/javascripts/acceptance/login-redirect-test.js @@ -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(), diff --git a/test/javascripts/lib/utilities-test.js b/test/javascripts/lib/utilities-test.js index b401ba634c3..6821ce5c78d 100644 --- a/test/javascripts/lib/utilities-test.js +++ b/test/javascripts/lib/utilities-test.js @@ -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 => {