From 54686388fbab15ac3936883310b31ed6fe16b404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 30 Dec 2013 19:42:45 +0100 Subject: [PATCH] FEATURE: display the Top tab by default for long-time-no-see users --- .../javascripts/discourse/models/user.js | 17 +++++++++----- test/javascripts/models/user_test.js | 22 ++++++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index c4141e5d4ff..1d622e0f39a 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -330,6 +330,10 @@ Discourse.User = Discourse.Model.extend({ }); }, + hasBeenSeenInTheLastMonth: function() { + return moment().diff(moment(this.get('last_seen_at')), 'month', true) < 1.0; + }.property("last_seen_at"), + /** Homepage of the user @@ -337,13 +341,16 @@ Discourse.User = Discourse.Model.extend({ @type {String} **/ homepage: function() { - // top is the default for new users - if (Discourse.SiteSettings.top_menu.indexOf("top") >= 0 && - this.get("trust_level") === 0) { - return "top"; + // 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"; + } } return Discourse.Utilities.defaultHomepage(); - }.property("trust_level") + }.property("trust_level", "hasBeenSeenInTheLastMonth") }); diff --git a/test/javascripts/models/user_test.js b/test/javascripts/models/user_test.js index d6620c0c9da..610b0729d34 100644 --- a/test/javascripts/models/user_test.js +++ b/test/javascripts/models/user_test.js @@ -27,25 +27,31 @@ test("isAllowedToUploadAFile", function() { }); test("homepage when top is disabled", function() { - var newUser = Discourse.User.create({ trust_level: 0 }), - oldUser = Discourse.User.create({ trust_level: 1 }), + 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"; - ok(newUser.get("homepage") === defaultHomepage, "new user's homepage is default when top is disabled"); - ok(oldUser.get("homepage") === defaultHomepage, "old user's homepage is default when top is disabled"); + equal(newUser.get("homepage"), defaultHomepage, "new user's homepage is default when top is disabled"); + equal(oldUser.get("homepage"), defaultHomepage, "old user's homepage is default when top is disabled"); + + oldUser.set("last_seen_at", moment().subtract('month', 2)); + 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() { - var newUser = Discourse.User.create({ trust_level: 0 }), - oldUser = Discourse.User.create({ trust_level: 1 }), + 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"; - ok(newUser.get("homepage") === "top", "new user's homepage is top when top is enabled"); - ok(oldUser.get("homepage") === defaultHomepage, "old user's homepage is default when top is enabled"); + 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"); + + oldUser.set("last_seen_at", moment().subtract('month', 2)); + equal(oldUser.get("homepage"), "top", "long-time-no-see old user's homepage is top when top is enabled"); });