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

change notifications to use new redirects
v1.18.x
barisusakli 9 years ago
parent b0747ad10e
commit eb0aea6390

@ -84,14 +84,7 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound,
payload.message = notifData.bodyShort; payload.message = notifData.bodyShort;
payload.type = 'info'; payload.type = 'info';
payload.clickfn = function() { payload.clickfn = function() {
socket.emit('notifications.generatePath', notifData.nid, function(err, path) { window.location.href = config.relative_path + '/' + notifData.path;
if (err) {
return app.alertError(err.message);
}
if (path) {
ajaxify.go(path);
}
});
}; };
} else { } else {
payload.message = '[[notifications:you_have_unread_notifications]]'; payload.message = '[[notifications:you_have_unread_notifications]]';

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var nconf = require('nconf'), var nconf = require('nconf');
async = require('async'), var async = require('async');
validator = require('validator'), var validator = require('validator');
translator = require('../../public/src/modules/translator'), var translator = require('../../public/src/modules/translator');
categories = require('../categories'), var categories = require('../categories');
plugins = require('../plugins'), var plugins = require('../plugins');
meta = require('../meta'); var meta = require('../meta');
var helpers = {}; var helpers = {};

@ -12,6 +12,7 @@ var helpers = require('./helpers');
var Controllers = { var Controllers = {
topics: require('./topics'), topics: require('./topics'),
posts: require('./posts'),
categories: require('./categories'), categories: require('./categories'),
category: require('./category'), category: require('./category'),
unread: require('./unread'), unread: require('./unread'),

@ -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;

@ -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) { middleware.isAdmin = function(req, res, next) {
if (!req.uid) { if (!req.uid) {
return controllers.helpers.notAllowed(req, res); return controllers.helpers.notAllowed(req, res);

@ -12,6 +12,7 @@ var User = require('./user');
var groups = require('./groups'); var groups = require('./groups');
var meta = require('./meta'); var meta = require('./meta');
var plugins = require('./plugins'); var plugins = require('./plugins');
var utils = require('../public/src/utils');
(function(Notifications) { (function(Notifications) {
@ -45,6 +46,8 @@ var plugins = require('./plugins');
return next(null, null); return next(null, null);
} }
notification.datetimeISO = utils.toISOString(notification.datetime);
if (notification.bodyLong) { if (notification.bodyLong) {
notification.bodyLong = S(notification.bodyLong).escapeHTML().s; notification.bodyLong = S(notification.bodyLong).escapeHTML().s;
} }
@ -85,7 +88,7 @@ var plugins = require('./plugins');
// Removes nids that have been pruned // Removes nids that have been pruned
db.isSortedSetMembers('notifications', nids, function(err, exists) { db.isSortedSetMembers('notifications', nids, function(err, exists) {
if (err) { if (err) {
return callbacK(err); return callback(err);
} }
nids = nids.filter(function(notifId, idx) { nids = nids.filter(function(notifId, idx) {

@ -1,8 +1,10 @@
'use strict'; 'use strict';
var async = require('async'), var async = require('async');
topics = require('../topics');
var topics = require('../topics');
var utils = require('../../public/src/utils');
module.exports = function(Posts) { module.exports = function(Posts) {
@ -42,4 +44,49 @@ module.exports = function(Posts) {
], callback); ], 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);
};
}; };

@ -4,8 +4,8 @@ var helpers = require('./helpers');
var setupPageRoute = helpers.setupPageRoute; var setupPageRoute = helpers.setupPageRoute;
module.exports = function (app, middleware, controllers) { module.exports = function (app, middleware, controllers) {
var middlewares = [middleware.checkGlobalPrivacySettings]; var middlewares = [middleware.checkGlobalPrivacySettings, middleware.redirectUidToUserslug];
var accountMiddlewares = [middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions]; var accountMiddlewares = [middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, middleware.redirectUidToUserslug];
setupPageRoute(app, '/user/:userslug', middleware, middlewares, controllers.accounts.profile.get); setupPageRoute(app, '/user/:userslug', middleware, middlewares, controllers.accounts.profile.get);
setupPageRoute(app, '/user/:userslug/following', middleware, middlewares, controllers.accounts.follow.getFollowing); setupPageRoute(app, '/user/:userslug/following', middleware, middlewares, controllers.accounts.follow.getFollowing);

@ -1,23 +1,23 @@
"use strict"; "use strict";
var nconf = require('nconf'), var nconf = require('nconf');
path = require('path'), var path = require('path');
async = require('async'), var async = require('async');
winston = require('winston'), var winston = require('winston');
controllers = require('../controllers'), var controllers = require('../controllers');
plugins = require('../plugins'), var plugins = require('../plugins');
express = require('express'), var express = require('express');
validator = require('validator'), var validator = require('validator');
accountRoutes = require('./accounts'), var accountRoutes = require('./accounts');
metaRoutes = require('./meta'), var metaRoutes = require('./meta');
apiRoutes = require('./api'), var apiRoutes = require('./api');
adminRoutes = require('./admin'), var adminRoutes = require('./admin');
feedRoutes = require('./feeds'), var feedRoutes = require('./feeds');
pluginRoutes = require('./plugins'), var pluginRoutes = require('./plugins');
authRoutes = require('./authentication'), var authRoutes = require('./authentication');
helpers = require('./helpers'); var helpers = require('./helpers');
var setupPageRoute = helpers.setupPageRoute; var setupPageRoute = helpers.setupPageRoute;
@ -46,6 +46,10 @@ function topicRoutes(app, middleware, controllers) {
setupPageRoute(app, '/topic/:topic_id/:slug?', middleware, [], controllers.topics.get); 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) { function tagRoutes(app, middleware, controllers) {
setupPageRoute(app, '/tags/:tag', middleware, [middleware.privateTagListing], controllers.tags.getTag); setupPageRoute(app, '/tags/:tag', middleware, [middleware.privateTagListing], controllers.tags.getTag);
setupPageRoute(app, '/tags', middleware, [middleware.privateTagListing], controllers.tags.getTags); 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); setupPageRoute(app, '/users/banned', middleware, middlewares, controllers.users.getBannedUsers);
} }
function groupRoutes(app, middleware, controllers) { function groupRoutes(app, middleware, controllers) {
var middlewares = [middleware.checkGlobalPrivacySettings, middleware.exposeGroupName]; var middlewares = [middleware.checkGlobalPrivacySettings, middleware.exposeGroupName];
@ -124,6 +127,7 @@ module.exports = function(app, middleware, hotswapIds) {
mainRoutes(router, middleware, controllers); mainRoutes(router, middleware, controllers);
topicRoutes(router, middleware, controllers); topicRoutes(router, middleware, controllers);
postRoutes(router, middleware, controllers);
globalModRoutes(router, middleware, controllers); globalModRoutes(router, middleware, controllers);
tagRoutes(router, middleware, controllers); tagRoutes(router, middleware, controllers);
categoryRoutes(router, middleware, controllers); categoryRoutes(router, middleware, controllers);

@ -78,6 +78,7 @@ SocketHelpers.sendNotificationToPostOwner = function(pid, fromuid, notification)
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]', bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
bodyLong: results.postObj.content, bodyLong: results.postObj.content,
pid: pid, pid: pid,
path: '/post/' + pid,
nid: 'post:' + pid + ':uid:' + fromuid, nid: 'post:' + pid + ':uid:' + fromuid,
from: fromuid, from: fromuid,
mergeId: notification + '|' + pid, mergeId: notification + '|' + pid,
@ -110,7 +111,7 @@ SocketHelpers.sendNotificationToTopicOwner = function(tid, fromuid, notification
notifications.create({ notifications.create({
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]', bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
path: nconf.get('relative_path') + '/topic/' + results.topicData.slug, path: '/topic/' + results.topicData.slug,
nid: 'tid:' + tid + ':uid:' + fromuid, nid: 'tid:' + tid + ':uid:' + fromuid,
from: fromuid from: fromuid
}, function(err, notification) { }, function(err, notification) {

@ -56,28 +56,4 @@ SocketNotifs.markAllRead = function(socket, data, callback) {
notifications.markAllRead(socket.uid, 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; module.exports = SocketNotifs;

@ -90,6 +90,7 @@ module.exports = function(SocketPosts) {
bodyShort: '[[notifications:user_flagged_post_in, ' + flaggingUser.username + ', ' + titleEscaped + ']]', bodyShort: '[[notifications:user_flagged_post_in, ' + flaggingUser.username + ', ' + titleEscaped + ']]',
bodyLong: post.content, bodyLong: post.content,
pid: data.pid, pid: data.pid,
path: '/post/' + pid,
nid: 'post_flag:' + data.pid + ':uid:' + socket.uid, nid: 'post_flag:' + data.pid + ':uid:' + socket.uid,
from: socket.uid, from: socket.uid,
mergeId: 'notifications:user_flagged_post_in|' + data.pid, mergeId: 'notifications:user_flagged_post_in|' + data.pid,

@ -155,7 +155,7 @@ SocketUser.follow = function(socket, data, callback) {
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]', bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
nid: 'follow:' + data.uid + ':uid:' + socket.uid, nid: 'follow:' + data.uid + ':uid:' + socket.uid,
from: socket.uid, from: socket.uid,
path: '/user/' + userData.userslug, path: '/user/' + socket.uid,
mergeId: 'notifications:user_started_following_you' mergeId: 'notifications:user_started_following_you'
}, next); }, next);
}, },

@ -141,6 +141,7 @@ module.exports = function(Topics) {
bodyShort: '[[notifications:user_posted_to, ' + postData.user.username + ', ' + titleEscaped + ']]', bodyShort: '[[notifications:user_posted_to, ' + postData.user.username + ', ' + titleEscaped + ']]',
bodyLong: postData.content, bodyLong: postData.content,
pid: postData.pid, pid: postData.pid,
path: '/post/' + postData.pid,
nid: 'new_post:tid:' + postData.topic.tid + ':pid:' + postData.pid + ':uid:' + exceptUid, nid: 'new_post:tid:' + postData.topic.tid + ':pid:' + postData.pid + ':uid:' + exceptUid,
tid: postData.topic.tid, tid: postData.topic.tid,
from: exceptUid, from: exceptUid,

@ -1,19 +1,14 @@
'use strict'; 'use strict';
var async = require('async'), var async = require('async');
nconf = require('nconf'), var winston = require('winston');
winston = require('winston'), var S = require('string');
S = require('string'),
var db = require('../database');
user = require('../user'), var meta = require('../meta');
db = require('../database'), var notifications = require('../notifications');
meta = require('../meta'), var privileges = require('../privileges');
notifications = require('../notifications'),
posts = require('../posts'),
topics = require('../topics'),
privileges = require('../privileges'),
utils = require('../../public/src/utils');
(function(UserNotifications) { (function(UserNotifications) {
@ -103,89 +98,13 @@ var async = require('async'),
if (err) { if (err) {
return callback(err); return callback(err);
} }
notifications = notifications.filter(function(notification) {
UserNotifications.generateNotificationPaths(notifications, uid, callback); return notification && notification.path;
});
};
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;
}); });
callback(null, notifications); 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) { 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. 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 + ']]', bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]',
bodyLong: postData.content, bodyLong: postData.content,
pid: postData.pid, pid: postData.pid,
path: '/post/' + postData.pid,
nid: 'tid:' + postData.tid + ':uid:' + uid, nid: 'tid:' + postData.tid + ':uid:' + uid,
tid: postData.tid, tid: postData.tid,
from: uid from: uid

Loading…
Cancel
Save