From 05fdc945f309574e46f4ce3488ad139e6cfc202e Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 17 Aug 2014 00:14:45 -0400 Subject: [PATCH] closes #1993 --- src/posts.js | 15 +++++--- src/socket.io/posts.js | 15 ++------ src/socket.io/user.js | 1 + src/topics.js | 4 +-- src/topics/create.js | 2 +- src/topics/follow.js | 7 ++-- src/user/notifications.js | 76 ++++++++++++++++++++++++++++++++------- 7 files changed, 84 insertions(+), 36 deletions(-) diff --git a/src/posts.js b/src/posts.js index 15ab9c38ff..4f33a78f47 100644 --- a/src/posts.js +++ b/src/posts.js @@ -522,13 +522,20 @@ var async = require('async'), }); } - Posts.getPidIndex = function(pid, callback) { - Posts.getPostField(pid, 'tid', function(err, tid) { + Posts.getPidIndex = function(pid, uid, callback) { + async.parallel({ + settings: function(next) { + user.getSettings(uid, next); + }, + tid: function(next) { + Posts.getPostField(pid, 'tid', next); + } + }, function(err, results) { if(err) { return callback(err); } - - db.sortedSetRank('tid:' + tid + ':posts', pid, function(err, index) { + var set = results.settings.topicPostSort === 'most_votes' ? 'tid:' + results.tid + ':posts:votes' : 'tid:' + results.tid + ':posts'; + db.sortedSetRank(set, pid, function(err, index) { if (!utils.isNumber(index)) { return callback(err, 1); } diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 0a1f3def44..3315bf2f5a 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -110,8 +110,6 @@ SocketPosts.sendNotificationToPostOwner = function(pid, fromuid, notification) { async.parallel({ username: async.apply(user.getUserField, fromuid, 'username'), - slug: async.apply(topics.getTopicField, postData.tid, 'slug'), - index: async.apply(posts.getPidIndex, pid), postContent: function(next) { async.waterfall([ async.apply(posts.getPostField, pid, 'content'), @@ -128,7 +126,7 @@ SocketPosts.sendNotificationToPostOwner = function(pid, fromuid, notification) { notifications.create({ bodyShort: '[[' + notification + ', ' + results.username + ']]', bodyLong: results.postContent, - path: nconf.get('relative_path') + '/topic/' + results.slug + '/' + results.index, + pid: pid, uniqueId: 'post:' + pid + ':uid:' + fromuid, from: fromuid }, function(err, nid) { @@ -274,7 +272,6 @@ SocketPosts.flag = function(socket, pid, callback) { } var message = '', - path = '', post; async.waterfall([ @@ -293,21 +290,13 @@ SocketPosts.flag = function(socket, pid, callback) { }, function(postData, next) { post = postData; - topics.getTopicField(postData.tid, 'slug', next); - }, - function(topicSlug, next) { - path = nconf.get('relative_path') + '/topic/' + topicSlug; - posts.getPidIndex(pid, next); - }, - function(postIndex, next) { - path += '/' + postIndex; groups.get('administrators', {}, next); }, function(adminGroup, next) { notifications.create({ bodyShort: message, bodyLong: post.content, - path: path, + pid: pid, uniqueId: 'post_flag:' + pid, from: socket.uid }, function(err, nid) { diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 6b9766fea0..42adbb5981 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -206,6 +206,7 @@ function toggleFollow(method, uid, theiruid, callback) { fromUid: uid, toUid: theiruid }); + callback(); }); } diff --git a/src/topics.js b/src/topics.js index 8ef34f8975..5eeeab2a3c 100644 --- a/src/topics.js +++ b/src/topics.js @@ -386,7 +386,7 @@ var async = require('async'), }); }; - Topics.getTeaser = function(tid, callback) { + Topics.getTeaser = function(tid, uid, callback) { Topics.getLatestUndeletedPid(tid, function(err, pid) { if (err || !pid) { return callback(err); @@ -411,7 +411,7 @@ var async = require('async'), }); }, postIndex: function(next) { - posts.getPidIndex(pid, next); + posts.getPidIndex(pid, uid, next); } }, function(err, results) { if (err) { diff --git a/src/topics/create.js b/src/topics/create.js index 955d42d182..60c41880b1 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -226,7 +226,7 @@ module.exports = function(Topics) { if (settings.followTopicsOnReply) { threadTools.follow(postData.tid, uid); } - posts.getPidIndex(postData.pid, next); + posts.getPidIndex(postData.pid, uid, next); }, function(index, next) { postData.index = index - 1; diff --git a/src/topics/follow.js b/src/topics/follow.js index 76fb14cf6a..0ea48620be 100644 --- a/src/topics/follow.js +++ b/src/topics/follow.js @@ -37,9 +37,8 @@ module.exports = function(Topics) { } async.parallel({ - topicData: async.apply(Topics.getTopicFields, tid, ['title', 'slug']), + title: async.apply(Topics.getTopicField, tid, 'title'), username: async.apply(user.getUserField, exceptUid, 'username'), - postIndex: async.apply(posts.getPidIndex, pid), postContent: function(next) { async.waterfall([ async.apply(posts.getPostField, pid, 'content'), @@ -54,9 +53,9 @@ module.exports = function(Topics) { } notifications.create({ - bodyShort: '[[notifications:user_posted_to, ' + results.username + ', ' + results.topicData.title + ']]', + bodyShort: '[[notifications:user_posted_to, ' + results.username + ', ' + results.title + ']]', bodyLong: results.postContent, - path: nconf.get('relative_path') + '/topic/' + results.topicData.slug + '/' + results.postIndex, + pid: pid, uniqueId: 'topic:' + tid + ':uid:' + exceptUid, tid: tid, from: exceptUid diff --git a/src/user/notifications.js b/src/user/notifications.js index 38ece77940..7f4a833350 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -130,21 +130,74 @@ var async = require('async'), return callback(err); } - notifications = notifications.map(function(notification, index) { - if (!notification) { - return null; - } - notification.read = hasRead[index]; - notification.datetimeISO = utils.toISOString(notification.datetime); - notification.readClass = !notification.read ? 'label-warning' : ''; - return notification; + var pids = notifications.map(function(notification) { + return notification ? notification.pid : null; }); - callback(null, notifications); + generatePostPaths(pids, uid, function(err, paths) { + if (err) { + return callback(err); + } + + notifications = notifications.map(function(notification, index) { + if (!notification) { + return null; + } + + notification.read = hasRead[index]; + notification.path = paths[index] || notification.path || ''; + notification.datetimeISO = utils.toISOString(notification.datetime); + notification.readClass = !notification.read ? 'label-warning' : ''; + return notification; + }); + + callback(null, notifications); + }); }); }); }; + function generatePostPaths(pids, uid, callback) { + 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'], next); + } + }, function(err, results) { + if (err) { + return callback(err); + } + + var paths = []; + pids.forEach(function(pid, index) { + var slug = results.topics[index] ? results.topics[index].slug : null; + var postIndex = results.indices[index] ? parseInt(results.indices[index], 10) + 1 : null; + if (slug && postIndex) { + paths.push(nconf.get('relative_path') + '/topic/' + slug + '/' + postIndex); + } else { + paths.push(null); + } + }); + callback(null, paths); + }); + }); + } + UserNotifications.getDailyUnread = function(uid, callback) { var now = Date.now(), yesterday = now - (1000*60*60*24); // Approximate, can be more or less depending on time changes, makes no difference really. @@ -221,8 +274,7 @@ var async = require('async'), async.parallel({ username: async.apply(user.getUserField, uid, 'username'), - topic: async.apply(topics.getTopicFields, tid, ['slug', 'cid', 'title']), - postIndex: async.apply(posts.getPidIndex, pid), + topic: async.apply(topics.getTopicFields, tid, ['cid', 'title']), postContent: function(next) { async.waterfall([ async.apply(posts.getPostField, pid, 'content'), @@ -246,7 +298,7 @@ var async = require('async'), notifications.create({ bodyShort: '[[notifications:user_posted_to, ' + results.username + ', ' + results.topic.title + ']]', bodyLong: results.postContent, - path: nconf.get('relative_path') + '/topic/' + results.topic.slug + '/' + results.postIndex, + pid: pid, uniqueId: 'topic:' + tid + ':uid:' + uid, tid: tid, from: uid