FIX: If you follow a link to a user that doesn't exist, show 404

This commit is contained in:
Robin Ward 2015-06-29 15:47:05 -04:00
parent e026dc16d8
commit 1b1ea08ac3
1 changed files with 22 additions and 18 deletions

View File

@ -1,22 +1,22 @@
var INDEX_STREAM_ROUTES = ["user.deletedPosts", "user.flaggedPosts", "userActivity.index"]; const INDEX_STREAM_ROUTES = ["user.deletedPosts", "user.flaggedPosts", "userActivity.index"];
export default Discourse.Route.extend({ export default Discourse.Route.extend({
titleToken: function() { titleToken() {
var model = this.modelFor('user'); const model = this.modelFor('user');
var username = model.get('username'); const username = model.get('username');
if (username) { if (username) {
return [I18n.t("user.profile"), username]; return [I18n.t("user.profile"), username];
} }
}, },
actions: { actions: {
logout: function() { logout() {
Discourse.logout(); Discourse.logout();
}, },
composePrivateMessage: function(user, post) { composePrivateMessage(user, post) {
var recipient = user ? user.get('username') : '', const recipient = user ? user.get('username') : '',
reply = post ? window.location.protocol + "//" + window.location.host + post.get("url") : null; reply = post ? window.location.protocol + "//" + window.location.host + post.get("url") : null;
return this.controllerFor('composer').open({ return this.controllerFor('composer').open({
@ -28,18 +28,18 @@ export default Discourse.Route.extend({
}); });
}, },
willTransition: function(transition) { willTransition(transition) {
// will reset the indexStream when transitioning to routes that aren't "indexStream" // will reset the indexStream when transitioning to routes that aren't "indexStream"
// otherwise the "header" will jump // otherwise the "header" will jump
var isIndexStream = ~INDEX_STREAM_ROUTES.indexOf(transition.targetName); const isIndexStream = ~INDEX_STREAM_ROUTES.indexOf(transition.targetName);
this.controllerFor('user').set('indexStream', isIndexStream); this.controllerFor('user').set('indexStream', isIndexStream);
return true; return true;
} }
}, },
model: function(params) { model(params) {
// If we're viewing the currently logged in user, return that object instead // If we're viewing the currently logged in user, return that object instead
var currentUser = Discourse.User.current(); const currentUser = this.currentUser;
if (currentUser && (params.username.toLowerCase() === currentUser.get('username_lower'))) { if (currentUser && (params.username.toLowerCase() === currentUser.get('username_lower'))) {
return currentUser; return currentUser;
} }
@ -47,34 +47,38 @@ export default Discourse.Route.extend({
return Discourse.User.create({username: params.username}); return Discourse.User.create({username: params.username});
}, },
afterModel: function() { afterModel() {
var user = this.modelFor('user'); const user = this.modelFor('user');
const self = this;
return user.findDetails().then(function() { return user.findDetails().then(function() {
return user.findStaffInfo(); return user.findStaffInfo();
}).catch(function() {
return self.replaceWith('/404');
}); });
}, },
serialize: function(model) { serialize(model) {
if (!model) return {}; if (!model) return {};
return { username: (Em.get(model, 'username') || '').toLowerCase() }; return { username: (Em.get(model, 'username') || '').toLowerCase() };
}, },
setupController: function(controller, user) { setupController(controller, user) {
controller.set('model', user); controller.set('model', user);
// Add a search context // Add a search context
this.controllerFor('search').set('searchContext', user.get('searchContext')); this.controllerFor('search').set('searchContext', user.get('searchContext'));
}, },
activate: function() { activate() {
this._super(); this._super();
var user = this.modelFor('user'); const user = this.modelFor('user');
this.messageBus.subscribe("/users/" + user.get('username_lower'), function(data) { this.messageBus.subscribe("/users/" + user.get('username_lower'), function(data) {
user.loadUserAction(data); user.loadUserAction(data);
}); });
}, },
deactivate: function() { deactivate() {
this._super(); this._super();
this.messageBus.unsubscribe("/users/" + this.modelFor('user').get('username_lower')); this.messageBus.unsubscribe("/users/" + this.modelFor('user').get('username_lower'));