diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index 405f70878..9470a9325 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -354,16 +354,18 @@ Discourse.User = Discourse.Model.extend({ @type {String} **/ homepage: function() { - // top is the default for: + // when there are enough topics, /top is the default for // - new users // - long-time-no-see user (ie. > 1 month) - if (Discourse.SiteSettings.top_menu.indexOf("top") >= 0) { - if (this.get("trust_level") === 0 || !this.get("hasBeenSeenInTheLastMonth")) { - return "top"; + if (Discourse.Site.currentProp("has_enough_topic_to_redirect_to_top_page")) { + if (Discourse.SiteSettings.top_menu.indexOf("top") >= 0) { + if (this.get("trust_level") === 0 || !this.get("hasBeenSeenInTheLastMonth")) { + return "top"; + } } } return Discourse.Utilities.defaultHomepage(); - }.property("trust_level", "hasBeenSeenInTheLastMonth"), + }.property("trust_level", "hasBeenSeenInTheLastMonth", "Discourse.Site.has_enough_topic_to_redirect_to_top_page"), updateMutedCategories: function() { this.set("mutedCategories", Discourse.Category.findByIds(this.muted_category_ids)); diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb index 0f2f1b699..914a8e765 100644 --- a/app/serializers/site_serializer.rb +++ b/app/serializers/site_serializer.rb @@ -9,7 +9,8 @@ class SiteSerializer < ApplicationSerializer :top_menu_items, :anonymous_top_menu_items, :uncategorized_category_id, # this is hidden so putting it here - :is_readonly + :is_readonly, + :has_enough_topic_to_redirect_to_top_page has_many :categories, serializer: BasicCategorySerializer, embed: :objects has_many :post_action_types, embed: :objects @@ -50,4 +51,12 @@ class SiteSerializer < ApplicationSerializer Discourse.readonly_mode? end + def has_enough_topic_to_redirect_to_top_page + Topic.listable_topics + .visible + .includes(:category).references(:category) + .where('COALESCE(categories.topic_id, 0) <> topics.id') + .count > SiteSetting.topics_per_period_in_top_page + end + end diff --git a/test/javascripts/models/user_test.js b/test/javascripts/models/user_test.js index 610b0729d..548cbe35c 100644 --- a/test/javascripts/models/user_test.js +++ b/test/javascripts/models/user_test.js @@ -40,12 +40,28 @@ test("homepage when top is disabled", function() { equal(oldUser.get("homepage"), defaultHomepage, "long-time-no-see old user's homepage is default when top is disabled"); }); -test("homepage when top is enabled", function() { +test("homepage when top is enabled and not enought topics", function() { var newUser = Discourse.User.create({ trust_level: 0, last_seen_at: moment() }), oldUser = Discourse.User.create({ trust_level: 1, last_seen_at: moment() }), defaultHomepage = Discourse.Utilities.defaultHomepage(); Discourse.SiteSettings.top_menu = "latest|top"; + Discourse.Site.currentProp("has_enough_topic_to_redirect_to_top_page", false); + + equal(newUser.get("homepage"), defaultHomepage, "new user's homepage is default"); + equal(oldUser.get("homepage"), defaultHomepage, "old user's homepage is default"); + + oldUser.set("last_seen_at", moment().subtract('month', 2)); + equal(oldUser.get("homepage"), defaultHomepage, "long-time-no-see old user's homepage is default"); +}); + +test("homepage when top is enabled and has enough topics", function() { + var newUser = Discourse.User.create({ trust_level: 0, last_seen_at: moment() }), + oldUser = Discourse.User.create({ trust_level: 1, last_seen_at: moment() }), + defaultHomepage = Discourse.Utilities.defaultHomepage(); + + Discourse.SiteSettings.top_menu = "latest|top"; + Discourse.Site.currentProp("has_enough_topic_to_redirect_to_top_page", true); equal(newUser.get("homepage"), "top", "new user's homepage is top when top is enabled"); equal(oldUser.get("homepage"), defaultHomepage, "old user's homepage is default when top is enabled");