From 857d54162bdb01217ffa6340e328ee1838c5e1b1 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 2 Aug 2016 13:21:06 -0400 Subject: [PATCH] Redirect to Summary when viewing yourself --- .../initializers/url-redirects.js.es6 | 12 +++- .../javascripts/discourse/models/user.js.es6 | 2 +- .../discourse/routes/user-index.js.es6 | 11 +++- .../discourse/routes/user-summary.js.es6 | 2 +- .../discourse/templates/{user => }/user.hbs | 0 .../javascripts/discourse/views/user.js.es6 | 4 +- .../acceptance/user-anonymous-test.js.es6 | 56 ++++++++++--------- test/javascripts/acceptance/user-test.js.es6 | 18 ++++-- .../helpers/create-pretender.js.es6 | 12 ++++ 9 files changed, 78 insertions(+), 39 deletions(-) rename app/assets/javascripts/discourse/templates/{user => }/user.hbs (100%) diff --git a/app/assets/javascripts/discourse/initializers/url-redirects.js.es6 b/app/assets/javascripts/discourse/initializers/url-redirects.js.es6 index 102e95f97..4005acc77 100644 --- a/app/assets/javascripts/discourse/initializers/url-redirects.js.es6 +++ b/app/assets/javascripts/discourse/initializers/url-redirects.js.es6 @@ -2,12 +2,22 @@ import DiscourseURL from 'discourse/lib/url'; export default { name: 'url-redirects', - initialize: function() { + after: 'inject-objects', + + initialize(container) { + + const currentUser = container.lookup('current-user:main'); // URL rewrites (usually due to refactoring) DiscourseURL.rewrite(/^\/category\//, "/c/"); DiscourseURL.rewrite(/^\/group\//, "/groups/"); DiscourseURL.rewrite(/\/private-messages\/$/, "/messages/"); + + if (currentUser) { + const username = currentUser.get('username'); + DiscourseURL.rewrite(new RegExp(`^/users/${username}/?$`, "i"), `/users/${username}/summary`); + } + DiscourseURL.rewrite(/^\/users\/([^\/]+)\/?$/, "/users/$1/activity"); } }; diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index 3afe8b166..932454aa0 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -245,7 +245,7 @@ const User = RestModel.extend({ @computed("groups.[]") displayGroups() { - const groups = this.get('groups'); + const groups = this.get('groups') || []; const filtered = groups.filter(group => { return !group.automatic || group.name === "moderators"; }); diff --git a/app/assets/javascripts/discourse/routes/user-index.js.es6 b/app/assets/javascripts/discourse/routes/user-index.js.es6 index 1653ef0bc..130125000 100644 --- a/app/assets/javascripts/discourse/routes/user-index.js.es6 +++ b/app/assets/javascripts/discourse/routes/user-index.js.es6 @@ -1,13 +1,18 @@ export default Discourse.Route.extend({ - beforeModel: function() { + beforeModel() { + + const { currentUser } = this; + const viewingMe = (currentUser && currentUser.get('username') === this.modelFor('user').get('username')); + const destination = viewingMe ? 'user.summary' : 'userActivity'; + // HACK: Something with the way the user card intercepts clicks seems to break how the // transition into a user's activity works. This makes the back button work on mobile // where there is no user card as well as desktop where there is. if (this.site.mobileView) { - this.replaceWith('userActivity'); + this.replaceWith(destination); } else { - this.transitionTo('userActivity'); + this.transitionTo(destination); } } diff --git a/app/assets/javascripts/discourse/routes/user-summary.js.es6 b/app/assets/javascripts/discourse/routes/user-summary.js.es6 index 4b24ecc6a..12fe0c929 100644 --- a/app/assets/javascripts/discourse/routes/user-summary.js.es6 +++ b/app/assets/javascripts/discourse/routes/user-summary.js.es6 @@ -1,5 +1,5 @@ export default Discourse.Route.extend({ model() { - return this.modelFor("User").summary(); + return this.modelFor("user").summary(); } }); diff --git a/app/assets/javascripts/discourse/templates/user/user.hbs b/app/assets/javascripts/discourse/templates/user.hbs similarity index 100% rename from app/assets/javascripts/discourse/templates/user/user.hbs rename to app/assets/javascripts/discourse/templates/user.hbs diff --git a/app/assets/javascripts/discourse/views/user.js.es6 b/app/assets/javascripts/discourse/views/user.js.es6 index 2ae0d55a4..f0b0ceb85 100644 --- a/app/assets/javascripts/discourse/views/user.js.es6 +++ b/app/assets/javascripts/discourse/views/user.js.es6 @@ -1,5 +1,3 @@ import ScrollTop from 'discourse/mixins/scroll-top'; -export default Ember.View.extend(ScrollTop, { - templateName: 'user/user', -}); +export default Ember.View.extend(ScrollTop); diff --git a/test/javascripts/acceptance/user-anonymous-test.js.es6 b/test/javascripts/acceptance/user-anonymous-test.js.es6 index 2151606d4..10baa0107 100644 --- a/test/javascripts/acceptance/user-anonymous-test.js.es6 +++ b/test/javascripts/acceptance/user-anonymous-test.js.es6 @@ -8,29 +8,35 @@ export function hasStream() { }); } -// function hasTopicList() { -// andThen(() => { -// equal(count('.user-stream .item'), 0, "has no stream displayed"); -// ok(count('.topic-list tr') > 0, 'it has a topic list'); -// }); -// } +function hasTopicList() { + andThen(() => { + equal(count('.user-stream .item'), 0, "has no stream displayed"); + ok(count('.topic-list tr') > 0, 'it has a topic list'); + }); +} -// test("Filters", () => { -// visit("/users/eviltrout"); -// hasStream(); -// -// visit("/users/eviltrout/activity/topics"); -// hasTopicList(); -// -// visit("/users/eviltrout/activity/posts"); -// hasStream(); -// -// }); -// -// test("Restricted Routes", () => { -// visit("/users/eviltrout/preferences"); -// -// andThen(() => { -// equal(currentURL(), '/users/eviltrout/activity', "it redirects from preferences"); -// }); -// }); +test("Root URL", () => { + visit("/users/eviltrout"); + andThen(() => { + equal(currentPath(), 'user.userActivity.index', "it defaults to activity"); + }); +}); + +test("Filters", () => { + visit("/users/eviltrout/activity"); + hasStream(); + + visit("/users/eviltrout/activity/topics"); + hasTopicList(); + + visit("/users/eviltrout/activity/replies"); + hasStream(); +}); + +test("Restricted Routes", () => { + visit("/users/eviltrout/preferences"); + + andThen(() => { + equal(currentURL(), '/users/eviltrout/activity', "it redirects from preferences"); + }); +}); diff --git a/test/javascripts/acceptance/user-test.js.es6 b/test/javascripts/acceptance/user-test.js.es6 index c91acb7d5..a7e917268 100644 --- a/test/javascripts/acceptance/user-test.js.es6 +++ b/test/javascripts/acceptance/user-test.js.es6 @@ -1,9 +1,17 @@ import { acceptance } from "helpers/qunit-helpers"; -// import { hasStream } from 'acceptance/user-anonymous-test'; +import { hasStream } from 'acceptance/user-anonymous-test'; acceptance("User", {loggedIn: true}); -// test("Pending", () => { -// visit("/users/eviltrout/activity/pending"); -// hasStream(); -// }); +test("Pending", () => { + visit("/users/eviltrout/activity/pending"); + hasStream(); +}); + +test("Root URL - Viewing Self", () => { + visit("/users/eviltrout"); + andThen(() => { + equal(currentPath(), 'user.summary', "it defaults to summary"); + }); +}); + diff --git a/test/javascripts/helpers/create-pretender.js.es6 b/test/javascripts/helpers/create-pretender.js.es6 index faa28c26e..85d7c9371 100644 --- a/test/javascripts/helpers/create-pretender.js.es6 +++ b/test/javascripts/helpers/create-pretender.js.es6 @@ -64,6 +64,18 @@ export default function() { return response(json); }); + this.get('/users/eviltrout/summary.json', () => { + return response({ + user_summary: { + topics: [], + topic_ids: [], + replies: [], + links: [] + }, + topics: [], + }); + }); + this.get('/clicks/track', success); this.put('/users/eviltrout', () => response({ user: {} }));