0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 15:18:34 +08:00
discourse/spec/system/homepage_spec.rb
Kris d2d79d290e
FEATURE: explicit default homepage setting (#41785)
This creates an explicit "default homepage" setting, because right now
the experience of changing the homepage via the "top menu" setting isn't
very intuitive. This will also make it easier to support new homepage
options in the future that don't necessarily need to exist within "top
menu."

The setting is empty by default, and when empty it falls back to "First
item in top menu" — using the current functionality as a fallback means
we don't need a migration.

The options in the list are the core top menu choices plus anything
registered via `Discourse.filters.push(:filter)`. Registered filters are
an existing pattern that come with all the prerequisites to function as
a homepage (server & client routes, and a topic list).

So for example, how the "votes" option is produced

plugins/discourse-topic-voting/plugin.rb
```ruby
Discourse.filters.push(:votes)
Discourse.anonymous_filters.push(:votes)
```

anonymous_filters is optional, if there's no anonymous equivalent of a
given option, we fall back to the first anon-viewable top menu item
(there's always at least one)

lib/discourse_topic_voting/topic_query_extension.rb
```ruby
def list_votes
  create_list(:votes, unordered: true) do |topics|
    topics.joins(
      "LEFT JOIN topic_voting_topic_vote_count dvtvc ON dvtvc.topic_id = topics.id",
    ).order("COALESCE(dvtvc.votes_count,'0')::integer DESC, topics.bumped_at DESC")
  end
end
```

config/locales/client.en.yml
```yaml
js:
  filters:
    votes:
      title: "Votes"
      help: "topics with the all time most votes"
```

<img width="600" alt="image"
src="https://github.com/user-attachments/assets/6b580e92-1b58-477c-81ab-6ceacc020672"
/>

This also adds a persistent "Default" option to the Default Home Page
user preference, which was previously only visible when a custom
homepage is set by a theme.

A future enhancement could add a plugin API for registering non-filter
homepage options (e.g. a chat channel)

Some related fixes along for the ride:

* select-kit treated a blank value as "clear selection", so it could not
be selected or displayed... this allows "First item in top menu" as an
alias for being unset
* the admin combo-box couldn't detect when the first choice's value was
blank, and submitted the whole object
* topic lists advertised RSS feeds that don't exist for registered
filters (this was happening with /votes)
2026-07-22 16:22:19 -04:00

289 lines
8.7 KiB
Ruby
Vendored

# frozen_string_literal: true
describe "Homepage" do
fab!(:admin)
fab!(:user)
fab!(:topics) { Fabricate.times(5, :post).map(&:topic) }
let(:discovery) { PageObjects::Pages::Discovery.new }
fab!(:theme)
let(:user_preferences_interface_page) { PageObjects::Pages::UserPreferencesInterface.new }
before do
# A workaround to avoid the global notice from interfering with the tests
# It is coming from the ensure_login_hint.rb initializer and it gets
# evaluated before the tests run (and it wrongly counts 0 admins defined)
SiteSetting.global_notice = ""
end
it "shows a list of topics by default" do
visit "/"
expect(discovery.topic_list).to have_topics(count: 5)
end
it "allows users to pick their homepage" do
sign_in user
visit "/"
expect(page).to have_css(".navigation-container .latest.active", text: "Latest")
user_preferences_interface_page.visit(user)
homepage_picker = PageObjects::Components::SelectKit.new("#home-selector")
homepage_picker.expand
homepage_picker.select_row_by_name("Hot")
user_preferences_interface_page.save_changes
visit "/"
expect(page).to have_css(".navigation-container .hot.active", text: "Hot")
end
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 "/"
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 "/"
expect(page).to have_css(".new-home", text: "Hi friends!")
expect(page).to have_no_css(".list-container")
find("#sidebar-section-content-community li:first-child").click
expect(page).to have_css(".list-container")
click_logo
expect(page).to have_no_css(".list-container")
# ensure clicking on logo brings user back to the custom homepage
expect(page).to have_css(".new-home", text: "Hi friends!")
end
it "respects the user's homepage choice" do
visit "/"
expect(page).not_to have_css(".list-container")
expect(page).to have_css(".new-home", text: "Hi friends!")
sign_in user
visit ""
expect(page).to have_css(".new-home", text: "Hi friends!")
user_preferences_interface_page.visit(user)
homepage_picker = PageObjects::Components::SelectKit.new("#home-selector")
homepage_picker.expand
# user overrides theme custom homepage
homepage_picker.select_row_by_name("Hot")
user_preferences_interface_page.save_changes
expect(user.user_option.homepage_id).to eq(UserOption::HOMEPAGES.key("hot"))
click_logo
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
# user selects theme custom homepage again
homepage_picker.select_row_by_name("Default")
user_preferences_interface_page.save_changes
click_logo
expect(page).to have_current_path("/")
expect(page).to have_css(".new-home", text: "Hi friends!")
end
end
context "when default theme uses a custom_homepage modifier" do
before do
theme.theme_modifier_set.custom_homepage = true
theme.theme_modifier_set.save!
theme.set_default!
end
it "shows empty state to regular users" do
sign_in user
visit "/"
expect(page).to have_no_css(".list-container")
expect(page).to have_no_css(".alert-info")
end
it "shows empty state and notice to admins" do
sign_in admin
visit "/"
expect(page).to have_no_css(".list-container")
expect(page).to have_css(".alert-info")
end
context "when the theme adds content to the [custom-homepage] connector" do
let!(:basic_html_field) do
Fabricate(
:theme_field,
theme: theme,
type_id: ThemeField.types[:js],
target_id: Theme.targets[:extra_js],
name: "discourse/api-initializers/theme-initializer.gjs",
value: <<~GJS,
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer((api) => {
api.renderInOutlet(
"custom-homepage",
<template>
<div class="new-home">Hi friends!</div>
</template>
);
});
GJS
)
end
include_examples "a custom homepage"
end
context "when a theme component adds content to the [custom-homepage] connector" do
let!(:component) { Fabricate(:theme, component: true) }
let!(:component_html_field) do
Fabricate(
:theme_field,
theme: component,
type_id: ThemeField.types[:js],
target_id: Theme.targets[:extra_js],
name: "discourse/api-initializers/theme-initializer.gjs",
value: <<~GJS,
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer((api) => {
api.renderInOutlet(
"custom-homepage",
<template>
<div class="new-home">Hi friends!</div>
</template>
);
});
GJS
)
end
before { theme.add_relative_theme!(:child, component) }
include_examples "a custom homepage"
end
end
context "when a theme component uses the custom_homepage modifier" do
let!(:component) { Fabricate(:theme, component: true) }
let!(:component_html_field) do
Fabricate(
:theme_field,
theme: component,
type_id: ThemeField.types[:js],
target_id: Theme.targets[:extra_js],
name: "discourse/api-initializers/theme-initializer.gjs",
value: <<~GJS,
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer((api) => {
api.renderInOutlet(
"custom-homepage",
<template>
<div class="new-home">Hi friends!</div>
</template>
);
});
GJS
)
end
before do
component.theme_modifier_set.custom_homepage = true
component.theme_modifier_set.save!
theme.add_relative_theme!(:child, component)
theme.set_default!
end
include_examples "a custom homepage"
end
end