mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-29 10:06:13 +01:00
closes #4688
This commit is contained in:
@@ -46,8 +46,7 @@ var meta = require('./meta');
|
|||||||
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
|
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
adjustPostVotes(postData, uid, type, unvote, function(err, votes) {
|
adjustPostVotes(postData, uid, type, unvote, function(err) {
|
||||||
postData.votes = votes;
|
|
||||||
callback(err, {
|
callback(err, {
|
||||||
user: {
|
user: {
|
||||||
reputation: newreputation
|
reputation: newreputation
|
||||||
@@ -91,11 +90,10 @@ var meta = require('./meta');
|
|||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
var voteCount = parseInt(results.upvotes, 10) - parseInt(results.downvotes, 10);
|
postData.upvotes = parseInt(results.upvotes, 10);
|
||||||
|
postData.downvotes = parseInt(results.downvotes, 10);
|
||||||
posts.updatePostVoteCount(postData, voteCount, function(err) {
|
postData.votes = postData.upvotes - postData.downvotes;
|
||||||
callback(err, voteCount);
|
posts.updatePostVoteCount(postData, callback);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/posts.js
12
src/posts.js
@@ -55,7 +55,9 @@ var plugins = require('./plugins');
|
|||||||
if (!post) {
|
if (!post) {
|
||||||
return next();
|
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.timestampISO = utils.toISOString(post.timestamp);
|
||||||
post.editedISO = parseInt(post.edited, 10) !== 0 ? utils.toISOString(post.edited) : '';
|
post.editedISO = parseInt(post.edited, 10) !== 0 ? utils.toISOString(post.edited) : '';
|
||||||
Posts.parsePost(post, next);
|
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) {
|
if (!postData || !postData.pid || !postData.tid) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function (next) {
|
function (next) {
|
||||||
if (postData.uid) {
|
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 {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
@@ -240,12 +242,12 @@ var plugins = require('./plugins');
|
|||||||
if (parseInt(mainPid, 10) === parseInt(postData.pid, 10)) {
|
if (parseInt(mainPid, 10) === parseInt(postData.pid, 10)) {
|
||||||
return next();
|
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);
|
], next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
Posts.setPostField(postData.pid, 'votes', voteCount, next);
|
Posts.setPostFields(postData.pid, {upvotes: postData.upvotes, downvotes: postData.downvotes}, next);
|
||||||
}
|
}
|
||||||
], function(err) {
|
], function(err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async'),
|
var async = require('async');
|
||||||
_ = require('underscore'),
|
var _ = require('underscore');
|
||||||
|
|
||||||
meta = require('../meta'),
|
var meta = require('../meta');
|
||||||
db = require('../database'),
|
var db = require('../database');
|
||||||
plugins = require('../plugins'),
|
var plugins = require('../plugins');
|
||||||
user = require('../user'),
|
var user = require('../user');
|
||||||
topics = require('../topics'),
|
var topics = require('../topics');
|
||||||
categories = require('../categories');
|
var categories = require('../categories');
|
||||||
|
|
||||||
|
|
||||||
module.exports = function(Posts) {
|
module.exports = function(Posts) {
|
||||||
|
|
||||||
Posts.create = function(data, callback) {
|
Posts.create = function(data, callback) {
|
||||||
// This is an internal method, consider using Topics.reply instead
|
// This is an internal method, consider using Topics.reply instead
|
||||||
var uid = data.uid,
|
var uid = data.uid;
|
||||||
tid = data.tid,
|
var tid = data.tid;
|
||||||
content = data.content.toString(),
|
var content = data.content.toString();
|
||||||
timestamp = data.timestamp || Date.now();
|
var timestamp = data.timestamp || Date.now();
|
||||||
|
|
||||||
if (!uid && parseInt(uid, 10) !== 0) {
|
if (!uid && parseInt(uid, 10) !== 0) {
|
||||||
return callback(new Error('[[error:invalid-uid]]'));
|
return callback(new Error('[[error:invalid-uid]]'));
|
||||||
@@ -38,7 +39,6 @@ module.exports = function(Posts) {
|
|||||||
'content': content,
|
'content': content,
|
||||||
'timestamp': timestamp,
|
'timestamp': timestamp,
|
||||||
'reputation': 0,
|
'reputation': 0,
|
||||||
'votes': 0,
|
|
||||||
'editor': '',
|
'editor': '',
|
||||||
'edited': 0,
|
'edited': 0,
|
||||||
'deleted': 0
|
'deleted': 0
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ var db = require('./database'),
|
|||||||
schemaDate, thisSchemaDate,
|
schemaDate, thisSchemaDate,
|
||||||
|
|
||||||
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
|
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
|
||||||
latestSchema = Date.UTC(2016, 4, 28);
|
latestSchema = Date.UTC(2016, 5, 13);
|
||||||
|
|
||||||
Upgrade.check = function(callback) {
|
Upgrade.check = function(callback) {
|
||||||
db.get('schemaDate', function(err, value) {
|
db.get('schemaDate', function(err, value) {
|
||||||
@@ -504,7 +504,7 @@ Upgrade.upgrade = function(callback) {
|
|||||||
|
|
||||||
var groupsAPI = require('./groups');
|
var groupsAPI = require('./groups');
|
||||||
var privilegesAPI = require('./privileges');
|
var privilegesAPI = require('./privileges');
|
||||||
|
|
||||||
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
|
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
|
||||||
async.eachSeries(cids, function(cid, next) {
|
async.eachSeries(cids, function(cid, next) {
|
||||||
privilegesAPI.categories.list(cid, function(err, data) {
|
privilegesAPI.categories.list(cid, function(err, data) {
|
||||||
@@ -523,13 +523,13 @@ Upgrade.upgrade = function(callback) {
|
|||||||
return next(err);
|
return next(err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
next(null);
|
next(null);
|
||||||
}, next);
|
}, next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
async.eachSeries(users, function(user, 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) {
|
return groupsAPI.join('cid:' + cid + ':privileges:topics:read', user.uid, function(err) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
winston.info('cid:' + cid + ':privileges:topics:read granted to uid: ' + user.uid);
|
winston.info('cid:' + cid + ':privileges:topics:read granted to uid: ' + user.uid);
|
||||||
@@ -538,7 +538,7 @@ Upgrade.upgrade = function(callback) {
|
|||||||
return next(err);
|
return next(err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
next(null);
|
next(null);
|
||||||
}, next);
|
}, next);
|
||||||
}
|
}
|
||||||
@@ -552,7 +552,7 @@ Upgrade.upgrade = function(callback) {
|
|||||||
});
|
});
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (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');
|
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!');
|
winston.info('[2016/05/28] Giving topics:read privs to any group that was previously allowed to Find & Access Category - skipped!');
|
||||||
next();
|
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
|
// Add new schema updates here
|
||||||
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!
|
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!
|
||||||
|
|||||||
Reference in New Issue
Block a user