From 63d88957a8a6e5278bbfc1d09e08f9c23a388d9b Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 30 Apr 2014 17:42:50 -0400 Subject: [PATCH] replace self links client side --- public/src/app.js | 13 +++++++++++++ src/middleware/middleware.js | 14 -------------- src/routes/index.js | 2 -- src/socket.io/index.js | 7 ++++--- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 3c693543cf..79dce1df9a 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -90,6 +90,7 @@ var socket, socket.on('event:connect', function (data) { app.username = data.username; + app.userslug = data.userslug; app.uid = data.uid; app.isAdmin = data.isAdmin; @@ -285,6 +286,16 @@ var socket, }); }; + app.replaceSelfLinks = function(selector) { + selector = selector || $('a'); + selector.each(function() { + var href = $(this).attr('href') + if (href && app.userslug) { + $(this).attr('href', href.replace(/\[self\]/g, app.userslug)); + } + }); + }; + app.processPage = function () { app.populateOnlineUsers(); @@ -299,6 +310,8 @@ var socket, app.createStatusTooltips(); + app.replaceSelfLinks(); + setTimeout(function () { window.scrollTo(0, 1); // rehide address bar on mobile after page load completes. }, 100); diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index a06c2209bb..53ee5e3117 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -52,20 +52,6 @@ middleware.redirectToAccountIfLoggedIn = function(req, res, next) { } }; -middleware.redirectToSelf = function(req, res, next) { - if (req.user && req.params.userslug === '[self]') { - user.getUserField(req.user.uid, 'userslug', function (err, userslug) { - if(res.locals.isAPI) { - res.json(302, '/user/' + userslug + (req.params.section ? '/' + req.params.section : '')); - } else { - res.redirect('/user/' + userslug + (req.params.section ? '/' + req.params.section : '')); - } - }); - } else { - next(); - } -}; - middleware.addSlug = function(req, res, next) { function redirect(method, id, name) { method(id, 'slug', function(err, slug) { diff --git a/src/routes/index.js b/src/routes/index.js index 0c494a6562..ad48f0f580 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -73,8 +73,6 @@ function categoryRoutes(app, middleware, controllers) { } function accountRoutes(app, middleware, controllers) { - app.get('/user/:userslug/:section?', middleware.redirectToSelf); - app.get('/api/user/:userslug/:section?', middleware.redirectToSelf); app.get('/user/:userslug', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.accounts.getAccount); app.get('/api/user/:userslug', middleware.checkGlobalPrivacySettings, controllers.accounts.getAccount); diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 2446a58a5f..b9abe1de6c 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -79,8 +79,8 @@ Sockets.init = function(server) { socket.join('uid_' + uid); async.parallel({ - username: function(next) { - user.getUserField(uid, 'username', next); + user: function(next) { + user.getUserFields(uid, ['username', 'userslug'], next); }, isAdmin: function(next) { user.isAdministrator(uid, next); @@ -88,7 +88,8 @@ Sockets.init = function(server) { }, function(err, userData) { socket.emit('event:connect', { status: 1, - username: userData.username, + username: userData.user.username, + userslug: userData.user.userslug, isAdmin: userData.isAdmin, uid: uid });