Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: explicit default homepage setting (#41785)

Krisd2d79d290e49
diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb
index 609039f3a35..1bb81ab118e 100644
--- a/app/controllers/list_controller.rb
+++ b/app/controllers/list_controller.rb
@@ -90,8 +90,11 @@ class ListController < ApplicationController
if Discourse.anonymous_filters.include?(filter)
@description = SiteSetting.site_description
- @rss = filter
- @rss_description = filter
+
+ if feed_route_exists?(filter)
+ @rss = filter
+ @rss_description = filter
+ end
# Note the first is the default and we don't add a title
if (filter.to_s != current_homepage) && use_crawler_layout?
@@ -382,6 +385,26 @@ class ListController < ApplicationController
private
+ def self.feed_route_exists?(filter)
+ @feed_route_exists ||= {}
+ @feed_route_exists.fetch(filter) do
+ @feed_route_exists[filter] = begin
+ Rails.application.routes.url_for(
+ controller: "list",
+ action: "#{filter}_feed",
+ only_path: true,
+ )
+ true
+ rescue ActionController::UrlGenerationError
+ false
+ end
+ end
+ end
+
+ def feed_route_exists?(filter)
+ self.class.feed_route_exists?(filter)
+ end
+
def topic_query(user = current_user, opts = {})
TopicQuery.new(user, build_topic_list_options.merge(opts))
end
diff --git a/app/models/homepage_site_setting.rb b/app/models/homepage_site_setting.rb
new file mode 100644
index 00000000000..be55fad16ac
--- /dev/null
+++ b/app/models/homepage_site_setting.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+require "enum_site_setting"
+
+class HomepageSiteSetting < EnumSiteSetting
+ def self.valid_value?(val)
+ val == "" || values.any? { |v| v[:value] == val }
+ end
+
+ def self.values
+ # A blank value means the homepage is derived from the first top_menu item.
+ [{ name: "admin.homepage.top_menu_default", value: "" }] +
+ TopMenu.homepage_choices.map { |f| { name: "filters.#{f}.title", value: f } }
+ end
+
+ def self.translate_names?
+ true
+ end
+end
diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb
index fa611479d1f..f821549fdd7 100644
--- a/app/models/site_setting.rb
+++ b/app/models/site_setting.rb
@@ -190,7 +190,13 @@ class SiteSetting < ActiveRecord::Base
end
def self.homepage
- top_menu_items[0].name
+ configured = default_homepage.presence
+
+ if configured && TopMenu.homepage_choices.include?(configured)
+ configured
+ else
+ top_menu_items[0].name
+ end
end
def self.anonymous_menu_items
@@ -198,6 +204,8 @@ class SiteSetting < ActiveRecord::Base
end
def self.anonymous_homepage
+ return homepage if anonymous_menu_items.include?(homepage)
+
top_menu_items
.map { |item| item.name }
.select { |item| anonymous_menu_items.include?(item) }
diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb
index ac407941b6a..7b6b3177121 100644
--- a/app/serializers/site_serializer.rb
+++ b/app/serializers/site_serializer.rb
@@ -11,6 +11,7 @@ class SiteSerializer < ApplicationSerializer
:trust_levels,
:groups,
:filters,
+ :homepage_choices,
:periods,
:top_menu_items,
:anonymous_top_menu_items,
@@ -223,6 +224,10 @@ class SiteSerializer < ApplicationSerializer
Discourse.filters.map(&:to_s)
end
+ def homepage_choices
+ TopMenu.homepage_choices
+ end
+
def periods
TopTopic.periods.map(&:to_s)
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index dd195b63996..7e385ea4f35 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -2298,7 +2298,7 @@ en:
any: "any"
homepage:
- default: "(default)"
+ default: "Default"
password_confirmation:
title: "Password Again"
@@ -8197,6 +8197,9 @@ en:
header_dropdown: "Header Dropdown"
legacy: "Legacy"
+ homepage:
+ top_menu_default: "First item in top menu"
+
backups:
title: "Backups"
files_title: "Backup files"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index eacaf3a0661..80675b063ed 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2079,7 +2079,8 @@ en:
suppress_secured_categories_from_admin: "Suppress private topics and PMs in some parts of UI for admins. Content will still be visible in some places. This is not a security feature: admins can always access all content on the site."
include_secure_categories_in_tag_counts: "When enabled, count of topics for a tag will include topics that are in read restricted categories for all users. When disabled, normal users are only shown a count of topics for a tag where all the topics are in public categories."
display_personal_messages_tag_counts: "When enabled, count of personal messages tagged with a given tag will be displayed."
- top_menu: "Determine which items appear in the homepage navigation, and in what order."
+ default_homepage: "The page people land on when they visit the site. When not set, the first item in {{setting:top_menu}} is used. Individuals can override this in their preferences."
+ top_menu: "Determine which navigation items appear on the discovery pages, and in what order."
custom_homepage_crawler_route: "Route shown to web crawlers when a custom homepage is enabled."
post_menu: "Configure the visibility and order of default post menu items. Additional items added by plugins or themes are managed separately and won’t appear in this list."
post_menu_hidden_items: "The menu items to hide by default in the post menu unless an expansion ellipsis is clicked on. Additional items added by plugins or themes are managed separately and won’t appear in this list."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index d0a1dd4b90b..3530dd23c74 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -315,6 +315,13 @@ basic:
upcoming_change_default_override:
upcoming_change: "enable_unified_new"
new_default: "latest|new|hot|categories"
+ default_homepage:
+ client: true
+ refresh: true
+ type: enum
+ default: ""
+ enum: "HomepageSiteSetting"
+ area: "interface"
custom_homepage_crawler_route:
type: enum
default: "latest"
diff --git a/frontend/discourse/admin/lib/setting-object-helper.js b/frontend/discourse/admin/lib/setting-object-helper.js
index 9fe9e692465..9f107087f99 100644
--- a/frontend/discourse/admin/lib/setting-object-helper.js
+++ b/frontend/discourse/admin/lib/setting-object-helper.js
@@ -30,7 +30,8 @@ export default class SettingObjectHelper {
return this.settingObj.get("valueProperty");
}
- if (isPresent(this.validValues?.[0]?.value)) {
+ const first = this.validValues?.[0];
+ if (first && typeof first === "object" && "value" in first) {
return "value";
}
return null;
diff --git a/frontend/discourse/app/controllers/preferences/interface.js b/frontend/discourse/app/controllers/preferences/interface.js
index ed1b72a94d8..68295192358 100644
--- a/frontend/discourse/app/controllers/preferences/interface.js
+++ b/frontend/discourse/app/controllers/preferences/interface.js
@@ -17,7 +17,10 @@ import {
setLocalTheme,
} from "discourse/lib/theme-selector";
import { applyValueTransformer } from "discourse/lib/transformer";
-import { setDefaultHomepage } from "discourse/lib/utilities";
+import {
+ setDefaultHomepage,
+ siteDefaultHomepage,
+} from "discourse/lib/utilities";
import { AUTO_DELETE_PREFERENCES } from "discourse/models/bookmark";
import { i18n } from "discourse-i18n";
@@ -261,7 +264,7 @@ export default class InterfaceController extends Controller {
}
homeChanged() {
- const siteHome = this.siteSettings.top_menu.split("|")[0].split(",")[0];
+ const siteHome = siteDefaultHomepage(this.siteSettings);
if (this.model.canPickThemeWithCustomHomepage) {
USER_HOMES[-1] = "custom";
@@ -280,16 +283,14 @@ export default class InterfaceController extends Controller {
homeValues[newKey] = newValue;
});
- let result = [];
+ let result = [{ name: i18n("user.homepage.default"), value: -1 }];
- if (this.model.canPickThemeWithCustomHomepage) {
- result.push({
- name: i18n("user.homepage.default"),
- value: -1,
- });
- }
+ const siteHome = siteDefaultHomepage(this.siteSettings);
+ const availableIds = this.siteSettings.top_menu
+ .split("|")
+ .filter((m) => m !== siteHome);
+ availableIds.unshift(siteHome);
- const availableIds = this.siteSettings.top_menu.split("|");
const userHome = USER_HOMES[this.get("model.user_option.homepage_id")];
if (userHome && !availableIds.includes(userHome)) {
diff --git a/frontend/discourse/app/lib/utilities.js b/frontend/discourse/app/lib/utilities.js
index 185496727bf..6fef64e9e6d 100644
--- a/frontend/discourse/app/lib/utilities.js
+++ b/frontend/discourse/app/lib/utilities.js
@@ -8,6 +8,7 @@ import discourseLater from "discourse/lib/later";
import { processSelectionFragment } from "discourse/lib/selection/preserve-list-structure";
import { parseAsync } from "discourse/lib/text";
import toMarkdown from "discourse/lib/to-markdown";
+import Site from "discourse/models/site";
import { capabilities } from "discourse/services/capabilities";
import { i18n } from "discourse-i18n";
@@ -279,11 +280,21 @@ export function setCaretPosition(ctrl, pos) {
}
}
+export function siteDefaultHomepage(siteSettings) {
+ const configured = siteSettings.default_homepage;
+
+ const choices = Site.current()?.homepage_choices;
+ if (configured && (!choices || choices.includes(configured))) {
+ return configured;
+ }
+
+ return siteSettings.top_menu.split("|")[0].split(",")[0];
+}
+
export function initializeDefaultHomepage(siteSettings) {
const sel = document.querySelector("meta[name='discourse_current_homepage']");
const homepage =
- sel?.getAttribute("content") ||
- siteSettings.top_menu.split("|")[0].split(",")[0];
+ sel?.getAttribute("content") || siteDefaultHomepage(siteSettings);
setDefaultHomepage(homepage);
}
diff --git a/frontend/discourse/select-kit/components/select-kit.js b/frontend/discourse/select-kit/components/select-kit.js
index f048246b8b8..08c231da6f7 100644
--- a/frontend/discourse/select-kit/components/select-kit.js
+++ b/frontend/discourse/select-kit/components/select-kit.js
@@ -560,8 +560,14 @@ export default class SelectKit extends Component {
}
if (this.singleSelect) {
- value = isPresent(value.firstObject) ? value.firstObject : null;
- items = isPresent(items.firstObject) ? items.firstObject : null;
+ const firstValue = value.firstObject;
+ const firstItem = items.firstObject;
+
+ value =
+ isPresent(firstValue) || (firstValue === "" && firstItem)
+ ? firstValue
+ : null;
+ items = isPresent(firstItem) ? firstItem : null;
}
this._boundaryActionHandler("onChange", value, items);
@@ -925,7 +931,9 @@ export default class SelectKit extends Component {
return;
}
- if (!isPresent(value)) {
+ // A row may explicitly carry a blank value (e.g. "use the default");
+ // selecting it is a real selection, not a clear.
+ if (!isPresent(value) && !(value === "" && item)) {
this._onClearSelection();
} else {
const existingItem = this.findValue(this.mainCollection, item);
diff --git a/frontend/discourse/select-kit/components/single-select.gjs b/frontend/discourse/select-kit/components/single-select.gjs
index 1d197d86ad3..9bdc3c6a0c6 100644
--- a/frontend/discourse/select-kit/components/single-select.gjs
+++ b/frontend/discourse/select-kit/components/single-select.gjs
@@ -46,6 +46,16 @@ export default class SingleSelect extends SelectKitComponent {
);
}
} else {
+ if (this.value === "" && this.selectKit.valueProperty) {
+ const blankItem = (this.content || []).find(
+ (item) => item[this.selectKit.valueProperty] === ""
+ );
+
+ if (blankItem) {
+ return this.selectKit.modifySelection(blankItem);
+ }
+ }
+
return this.selectKit.noneItem;
}
}
diff --git a/frontend/discourse/tests/fixtures/site-fixtures.js b/frontend/discourse/tests/fixtures/site-fixtures.js
index 32a8765ec37..196be4c5b24 100644
--- a/frontend/discourse/tests/fixtures/site-fixtures.js
+++ b/frontend/discourse/tests/fixtures/site-fixtures.js
@@ -44,6 +44,18 @@ const siteFixtures = {
"hot",
"unseen"
],
+ homepage_choices: [
+ "latest",
+ "new",
+ "unseen",
+ "top",
+ "categories",
+ "read",
+ "posted",
+ "bookmarks",
+ "hot",
+ "unread"
+ ],
periods: ["all", "yearly", "quarterly", "monthly", "weekly", "daily"],
top_menu_items: [
"latest",
diff --git a/frontend/discourse/tests/integration/components/select-kit/combo-box-test.gjs b/frontend/discourse/tests/integration/components/select-kit/combo-box-test.gjs
index f493e4fabf0..bceb4803896 100644
--- a/frontend/discourse/tests/integration/components/select-kit/combo-box-test.gjs
+++ b/frontend/discourse/tests/integration/components/select-kit/combo-box-test.gjs
@@ -31,6 +31,37 @@ module("Integration | Component | SelectKit | ComboBox", function (hooks) {
this.set("subject", selectKit());
});
+ test("selecting a row with a blank value", async function (assert) {
+ setDefaultState(this, {
+ content: [{ value: "", name: "default" }, ...DEFAULT_CONTENT],
+ value: "latest",
+ onChange: (value) => this.set("value", value),
+ });
+
+ await render(
+ <template>
+ <ComboBox
+ @content={{this.content}}
+ @value={{this.value}}
+ @valueProperty="value"
+ @nameProperty="name"
+ @onChange={{this.onChange}}
+ @options={{hash castInteger=true}}
+ />
+ </template>
+ );
+
+ await this.subject.expand();
+ await this.subject.selectRowByValue("");
+
+ assert.strictEqual(this.value, "", "onChange receives the blank value");
+ assert.strictEqual(
+ this.subject.header().name(),
+ "default",
+ "the blank-value row displays as selected before any save"
+ );
+ });
+
test("options.clearable", async function (assert) {
setDefaultState(this, {
clearable: true,
diff --git a/frontend/discourse/tests/integration/components/select-kit/single-select-test.gjs b/frontend/discourse/tests/integration/components/select-kit/single-select-test.gjs
index 16c1bf2f40f..29319cda13a 100644
--- a/frontend/discourse/tests/integration/components/select-kit/single-select-test.gjs
+++ b/frontend/discourse/tests/integration/components/select-kit/single-select-test.gjs
@@ -221,6 +221,40 @@ module("Integration | Component | SelectKit | SingleSelect", function (hooks) {
assert.strictEqual(this.subject.selectedRow().value(), this.value);
});
+ test("selects a row with a blank value", async function (assert) {
+ setDefaultState(this, {
+ content: [{ id: "", name: "default" }, ...DEFAULT_CONTENT],
+ value: "",
+ });
+
+ await render(
+ <template>
+ <SingleSelect
+ @value={{this.value}}
+ @content={{this.content}}
+ @nameProperty={{this.nameProperty}}
+ @valueProperty={{this.valueProperty}}
+ @onChange={{this.onChange}}
+ />
+ </template>
+ );
+
+ assert.strictEqual(this.subject.header().name(), "default");
+
+ await this.subject.expand();
+ await this.subject.selectRowByValue(1);
+ assert.strictEqual(this.subject.header().name(), "foo");
+
+ await this.subject.expand();
+ await this.subject.selectRowByValue("");
+ assert.strictEqual(
+ this.subject.header().name(),
+ "default",
+ "selecting the blank-value row selects it rather than clearing"
+ );
+ assert.strictEqual(this.value, "");
+ });
+
test("none:string", async function (assert) {
I18n.translations[I18n.locale].js.test = { none: "(default)" };
setDefaultState(this, { value: 1 });
diff --git a/frontend/discourse/tests/unit/lib/setting-object-helper-test.js b/frontend/discourse/tests/unit/lib/setting-object-helper-test.js
index fce65498019..5fe0da8de45 100644
--- a/frontend/discourse/tests/unit/lib/setting-object-helper-test.js
+++ b/frontend/discourse/tests/unit/lib/setting-object-helper-test.js
@@ -28,6 +28,20 @@ module("Unit | Lib | setting-object-helper", function (hooks) {
assert.strictEqual(helper.computedNameProperty, "name");
});
+ test("object with a blank first value", function (assert) {
+ const settingObj = EmberObject.create({
+ valid_values: [
+ { value: "", name: "bar" },
+ { value: "foo", name: "baz" },
+ ],
+ });
+
+ const helper = new SettingObjectHelper(settingObj);
+
+ assert.strictEqual(helper.computedValueProperty, "value");
+ assert.strictEqual(helper.computedNameProperty, "name");
+ });
+
test("no values", function (assert) {
const settingObj = EmberObject.create({
valid_values: [],
diff --git a/frontend/discourse/tests/unit/lib/utilities-test.js b/frontend/discourse/tests/unit/lib/utilities-test.js
index b36f60b0e48..917799289f7 100644
--- a/frontend/discourse/tests/unit/lib/utilities-test.js
+++ b/frontend/discourse/tests/unit/lib/utilities-test.js
@@ -24,6 +24,7 @@ import {
toAsciiPrintable,
unicodeSlugify,
} from "discourse/lib/utilities";
+import Site from "discourse/models/site";
import {
mdTable,
mdTableNonUniqueHeadings,
@@ -111,6 +112,48 @@ module("Unit | Utilities", function (hooks) {
);
});
+ test("defaultHomepage prefers the default_homepage site setting", function (assert) {
+ const siteSettings = getOwner(this).lookup("service:site-settings");
+ siteSettings.top_menu = "top|latest|hot";
+ siteSettings.default_homepage = "hot";
+ initializeDefaultHomepage(siteSettings);
+
+ assert.strictEqual(
+ defaultHomepage(),
+ "hot",
+ "default homepage is pulled from the default_homepage site setting"
+ );
+ });
+
+ test("defaultHomepage ignores a default_homepage value that is no longer an eligible choice", function (assert) {
+ const siteSettings = getOwner(this).lookup("service:site-settings");
+ siteSettings.top_menu = "top|latest|hot";
+ siteSettings.default_homepage = "votes";
+
+ const site = Site.current();
+ const originalChoices = site.homepage_choices;
+
+ try {
+ site.set("homepage_choices", ["latest", "top", "hot"]);
+ initializeDefaultHomepage(siteSettings);
+ assert.strictEqual(
+ defaultHomepage(),
+ "top",
+ "an ineligible value falls back to the first top_menu item"
+ );
+
+ site.set("homepage_choices", ["latest", "top", "hot", "votes"]);
+ initializeDefaultHomepage(siteSettings);
+ assert.strictEqual(
+ defaultHomepage(),
+ "votes",
+ "an eligible value is used"
+ );
+ } finally {
+ site.set("homepage_choices", originalChoices);
+ }
+ });
+
test("setDefaultHomepage", function (assert) {
const siteSettings = getOwner(this).lookup("service:site-settings");
initializeDefaultHomepage(siteSettings);
diff --git a/lib/top_menu.rb b/lib/top_menu.rb
index 3deac7ac903..d6c9ee5f74b 100644
--- a/lib/top_menu.rb
+++ b/lib/top_menu.rb
@@ -16,4 +16,8 @@ module TopMenu
def self.crawler_homepage_choices
choices & (Discourse.anonymous_filters.map(&:to_s) + %w[new categories])
end
+
+ def self.homepage_choices
+ choices | (Discourse.filters.map(&:to_s) - %w[unread])
+ end
end
diff --git a/spec/lib/homepage_helper_spec.rb b/spec/lib/homepage_helper_spec.rb
index 527e2ea1aa8..35075196cf8 100644
--- a/spec/lib/homepage_helper_spec.rb
+++ b/spec/lib/homepage_helper_spec.rb
@@ -45,24 +45,55 @@ RSpec.describe HomepageHelper do
expect(HomepageHelper.resolve).to eq("custom")
end
- context "when first item in top menu is not valid for anons" do
- before { SiteSetting.top_menu = "new|top|latest" }
+ context "when the configured homepage is not valid for anons" do
+ before do
+ SiteSetting.top_menu = "new|top|latest"
+ SiteSetting.default_homepage = "new"
+ end
it "distinguishes between auth homepage and anon homepage" do
expect(HomepageHelper.resolve(nil, user)).to eq("new")
- # new is not a valid route for anon users, anon homepage is next item, top
+ # new is not a valid route for anon users, so the anon homepage falls back
+ # to the first anon-visible item in the top menu, top
expect(HomepageHelper.resolve).to eq(SiteSetting.anonymous_homepage)
expect(HomepageHelper.resolve).to eq("top")
end
end
+ context "when default_homepage is set" do
+ before { SiteSetting.top_menu = "latest|new|top|categories" }
+
+ it "uses default_homepage regardless of top_menu order" do
+ SiteSetting.default_homepage = "categories"
+ expect(HomepageHelper.resolve(nil, user)).to eq("categories")
+ expect(HomepageHelper.resolve).to eq("categories")
+ end
+
+ it "uses default_homepage even when it is not one of the top_menu items" do
+ SiteSetting.top_menu = "latest|new|categories"
+ SiteSetting.default_homepage = "top"
+ expect(HomepageHelper.resolve(nil, user)).to eq("top")
+ expect(HomepageHelper.resolve).to eq("top")
+ end
+ end
+
+ context "when default_homepage is not set" do
+ before { SiteSetting.top_menu = "new|top|latest" }
+
+ it "falls back to the first top_menu item, and the first anon-visible one for anons" do
+ expect(HomepageHelper.resolve(nil, user)).to eq("new")
+ expect(HomepageHelper.resolve).to eq("top")
+ end
+ end
+
context "with login required" do
before do
SiteSetting.login_required = true
SiteSetting.top_menu = "new|top|latest"
+ SiteSetting.default_homepage = "new"
end
- it "returns a blank route for anon, first result from top menu for authenticated user" do
+ it "returns a blank route for anon, and the configured homepage for an authenticated user" do
expect(HomepageHelper.resolve).to eq("blank")
expect(HomepageHelper.resolve(nil, user)).to eq("new")
end
diff --git a/spec/models/homepage_site_setting_spec.rb b/spec/models/homepage_site_setting_spec.rb
new file mode 100644
index 00000000000..fb4a7ca234b
--- /dev/null
+++ b/spec/models/homepage_site_setting_spec.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+describe HomepageSiteSetting do
+ it "offers the top_menu fallback and every homepage choice" do
+ values = described_class.values
+
+ expect(values.first).to eq({ name: "admin.homepage.top_menu_default", value: "" })
+ expect(values.map { |v| v[:value] }).to include(*TopMenu.choices)
+ expect(described_class.translate_names?).to eq(true)
+ end
+
+ it "includes filters registered at runtime" do
+ filters = Discourse.filters
+ Discourse.stubs(:filters).returns(filters + [:custom_filter])
+
+ expect(described_class.values).to include(
+ { name: "filters.custom_filter.title", value: "custom_filter" },
+ )
+ end
+
+ it "does not offer unread when it is excluded from top menu choices" do
+ TopMenu.stubs(:choices).returns(%w[latest new top categories])
+
+ expect(described_class.values.map { |v| v[:value] }).not_to include("unread")
+ end
+
+ it "validates values" do
+ expect(described_class.valid_value?("")).to eq(true)
+ expect(described_class.valid_value?("latest")).to eq(true)
+ expect(described_class.valid_value?("invalid")).to eq(false)
+ end
+end
diff --git a/spec/models/site_setting_spec.rb b/spec/models/site_setting_spec.rb
index 6188b3ee824..ac8adc738d5 100644
--- a/spec/models/site_setting_spec.rb
+++ b/spec/models/site_setting_spec.rb
@@ -77,10 +77,38 @@ RSpec.describe SiteSetting do
end
describe "homepage" do
- it "has homepage" do
+ it "uses default_homepage when set" do
+ SiteSetting.default_homepage = "bookmarks"
+ expect(SiteSetting.homepage).to eq("bookmarks")
+ end
+
+ it "falls back to the first top_menu item when default_homepage is not set" do
SiteSetting.top_menu = "bookmarks|latest"
expect(SiteSetting.homepage).to eq("bookmarks")
end
+
+ it "falls back to the first top_menu item when the persisted value's filter is no longer registered" do
+ SiteSetting.top_menu = "categories|latest"
+
+ filters = Discourse.filters
+ Discourse.stubs(:filters).returns(filters + [:votes])
+ SiteSetting.default_homepage = "votes"
+ expect(SiteSetting.homepage).to eq("votes")
+
+ Discourse.stubs(:filters).returns(filters)
+ expect(SiteSetting.homepage).to eq("categories")
+ end
+
+ it "falls back when the persisted value is no longer an eligible choice" do
+ SiteSetting.enable_unified_new = false
+ SiteSetting.top_menu = "categories|latest"
+ SiteSetting.default_homepage = "unread"
+ expect(SiteSetting.homepage).to eq("unread")
+
+ # enabling unified-new removes unread from the eligible homepage choices
+ SiteSetting.enable_unified_new = true
+ expect(SiteSetting.homepage).to eq("categories")
+ end
end
end
diff --git a/spec/requests/api/schemas/json/site_response.json b/spec/requests/api/schemas/json/site_response.json
index 940ec01adcf..26884aa65d0 100644
--- a/spec/requests/api/schemas/json/site_response.json
+++ b/spec/requests/api/schemas/json/site_response.json
@@ -329,6 +329,10 @@
"type": "array",
"items": {}
},
+ "homepage_choices": {
+ "type": "array",
+ "items": {}
+ },
"periods": {
"type": "array",
"items": {}
@@ -1039,6 +1043,7 @@
"trust_levels",
"groups",
"filters",
+ "homepage_choices",
"periods",
"top_menu_items",
"anonymous_top_menu_items",
diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb
index d1cfd66a10c..b9ea4e820dc 100644
--- a/spec/requests/list_controller_spec.rb
+++ b/spec/requests/list_controller_spec.rb
@@ -436,6 +436,57 @@ RSpec.describe ListController do
end
end
+ describe "rss feed discovery" do
+ it "advertises the feed for anonymous filters with a feed route" do
+ topic
+ get "/latest", params: { _escaped_fragment_: "true" }
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include("latest.rss")
+ end
+
+ it "omits the feed for anonymous filters without a feed route" do
+ topic
+ sign_in(user)
+
+ SiteSetting.anonymous_menu_items
+ anonymous_filters = Discourse.anonymous_filters
+ Discourse.stubs(:anonymous_filters).returns(anonymous_filters + [:new])
+
+ get "/new", params: { _escaped_fragment_: "true" }
+
+ expect(response.status).to eq(200)
+ expect(response.body).not_to include("application/rss+xml")
+ end
+ end
+
+ describe "crawler homepage rendering" do
+ fab!(:homepage_topic) { Fabricate(:post).topic }
+
+ before do
+ SiteSetting.has_login_hint = false
+ SiteSetting.top_menu = "latest|new|bookmarks|categories"
+ end
+
+ it "renders the crawler homepage without error for each reachable filter" do
+ %w[latest categories top hot new bookmarks].each do |filter|
+ SiteSetting.default_homepage = filter
+ get "/", params: { _escaped_fragment_: "true" }
+
+ expect(response.status).to eq(200), "expected 200 for default_homepage=#{filter}"
+ expect(response.body).not_to include("finish-installation")
+ end
+ end
+
+ it "falls back to an anon-visible list when the homepage is a user-scoped filter" do
+ SiteSetting.default_homepage = "bookmarks"
+ get "/", params: { _escaped_fragment_: "true" }
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include(homepage_topic.title)
+ end
+ end
+
describe "filter private messages by tag" do
fab!(:user)
fab!(:moderator)
diff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rb
index 60e1fa5475f..d848e216437 100644
--- a/spec/serializers/site_serializer_spec.rb
+++ b/spec/serializers/site_serializer_spec.rb
@@ -16,6 +16,13 @@ RSpec.describe SiteSerializer do
end
end
+ describe "#homepage_choices" do
+ it "exposes the eligible homepage choices" do
+ serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
+ expect(serialized[:homepage_choices]).to eq(TopMenu.homepage_choices)
+ end
+ end
+
describe "#user_tips" do
it "is included if enable_user_tips" do
SiteSetting.enable_user_tips = true
diff --git a/spec/system/homepage_spec.rb b/spec/system/homepage_spec.rb
index 311f73713c1..78edd0fd210 100644
--- a/spec/system/homepage_spec.rb
+++ b/spec/system/homepage_spec.rb
@@ -39,11 +39,40 @@ describe "Homepage" do
expect(page).to have_css(".navigation-container .hot.active", text: "Hot")
end
- it "defaults to first top_menu item as anonymous homepage" do
+ it "allows users to reset their homepage preference to the site default" do
+ user.user_option.update!(homepage_id: UserOption::HOMEPAGES.key("hot"))
+ SiteSetting.default_homepage = "categories"
+
+ sign_in user
+ visit "/"
+
+ expect(page).to have_css(".navigation-container .hot.active", text: "Hot")
+
+ user_preferences_interface_page.visit(user)
+
+ homepage_picker = PageObjects::Components::SelectKit.new("#home-selector")
+ homepage_picker.expand
+ homepage_picker.select_row_by_name("Default")
+
+ user_preferences_interface_page.save_changes
+
+ visit "/"
+
+ expect(page).to have_css(".navigation-container .categories.active", text: "Categories")
+ end
+
+ it "defaults to first top_menu item as anonymous homepage when default_homepage is not set" do
SiteSetting.top_menu = "categories|latest|new"
visit "/"
expect(page).to have_css(".navigation-container .categories.active", text: "Categories")
+ end
+
+ it "uses default_homepage for both anonymous and logged-in users" do
+ SiteSetting.default_homepage = "categories"
+ visit "/"
+
+ expect(page).to have_css(".navigation-container .categories.active", text: "Categories")
sign_in user
visit "/"
@@ -51,6 +80,43 @@ describe "Homepage" do
expect(page).to have_css(".navigation-container .categories.active", text: "Categories")
end
+ it "diverges for a user-scoped homepage: logged-in users get it, anon falls back" do
+ SiteSetting.top_menu = "latest|new|bookmarks|categories"
+ SiteSetting.default_homepage = "bookmarks"
+
+ visit "/"
+ expect(page).to have_current_path("/")
+ expect(page).to have_css(".navigation-container .latest.active", text: "Latest")
+
+ sign_in user
+ visit "/"
+ expect(page).to have_current_path("/")
+ expect(page).to have_css(".navigation-container .bookmarks.active", text: "Bookmarks")
+ end
+
+ it "renders an empty state when a user-scoped homepage has no content" do
+ SiteSetting.top_menu = "latest|new|bookmarks|categories"
+ SiteSetting.default_homepage = "bookmarks"
+
+ sign_in user
+ visit "/"
+
+ expect(page).to have_current_path("/")
+ expect(page).to have_css(".navigation-container .bookmarks.active", text: "Bookmarks")
+ expect(discovery.topic_list).to have_no_topics
+ end
+
+ it "renders the homepage but highlights no nav tab when the homepage is not a top_menu item" do
+ SiteSetting.top_menu = "latest|new|categories"
+ SiteSetting.default_homepage = "top"
+
+ visit "/"
+
+ expect(page).to have_css(".list-container")
+ expect(page).to have_no_css(".nav-item_top")
+ expect(page).to have_no_css(".navigation-container .active")
+ end
+
shared_examples "a custom homepage" do
it "shows the custom homepage component" do
visit "/"
@@ -98,7 +164,7 @@ describe "Homepage" do
homepage_picker = PageObjects::Components::SelectKit.new("#home-selector")
homepage_picker.expand
# user selects theme custom homepage again
- homepage_picker.select_row_by_name("(default)")
+ homepage_picker.select_row_by_name("Default")
user_preferences_interface_page.save_changes
click_logo