diff --git a/src/favourites.js b/src/favourites.js index e5d8eb3e0a..543b41af98 100644 --- a/src/favourites.js +++ b/src/favourites.js @@ -46,8 +46,7 @@ var meta = require('./meta'); db.sortedSetAdd('users:reputation', newreputation, postData.uid); } - adjustPostVotes(postData, uid, type, unvote, function(err, votes) { - postData.votes = votes; + adjustPostVotes(postData, uid, type, unvote, function(err) { callback(err, { user: { reputation: newreputation @@ -91,11 +90,10 @@ var meta = require('./meta'); if (err) { return callback(err); } - var voteCount = parseInt(results.upvotes, 10) - parseInt(results.downvotes, 10); - - posts.updatePostVoteCount(postData, voteCount, function(err) { - callback(err, voteCount); - }); + postData.upvotes = parseInt(results.upvotes, 10); + postData.downvotes = parseInt(results.downvotes, 10); + postData.votes = postData.upvotes - postData.downvotes; + posts.updatePostVoteCount(postData, callback); }); }); } diff --git a/src/posts.js b/src/posts.js index c4703f8ae7..8a494be7c7 100644 --- a/src/posts.js +++ b/src/posts.js @@ -55,7 +55,9 @@ var plugins = require('./plugins'); if (!post) { return next(); } - + post.upvotes = parseInt(post.upvotes, 10) || 0; + post.downvotes = parseInt(post.downvotes, 10) || 0; + post.votes = post.upvotes - post.downvotes; post.timestampISO = utils.toISOString(post.timestamp); post.editedISO = parseInt(post.edited, 10) !== 0 ? utils.toISOString(post.edited) : ''; Posts.parsePost(post, next); @@ -219,14 +221,14 @@ var plugins = require('./plugins'); }); }; - Posts.updatePostVoteCount = function(postData, voteCount, callback) { + Posts.updatePostVoteCount = function(postData, callback) { if (!postData || !postData.pid || !postData.tid) { return callback(); } async.parallel([ function (next) { if (postData.uid) { - db.sortedSetAdd('uid:' + postData.uid + ':posts:votes', voteCount, postData.pid, next); + db.sortedSetAdd('uid:' + postData.uid + ':posts:votes', postData.votes, postData.pid, next); } else { next(); } @@ -240,12 +242,12 @@ var plugins = require('./plugins'); if (parseInt(mainPid, 10) === parseInt(postData.pid, 10)) { return next(); } - db.sortedSetAdd('tid:' + postData.tid + ':posts:votes', voteCount, postData.pid, next); + db.sortedSetAdd('tid:' + postData.tid + ':posts:votes', postData.votes, postData.pid, next); } ], next); }, function (next) { - Posts.setPostField(postData.pid, 'votes', voteCount, next); + Posts.setPostFields(postData.pid, {upvotes: postData.upvotes, downvotes: postData.downvotes}, next); } ], function(err) { callback(err); diff --git a/src/posts/create.js b/src/posts/create.js index 041d662915..e84fdaaf3d 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -1,23 +1,24 @@ 'use strict'; -var async = require('async'), - _ = require('underscore'), +var async = require('async'); +var _ = require('underscore'); - meta = require('../meta'), - db = require('../database'), - plugins = require('../plugins'), - user = require('../user'), - topics = require('../topics'), - categories = require('../categories'); +var meta = require('../meta'); +var db = require('../database'); +var plugins = require('../plugins'); +var user = require('../user'); +var topics = require('../topics'); +var categories = require('../categories'); module.exports = function(Posts) { + Posts.create = function(data, callback) { // This is an internal method, consider using Topics.reply instead - var uid = data.uid, - tid = data.tid, - content = data.content.toString(), - timestamp = data.timestamp || Date.now(); + var uid = data.uid; + var tid = data.tid; + var content = data.content.toString(); + var timestamp = data.timestamp || Date.now(); if (!uid && parseInt(uid, 10) !== 0) { return callback(new Error('[[error:invalid-uid]]')); @@ -38,7 +39,6 @@ module.exports = function(Posts) { 'content': content, 'timestamp': timestamp, 'reputation': 0, - 'votes': 0, 'editor': '', 'edited': 0, 'deleted': 0 diff --git a/src/upgrade.js b/src/upgrade.js index 0f863c757c..895520d193 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -10,7 +10,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2016, 4, 28); + latestSchema = Date.UTC(2016, 5, 13); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -504,7 +504,7 @@ Upgrade.upgrade = function(callback) { var groupsAPI = require('./groups'); var privilegesAPI = require('./privileges'); - + db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { async.eachSeries(cids, function(cid, next) { privilegesAPI.categories.list(cid, function(err, data) { @@ -523,13 +523,13 @@ Upgrade.upgrade = function(callback) { return next(err); }); } - + next(null); }, next); }, function(next) { async.eachSeries(users, function(user, next) { - if (user.privileges['read']) { + if (user.privileges.read) { return groupsAPI.join('cid:' + cid + ':privileges:topics:read', user.uid, function(err) { if (!err) { winston.info('cid:' + cid + ':privileges:topics:read granted to uid: ' + user.uid); @@ -538,7 +538,7 @@ Upgrade.upgrade = function(callback) { return next(err); }); } - + next(null); }, next); } @@ -552,7 +552,7 @@ Upgrade.upgrade = function(callback) { }); }, function(err) { if (err) { - return next(err); + return next(err); } winston.info('[2016/05/28] Giving topics:read privs to any group that was previously allowed to Find & Access Category - done'); @@ -563,6 +563,60 @@ Upgrade.upgrade = function(callback) { winston.info('[2016/05/28] Giving topics:read privs to any group that was previously allowed to Find & Access Category - skipped!'); next(); } + }, + function(next) { + thisSchemaDate = Date.UTC(2016, 5, 13); + + if (schemaDate < thisSchemaDate) { + updatesMade = true; + winston.info('[2016/06/13] Store upvotes/downvotes separately'); + + var batch = require('./batch'); + var posts = require('./posts'); + var count = 0; + batch.processSortedSet('posts:pid', function(pids, next) { + winston.info('upgraded ' + count + ' posts'); + count += pids.length; + async.each(pids, function(pid, next) { + async.parallel({ + upvotes: function(next) { + db.setCount('pid:' + pid + ':upvote', next); + }, + downvotes: function(next) { + db.setCount('pid:' + pid + ':downvote', next); + } + }, function(err, results) { + if (err) { + return next(err); + } + var data = {}; + + if (parseInt(results.upvotes, 10) > 0) { + data.upvotes = results.upvotes; + } + if (parseInt(results.downvotes, 10) > 0) { + data.downvotes = results.downvotes; + } + + if (Object.keys(data).length) { + posts.setPostFields(pid, data, next); + } else { + next(); + } + }, next); + }, next); + }, {}, function(err) { + if (err) { + return next(err); + } + + winston.info('[2016/06/13] Store upvotes/downvotes separately done'); + Upgrade.update(thisSchemaDate, next); + }); + } else { + winston.info('[2016/06/13] Store upvotes/downvotes separately skipped!'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!