diff --git a/src/categories.js b/src/categories.js index bfdd51435f..ac65ae2405 100644 --- a/src/categories.js +++ b/src/categories.js @@ -400,4 +400,25 @@ var db = require('./database.js'), db.setRemove('cid:' + cid + ':active_users', uid); }; + Categories.onNewPostMade = function(uid, tid, pid, timestamp) { + topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { + + var cid = topicData.cid; + + db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid); + + if(parseInt(topicData.pinned, 10) === 0) { + db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid); + } + + db.setCount('cid:' + cid + ':active_users', function(err, amount) { + if (amount > 15) { + db.setRemoveRandom('cid:' + cid + ':active_users'); + } + + Categories.addActiveUser(cid, uid); + }); + }); + } + }(exports)); \ No newline at end of file diff --git a/src/posts.js b/src/posts.js index 033168f030..469d256927 100644 --- a/src/posts.js +++ b/src/posts.js @@ -57,39 +57,10 @@ var db = require('./database'), }; db.setObject('post:' + pid, postData); - - postData.favourited = false; - postData.display_moderator_tools = true; - postData.relativeTime = new Date(timestamp).toISOString(); - - topics.addPostToTopic(tid, pid); - topics.increasePostCount(tid); - topics.updateTimestamp(tid, timestamp); - db.incrObjectField('global', 'postCount'); - topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { - - var cid = topicData.cid; - - feed.updateTopic(tid); - feed.updateRecent(); - - db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid); - - if(parseInt(topicData.pinned, 10) === 0) { - db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid); - } - - db.setCount('cid:' + cid + ':active_users', function(err, amount) { - if (amount > 15) { - db.setRemoveRandom('cid:' + cid + ':active_users'); - } - - categories.addActiveUser(cid, uid); - }); - }); - + topics.onNewPostMade(tid, pid, timestamp); + categories.onNewPostMade(uid, tid, pid, timestamp); user.onNewPostMade(uid, tid, pid, timestamp); plugins.fireHook('filter:post.get', postData, function(err, newPostData) { @@ -97,19 +68,17 @@ var db = require('./database'), return callback(err, null); } - postData = newPostData; - - postTools.parse(postData.content, function(err, content) { + postTools.parse(newPostData.content, function(err, content) { if(err) { return callback(err, null); } - postData.content = content; + newPostData.content = content; - plugins.fireHook('action:post.save', postData); + plugins.fireHook('action:post.save', newPostData); db.searchIndex('post', content, pid); - callback(null, postData); + callback(null, newPostData); }); }); }); @@ -117,55 +86,6 @@ var db = require('./database'), }); }; - Posts.reply = function(tid, uid, content, callback) { - threadTools.privileges(tid, uid, function(err, privileges) { - if (content) { - content = content.trim(); - } - - if (!content || content.length < meta.config.minimumPostLength) { - return callback(new Error('content-too-short')); - } else if (!privileges.write) { - return callback(new Error('no-privileges')); - } - - Posts.create(uid, tid, content, function(err, postData) { - if(err) { - return callback(err, null); - } else if(!postData) { - callback(new Error('reply-error'), null); - } - - Posts.getCidByPid(postData.pid, function(err, cid) { - if(err) { - return callback(err, null); - } - - db.delete('cid:' + cid + ':read_by_uid'); - }); - - topics.markAsUnreadForAll(tid, function(err) { - if(err) { - return callback(err, null); - } - - topics.markAsRead(tid, uid); - topics.pushUnreadCount(); - }); - - threadTools.notifyFollowers(tid, uid); - - Posts.addUserInfoToPost(postData, function(err) { - if(err) { - return callback(err, null); - } - - callback(null, postData); - }); - }); - }); - } - Posts.getPostsByTid = function(tid, start, end, callback) { db.getListRange('tid:' + tid + ':posts', start, end, function(err, pids) { if(err) { diff --git a/src/topics.js b/src/topics.js index 8fffcedbdf..43cad5c945 100644 --- a/src/topics.js +++ b/src/topics.js @@ -87,11 +87,6 @@ var async = require('async'), user.addTopicIdToUser(uid, tid); - // let everyone know that there is an unread topic in this category - db.delete('cid:' + cid + ':read_by_uid', function(err, data) { - Topics.markAsRead(tid, uid); - }); - // in future it may be possible to add topics to several categories, so leaving the door open here. db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid); db.incrObjectField('category:' + cid, 'topic_count'); @@ -99,18 +94,15 @@ var async = require('async'), feed.updateCategory(cid); - posts.create(uid, tid, content, function(err, postData) { + Topics.reply(tid, uid, content, function(err, postData) { if(err) { return callback(err, null); } else if(!postData) { return callback(new Error('invalid-post'), null); } - // Auto-subscribe the post creator to the newly created topic threadTools.toggleFollow(tid, uid); - Topics.pushUnreadCount(); - Topics.getTopicForCategoryView(tid, uid, function(topicData) { topicData.unreplied = 1; @@ -125,6 +117,73 @@ var async = require('async'), }); }; + Topics.reply = function(tid, uid, content, callback) { + threadTools.privileges(tid, uid, function(err, privileges) { + if (content) { + content = content.trim(); + } + + if (!content || content.length < meta.config.minimumPostLength) { + return callback(new Error('content-too-short')); + } else if (!privileges.write) { + return callback(new Error('no-privileges')); + } + + posts.create(uid, tid, content, function(err, postData) { + if(err) { + return callback(err, null); + } else if(!postData) { + callback(new Error('reply-error'), null); + } + + posts.getCidByPid(postData.pid, function(err, cid) { + if(err) { + return callback(err, null); + } + + db.delete('cid:' + cid + ':read_by_uid', function(err) { + Topics.markAsUnreadForAll(tid, function(err) { + if(err) { + return callback(err, null); + } + + Topics.markAsRead(tid, uid); + Topics.pushUnreadCount(); + }); + }); + }); + + db.getObjectField('tid:lastFeedUpdate', tid, function(err, lastFeedUpdate) { + console.log(lastFeedUpdate); + console.log(Date.now()); + var now = Date.now(); + if(!lastFeedUpdate || parseInt(lastFeedUpdate, 10) < now - 3600000) { + feed.updateTopic(tid); + db.setObjectField('tid:lastFeedUpdate', tid, now); + } + }); + + feed.updateRecent(); + + threadTools.notifyFollowers(tid, uid); + + user.sendPostNotificationToFollowers(uid, tid, postData.pid); + + posts.addUserInfoToPost(postData, function(err) { + if(err) { + return callback(err, null); + } + + postData.favourited = false; + postData.display_moderator_tools = true; + postData.relativeTime = new Date(postData.timestamp).toISOString(); + + callback(null, postData); + }); + }); + }); + } + Topics.getTopicData = function(tid, callback) { db.getObject('topic:' + tid, function(err, data) { if(err) { @@ -883,6 +942,12 @@ var async = require('async'), Topics.setTopicField(tid, 'lastposttime', timestamp); } + Topics.onNewPostMade = function(tid, pid, timestamp) { + Topics.addPostToTopic(tid, pid); + Topics.increasePostCount(tid); + Topics.updateTimestamp(tid, timestamp); + } + Topics.addPostToTopic = function(tid, pid) { db.listAppend('tid:' + tid + ':posts', pid); } diff --git a/src/user.js b/src/user.js index 719c09cdc7..1b163825a5 100644 --- a/src/user.js +++ b/src/user.js @@ -433,8 +433,6 @@ var bcrypt = require('bcrypt'), }); User.setUserField(uid, 'lastposttime', timestamp); - - User.sendPostNotificationToFollowers(uid, tid, pid); }; User.addPostIdToUser = function(uid, pid) { diff --git a/src/websockets.js b/src/websockets.js index 9a9d0700d4..8d16a66c08 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -449,7 +449,7 @@ websockets.init = function(io) { return; } - posts.reply(data.topic_id, uid, data.content, function(err, postData) { + topics.reply(data.topic_id, uid, data.content, function(err, postData) { if(err) { if (err.message === 'content-too-short') { posts.emitContentTooShortAlert(socket);