Skip to content

Node.js based forum software built for the modern web

add /user/<uid> and /post/<pid> redirects

barisusaklieb0aea6390cd
diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js
index 35f1e677dd..f98074ac5b 100644
--- a/public/src/modules/notifications.js
+++ b/public/src/modules/notifications.js
@@ -84,14 +84,7 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound,
payload.message = notifData.bodyShort;
payload.type = 'info';
payload.clickfn = function() {
- socket.emit('notifications.generatePath', notifData.nid, function(err, path) {
- if (err) {
- return app.alertError(err.message);
- }
- if (path) {
- ajaxify.go(path);
- }
- });
+ window.location.href = config.relative_path + '/' + notifData.path;
};
} else {
payload.message = '[[notifications:you_have_unread_notifications]]';
@@ -104,13 +97,13 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound,
if (ajaxify.currentPage === 'notifications') {
ajaxify.refresh();
}
-
+
if (!unreadNotifs[notifData.nid]) {
incrementNotifCount(1);
sound.play('notification');
- unreadNotifs[notifData.nid] = true;
- }
+ unreadNotifs[notifData.nid] = true;
+ }
});
socket.on('event:notifications.updateCount', function(count) {
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index a558d665dd..8032ed7a0e 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -1,13 +1,13 @@
'use strict';
-var nconf = require('nconf'),
- async = require('async'),
- validator = require('validator'),
-
- translator = require('../../public/src/modules/translator'),
- categories = require('../categories'),
- plugins = require('../plugins'),
- meta = require('../meta');
+var nconf = require('nconf');
+var async = require('async');
+var validator = require('validator');
+
+var translator = require('../../public/src/modules/translator');
+var categories = require('../categories');
+var plugins = require('../plugins');
+var meta = require('../meta');
var helpers = {};
diff --git a/src/controllers/index.js b/src/controllers/index.js
index 0a3bf6f67f..76d86cee31 100644
--- a/src/controllers/index.js
+++ b/src/controllers/index.js
@@ -12,6 +12,7 @@ var helpers = require('./helpers');
var Controllers = {
topics: require('./topics'),
+ posts: require('./posts'),
categories: require('./categories'),
category: require('./category'),
unread: require('./unread'),
diff --git a/src/controllers/posts.js b/src/controllers/posts.js
new file mode 100644
index 0000000000..c8b490f65b
--- /dev/null
+++ b/src/controllers/posts.js
@@ -0,0 +1,24 @@
+"use strict";
+
+var posts = require('../posts');
+var helpers = require('./helpers');
+
+var postsController = {};
+
+postsController.redirectToPost = function(req, res, callback) {
+ var pid = parseInt(req.params.pid, 10);
+ if (!pid) {
+ return callback();
+ }
+
+ posts.generatePostPath(pid, req.uid, function(err, path) {
+ if (err) {
+ return callback(err);
+ }
+
+ helpers.redirect(res, path);
+ });
+};
+
+
+module.exports = postsController;
diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js
index 184ea28a78..1dd5eee7bc 100644
--- a/src/middleware/middleware.js
+++ b/src/middleware/middleware.js
@@ -165,6 +165,21 @@ middleware.checkAccountPermissions = function(req, res, next) {
});
};
+middleware.redirectUidToUserslug = function(req, res, next) {
+ var uid = parseInt(req.params.userslug, 10);
+ if (!uid) {
+ return next();
+ }
+ user.getUserField(uid, 'userslug', function(err, userslug) {
+ if (err || !userslug) {
+ return next(err);
+ }
+
+ var path = req.path.replace(/^\/api/, '').replace(uid, function() { return userslug; });
+ controllers.helpers.redirect(res, path);
+ });
+};
+
middleware.isAdmin = function(req, res, next) {
if (!req.uid) {
return controllers.helpers.notAllowed(req, res);
diff --git a/src/notifications.js b/src/notifications.js
index 8a6b41d303..4f6d73f385 100644
--- a/src/notifications.js
+++ b/src/notifications.js
@@ -12,6 +12,7 @@ var User = require('./user');
var groups = require('./groups');
var meta = require('./meta');
var plugins = require('./plugins');
+var utils = require('../public/src/utils');
(function(Notifications) {
@@ -45,6 +46,8 @@ var plugins = require('./plugins');
return next(null, null);
}
+ notification.datetimeISO = utils.toISOString(notification.datetime);
+
if (notification.bodyLong) {
notification.bodyLong = S(notification.bodyLong).escapeHTML().s;
}
@@ -85,7 +88,7 @@ var plugins = require('./plugins');
// Removes nids that have been pruned
db.isSortedSetMembers('notifications', nids, function(err, exists) {
if (err) {
- return callbacK(err);
+ return callback(err);
}
nids = nids.filter(function(notifId, idx) {
diff --git a/src/posts/topics.js b/src/posts/topics.js
index a9946fdace..7ffd9592ce 100644
--- a/src/posts/topics.js
+++ b/src/posts/topics.js
@@ -1,8 +1,10 @@
'use strict';
-var async = require('async'),
- topics = require('../topics');
+var async = require('async');
+
+var topics = require('../topics');
+var utils = require('../../public/src/utils');
module.exports = function(Posts) {
@@ -42,4 +44,49 @@ module.exports = function(Posts) {
], callback);
};
+ Posts.generatePostPath = function (pid, uid, callback) {
+ Posts.generatePostPaths([pid], uid, function(err, paths) {
+ callback(err, Array.isArray(paths) && paths.length ? paths[0] : null);
+ });
+ };
+
+ Posts.generatePostPaths = function (pids, uid, callback) {
+ async.waterfall([
+ function (next) {
+ Posts.getPostsFields(pids, ['pid', 'tid'], next);
+ },
+ function (postData, next) {
+ async.parallel({
+ indices: function(next) {
+ Posts.getPostIndices(postData, uid, next);
+ },
+ topics: function(next) {
+ var tids = postData.map(function(post) {
+ return post ? post.tid : null;
+ });
+
+ topics.getTopicsFields(tids, ['slug', 'deleted'], next);
+ }
+ }, next);
+ },
+ function (results, next) {
+ var paths = pids.map(function(pid, index) {
+ if (parseInt(results.topics[index].deleted, 10) === 1) {
+ return null;
+ }
+
+ var slug = results.topics[index] ? results.topics[index].slug : null;
+ var postIndex = utils.isNumber(results.indices[index]) ? parseInt(results.indices[index], 10) + 1 : null;
+
+ if (slug && postIndex) {
+ return '/topic/' + slug + '/' + postIndex;
+ }
+ return null;
+ });
+
+ next(null, paths);
+ }
+ ], callback);
+ };
+
};
\ No newline at end of file
diff --git a/src/routes/accounts.js b/src/routes/accounts.js
index 8c7a505d0b..48d23b0ce0 100644
--- a/src/routes/accounts.js
+++ b/src/routes/accounts.js
@@ -4,8 +4,8 @@ var helpers = require('./helpers');
var setupPageRoute = helpers.setupPageRoute;
module.exports = function (app, middleware, controllers) {
- var middlewares = [middleware.checkGlobalPrivacySettings];
- var accountMiddlewares = [middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions];
+ var middlewares = [middleware.checkGlobalPrivacySettings, middleware.redirectUidToUserslug];
+ var accountMiddlewares = [middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, middleware.redirectUidToUserslug];
setupPageRoute(app, '/user/:userslug', middleware, middlewares, controllers.accounts.profile.get);
setupPageRoute(app, '/user/:userslug/following', middleware, middlewares, controllers.accounts.follow.getFollowing);
diff --git a/src/routes/index.js b/src/routes/index.js
index 8efdc1f2a7..86198b56dd 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -1,23 +1,23 @@
"use strict";
-var nconf = require('nconf'),
- path = require('path'),
- async = require('async'),
- winston = require('winston'),
- controllers = require('../controllers'),
- plugins = require('../plugins'),
- express = require('express'),
- validator = require('validator'),
-
- accountRoutes = require('./accounts'),
-
- metaRoutes = require('./meta'),
- apiRoutes = require('./api'),
- adminRoutes = require('./admin'),
- feedRoutes = require('./feeds'),
- pluginRoutes = require('./plugins'),
- authRoutes = require('./authentication'),
- helpers = require('./helpers');
+var nconf = require('nconf');
+var path = require('path');
+var async = require('async');
+var winston = require('winston');
+var controllers = require('../controllers');
+var plugins = require('../plugins');
+var express = require('express');
+var validator = require('validator');
+
+var accountRoutes = require('./accounts');
+
+var metaRoutes = require('./meta');
+var apiRoutes = require('./api');
+var adminRoutes = require('./admin');
+var feedRoutes = require('./feeds');
+var pluginRoutes = require('./plugins');
+var authRoutes = require('./authentication');
+var helpers = require('./helpers');
var setupPageRoute = helpers.setupPageRoute;
@@ -46,6 +46,10 @@ function topicRoutes(app, middleware, controllers) {
setupPageRoute(app, '/topic/:topic_id/:slug?', middleware, [], controllers.topics.get);
}
+function postRoutes(app, middleware, controllers) {
+ setupPageRoute(app, '/post/:pid', middleware, [], controllers.posts.redirectToPost);
+}
+
function tagRoutes(app, middleware, controllers) {
setupPageRoute(app, '/tags/:tag', middleware, [middleware.privateTagListing], controllers.tags.getTag);
setupPageRoute(app, '/tags', middleware, [middleware.privateTagListing], controllers.tags.getTags);
@@ -71,7 +75,6 @@ function userRoutes(app, middleware, controllers) {
setupPageRoute(app, '/users/banned', middleware, middlewares, controllers.users.getBannedUsers);
}
-
function groupRoutes(app, middleware, controllers) {
var middlewares = [middleware.checkGlobalPrivacySettings, middleware.exposeGroupName];
@@ -124,6 +127,7 @@ module.exports = function(app, middleware, hotswapIds) {
mainRoutes(router, middleware, controllers);
topicRoutes(router, middleware, controllers);
+ postRoutes(router, middleware, controllers);
globalModRoutes(router, middleware, controllers);
tagRoutes(router, middleware, controllers);
categoryRoutes(router, middleware, controllers);
diff --git a/src/socket.io/helpers.js b/src/socket.io/helpers.js
index e4621811ca..b39bf7360a 100644
--- a/src/socket.io/helpers.js
+++ b/src/socket.io/helpers.js
@@ -78,6 +78,7 @@ SocketHelpers.sendNotificationToPostOwner = function(pid, fromuid, notification)
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
bodyLong: results.postObj.content,
pid: pid,
+ path: '/post/' + pid,
nid: 'post:' + pid + ':uid:' + fromuid,
from: fromuid,
mergeId: notification + '|' + pid,
@@ -110,7 +111,7 @@ SocketHelpers.sendNotificationToTopicOwner = function(tid, fromuid, notification
notifications.create({
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
- path: nconf.get('relative_path') + '/topic/' + results.topicData.slug,
+ path: '/topic/' + results.topicData.slug,
nid: 'tid:' + tid + ':uid:' + fromuid,
from: fromuid
}, function(err, notification) {
diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.js
index 121ede2a96..db6753bc40 100644
--- a/src/socket.io/notifications.js
+++ b/src/socket.io/notifications.js
@@ -56,28 +56,4 @@ SocketNotifs.markAllRead = function(socket, data, callback) {
notifications.markAllRead(socket.uid, callback);
};
-SocketNotifs.generatePath = function(socket, nid, callback) {
- if (!socket.uid) {
- return callback(new Error('[[error:no-privileges]]'));;
- }
- async.waterfall([
- function (next) {
- notifications.get(nid, next);
- },
- function (notification, next) {
- if (!notification) {
- return next(null, '');
- }
- user.notifications.generateNotificationPaths([notification], socket.uid, next);
- },
- function (notificationsData, next) {
- if (notificationsData && notificationsData.length) {
- next(null, notificationsData[0].path);
- } else {
- next();
- }
- }
- ], callback);
-};
-
module.exports = SocketNotifs;
diff --git a/src/socket.io/posts/flag.js b/src/socket.io/posts/flag.js
index bbf7a4721c..e9d378d2be 100644
--- a/src/socket.io/posts/flag.js
+++ b/src/socket.io/posts/flag.js
@@ -90,6 +90,7 @@ module.exports = function(SocketPosts) {
bodyShort: '[[notifications:user_flagged_post_in, ' + flaggingUser.username + ', ' + titleEscaped + ']]',
bodyLong: post.content,
pid: data.pid,
+ path: '/post/' + pid,
nid: 'post_flag:' + data.pid + ':uid:' + socket.uid,
from: socket.uid,
mergeId: 'notifications:user_flagged_post_in|' + data.pid,
diff --git a/src/socket.io/user.js b/src/socket.io/user.js
index 75ecd949fd..59672c006b 100644
--- a/src/socket.io/user.js
+++ b/src/socket.io/user.js
@@ -155,7 +155,7 @@ SocketUser.follow = function(socket, data, callback) {
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
nid: 'follow:' + data.uid + ':uid:' + socket.uid,
from: socket.uid,
- path: '/user/' + userData.userslug,
+ path: '/user/' + socket.uid,
mergeId: 'notifications:user_started_following_you'
}, next);
},
diff --git a/src/topics/follow.js b/src/topics/follow.js
index f6f1da7dc5..90708ba50a 100644
--- a/src/topics/follow.js
+++ b/src/topics/follow.js
@@ -141,6 +141,7 @@ module.exports = function(Topics) {
bodyShort: '[[notifications:user_posted_to, ' + postData.user.username + ', ' + titleEscaped + ']]',
bodyLong: postData.content,
pid: postData.pid,
+ path: '/post/' + postData.pid,
nid: 'new_post:tid:' + postData.topic.tid + ':pid:' + postData.pid + ':uid:' + exceptUid,
tid: postData.topic.tid,
from: exceptUid,
diff --git a/src/user/notifications.js b/src/user/notifications.js
index 2f766a72de..852f9f0924 100644
--- a/src/user/notifications.js
+++ b/src/user/notifications.js
@@ -1,19 +1,14 @@
'use strict';
-var async = require('async'),
- nconf = require('nconf'),
- winston = require('winston'),
- S = require('string'),
-
- user = require('../user'),
- db = require('../database'),
- meta = require('../meta'),
- notifications = require('../notifications'),
- posts = require('../posts'),
- topics = require('../topics'),
- privileges = require('../privileges'),
- utils = require('../../public/src/utils');
+var async = require('async');
+var winston = require('winston');
+var S = require('string');
+
+var db = require('../database');
+var meta = require('../meta');
+var notifications = require('../notifications');
+var privileges = require('../privileges');
(function(UserNotifications) {
@@ -103,89 +98,13 @@ var async = require('async'),
if (err) {
return callback(err);
}
-
- UserNotifications.generateNotificationPaths(notifications, uid, callback);
- });
- };
-
- UserNotifications.generateNotificationPaths = function (notifications, uid, callback) {
- var pids = notifications.map(function(notification) {
- return notification ? notification.pid : null;
- });
-
- generatePostPaths(pids, uid, function(err, pidToPaths) {
- if (err) {
- return callback(err);
- }
-
- notifications = notifications.map(function(notification, index) {
- if (!notification) {
- return null;
- }
-
- notification.path = pidToPaths[notification.pid] || notification.path || null;
-
- if (notification.nid.startsWith('follow')) {
- notification.path = '/user/' + notification.user.userslug;
- }
-
- notification.datetimeISO = utils.toISOString(notification.datetime);
- return notification;
- }).filter(function(notification) {
- // Remove notifications that do not resolve to a path
- return notification && notification.path !== null;
+ notifications = notifications.filter(function(notification) {
+ return notification && notification.path;
});
-
callback(null, notifications);
});
};
- function generatePostPaths(pids, uid, callback) {
- pids = pids.filter(Boolean);
- var postKeys = pids.map(function(pid) {
- return 'post:' + pid;
- });
-
- db.getObjectsFields(postKeys, ['pid', 'tid'], function(err, postData) {
- if (err) {
- return callback(err);
- }
-
- var topicKeys = postData.map(function(post) {
- return post ? 'topic:' + post.tid : null;
- });
-
- async.parallel({
- indices: function(next) {
- posts.getPostIndices(postData, uid, next);
- },
- topics: function(next) {
- db.getObjectsFields(topicKeys, ['slug', 'deleted'], next);
- }
- }, function(err, results) {
- if (err) {
- return callback(err);
- }
-
- var pidToPaths = {};
- pids.forEach(function(pid, index) {
- if (parseInt(results.topics[index].deleted, 10) === 1) {
- pidToPaths[pid] = null;
- return;
- }
-
- var slug = results.topics[index] ? results.topics[index].slug : null;
- var postIndex = utils.isNumber(results.indices[index]) ? parseInt(results.indices[index], 10) + 1 : null;
-
- if (slug && postIndex) {
- pidToPaths[pid] = '/topic/' + slug + '/' + postIndex;
- }
- });
-
- callback(null, pidToPaths);
- });
- });
- }
UserNotifications.getDailyUnread = function(uid, callback) {
var yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really.
@@ -300,6 +219,7 @@ var async = require('async'),
bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]',
bodyLong: postData.content,
pid: postData.pid,
+ path: '/post/' + postData.pid,
nid: 'tid:' + postData.tid + ':uid:' + uid,
tid: postData.tid,
from: uid