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.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) {

@ -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 = {};

@ -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'),

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

@ -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) {

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

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

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

@ -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) {

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

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

@ -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);
},

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

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

Loading…
Cancel
Save