Merge branch 'develop' into chat-rewrite

This commit is contained in:
Julian Lam
2018-01-09 16:34:31 -05:00
83 changed files with 387 additions and 220 deletions

View File

@@ -61,6 +61,8 @@ module.exports = function (Categories) {
'topics:tag',
'posts:edit',
'posts:delete',
'posts:upvote',
'posts:downvote',
'topics:delete',
];

View File

@@ -6,9 +6,11 @@ var path = require('path');
var packageInstall = require('./package-install');
var dirname = require('./paths').baseDir;
var defaultPackage;
// check to make sure dependencies are installed
try {
fs.readFileSync(path.join(dirname, 'package.json'));
defaultPackage = JSON.parse(fs.readFileSync(path.join(dirname, 'install/package.json'), 'utf8'));
} catch (e) {
if (e.code === 'ENOENT') {
console.warn('package.json not found.');
@@ -29,13 +31,24 @@ try {
}
try {
fs.readFileSync(path.join(dirname, 'node_modules/async/package.json'), 'utf8');
fs.readFileSync(path.join(dirname, 'node_modules/commander/package.json'), 'utf8');
fs.readFileSync(path.join(dirname, 'node_modules/colors/package.json'), 'utf8');
fs.readFileSync(path.join(dirname, 'node_modules/nconf/package.json'), 'utf8');
var semver = require('semver');
var checkVersion = function (packageName) {
var version = JSON.parse(fs.readFileSync(path.join(dirname, 'node_modules', packageName, 'package.json'), 'utf8')).version;
if (!semver.satisfies(version, defaultPackage.dependencies[packageName])) {
var e = new TypeError('Incorrect dependency version: ' + packageName);
e.code = 'DEP_WRONG_VERSION';
throw e;
}
};
checkVersion('nconf');
checkVersion('async');
checkVersion('commander');
checkVersion('colors');
} catch (e) {
if (e.code === 'ENOENT') {
console.warn('Dependencies not yet installed.');
if (['ENOENT', 'DEP_WRONG_VERSION', 'MODULE_NOT_FOUND'].indexOf(e.code) !== -1) {
console.warn('Dependencies outdated or not yet installed.');
console.log('Installing them now...\n');
packageInstall.installAll();
@@ -241,7 +254,7 @@ program
'When running particular upgrade scripts, options are ignored.',
'By default all options are enabled. Passing any options disables that default.',
'Only package and dependency updates: ' + './nodebb upgrade -mi'.yellow,
'Only database update: ' + './nodebb upgrade -d'.yellow,
'Only database update: ' + './nodebb upgrade -s'.yellow,
].join('\n'));
})
.action(function (scripts, options) {

View File

@@ -30,8 +30,6 @@ function updatePackageFile() {
exports.updatePackageFile = updatePackageFile;
function installAll() {
process.stdout.write(' started\n'.green);
var prod = global.env !== 'development';
var command = 'npm install';
try {

View File

@@ -23,6 +23,7 @@ var steps = {
install: {
message: 'Bringing base dependencies up to date...',
handler: function (next) {
process.stdout.write(' started\n'.green);
packageInstall.installAll();
next();
},

View File

@@ -8,7 +8,7 @@ var topics = require('../topics');
var pagination = require('../pagination');
var helpers = require('./helpers');
var tagsController = {};
var tagsController = module.exports;
tagsController.getTag = function (req, res, next) {
var tag = validator.escape(String(req.params.tag));
@@ -33,7 +33,7 @@ tagsController.getTag = function (req, res, next) {
templateData.nextStart = stop + 1;
async.parallel({
topicCount: function (next) {
topics.getTagTopicCount(tag, next);
topics.getTagTopicCount(req.params.tag, next);
},
tids: function (next) {
topics.getTagTids(req.params.tag, start, stop, next);
@@ -47,44 +47,41 @@ tagsController.getTag = function (req, res, next) {
topicCount = results.topicCount;
topics.getTopics(results.tids, req.uid, next);
},
], function (err, topics) {
if (err) {
return next(err);
}
function (topics) {
res.locals.metaTags = [
{
name: 'title',
content: tag,
},
{
property: 'og:title',
content: tag,
},
];
templateData.topics = topics;
res.locals.metaTags = [
{
name: 'title',
content: tag,
},
{
property: 'og:title',
content: tag,
},
];
templateData.topics = topics;
var pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage));
templateData.pagination = pagination.create(page, pageCount);
var pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage));
templateData.pagination = pagination.create(page, pageCount);
res.render('tag', templateData);
});
res.render('tag', templateData);
},
], next);
};
tagsController.getTags = function (req, res, next) {
topics.getTags(0, 99, function (err, tags) {
if (err) {
return next(err);
}
tags = tags.filter(Boolean);
var data = {
tags: tags,
nextStart: 100,
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[tags:tags]]' }]),
title: '[[pages:tags]]',
};
res.render('tags', data);
});
async.waterfall([
function (next) {
topics.getTags(0, 99, next);
},
function (tags) {
tags = tags.filter(Boolean);
var data = {
tags: tags,
nextStart: 100,
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[tags:tags]]' }]),
title: '[[pages:tags]]',
};
res.render('tags', data);
},
], next);
};
module.exports = tagsController;

View File

@@ -90,11 +90,11 @@ module.exports = function (Groups) {
return callback(new Error('[[error:group-name-too-long]]'));
}
if (!Groups.isPrivilegeGroup(name) && name.indexOf(':') !== -1) {
if (!Groups.isPrivilegeGroup(name) && name.includes(':')) {
return callback(new Error('[[error:invalid-group-name]]'));
}
if (name.indexOf('/') !== -1 || !utils.slugify(name)) {
if (name.includes('/') || !utils.slugify(name)) {
return callback(new Error('[[error:invalid-group-name]]'));
}

View File

@@ -14,6 +14,7 @@ var JS = module.exports;
JS.scripts = {
base: [
'node_modules/promise-polyfill/dist/polyfill.js',
'node_modules/jquery/dist/jquery.js',
'node_modules/socket.io-client/dist/socket.io.js',
'public/vendor/jquery/timeago/jquery.timeago.js',
@@ -36,7 +37,6 @@ JS.scripts = {
'public/src/ajaxify.js',
'public/src/overrides.js',
'public/src/widgets.js',
'node_modules/promise-polyfill/promise.js',
],
// files listed below are only available client-side, or are bundled in to reduce # of network requests on cold load

View File

@@ -6,6 +6,7 @@ var meta = require('../meta');
var db = require('../database');
var user = require('../user');
var plugins = require('../plugins');
var privileges = require('../privileges');
module.exports = function (Posts) {
var votesInProgress = {};
@@ -15,16 +16,27 @@ module.exports = function (Posts) {
return callback(new Error('[[error:reputation-system-disabled]]'));
}
if (voteInProgress(pid, uid)) {
return callback(new Error('[[error:already-voting-for-this-post]]'));
}
async.waterfall([
function (next) {
privileges.posts.can('posts:upvote', pid, uid, next);
},
function (canUpvote, next) {
if (!canUpvote) {
return next(new Error('[[error:no-privileges]]'));
}
putVoteInProgress(pid, uid);
if (voteInProgress(pid, uid)) {
return next(new Error('[[error:already-voting-for-this-post]]'));
}
toggleVote('upvote', pid, uid, function (err, data) {
clearVoteProgress(pid, uid);
callback(err, data);
});
putVoteInProgress(pid, uid);
toggleVote('upvote', pid, uid, function (err, data) {
clearVoteProgress(pid, uid);
next(err, data);
});
},
], callback);
};
Posts.downvote = function (pid, uid, callback) {
@@ -36,16 +48,27 @@ module.exports = function (Posts) {
return callback(new Error('[[error:downvoting-disabled]]'));
}
if (voteInProgress(pid, uid)) {
return callback(new Error('[[error:already-voting-for-this-post]]'));
}
async.waterfall([
function (next) {
privileges.posts.can('posts:downvote', pid, uid, next);
},
function (canUpvote, next) {
if (!canUpvote) {
return next(new Error('[[error:no-privileges]]'));
}
putVoteInProgress(pid, uid);
if (voteInProgress(pid, uid)) {
return next(new Error('[[error:already-voting-for-this-post]]'));
}
toggleVote('downvote', pid, uid, function (err, data) {
clearVoteProgress(pid, uid);
callback(err, data);
});
putVoteInProgress(pid, uid);
toggleVote('downvote', pid, uid, function (err, data) {
clearVoteProgress(pid, uid);
next(err, data);
});
},
], callback);
};
Posts.unvote = function (pid, uid, callback) {
@@ -106,7 +129,7 @@ module.exports = function (Posts) {
};
function voteInProgress(pid, uid) {
return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].indexOf(parseInt(pid, 10)) !== -1;
return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].includes(parseInt(pid, 10));
}
function putVoteInProgress(pid, uid) {

View File

@@ -11,6 +11,8 @@ privileges.privilegeLabels = [
{ name: 'Tag Topics' },
{ name: 'Edit Posts' },
{ name: 'Delete Posts' },
{ name: 'Upvote Posts' },
{ name: 'Downvote Posts' },
{ name: 'Delete Topics' },
{ name: 'Purge' },
{ name: 'Moderate' },
@@ -25,6 +27,8 @@ privileges.userPrivilegeList = [
'topics:tag',
'posts:edit',
'posts:delete',
'posts:upvote',
'posts:downvote',
'topics:delete',
'purge',
'moderate',

View File

@@ -64,6 +64,9 @@ module.exports = function (Topics) {
'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids',
], timestamp, topicData.tid, next);
},
function (next) {
db.sortedSetAdd('cid:' + topicData.cid + ':tids:votes', 0, topicData.tid, next);
},
function (next) {
categories.updateRecentTid(topicData.cid, topicData.tid, next);
},

View File

@@ -250,6 +250,7 @@ module.exports = function (Topics) {
var topic;
var oldCid;
var cid = data.cid;
async.waterfall([
function (next) {
Topics.exists(tid, next);
@@ -258,14 +259,18 @@ module.exports = function (Topics) {
if (!exists) {
return next(new Error('[[error:no-topic]]'));
}
Topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount'], next);
Topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount', 'upvotes', 'downvotes'], next);
},
function (topicData, next) {
topic = topicData;
if (parseInt(cid, 10) === parseInt(topic.cid, 10)) {
return next(new Error('[[error:cant-move-topic-to-same-category]]'));
}
db.sortedSetsRemove([
'cid:' + topicData.cid + ':tids',
'cid:' + topicData.cid + ':tids:pinned',
'cid:' + topicData.cid + ':tids:posts',
'cid:' + topicData.cid + ':tids:votes',
'cid:' + topicData.cid + ':tids:lastposttime',
'cid:' + topicData.cid + ':recent_tids',
], tid, next);
@@ -285,6 +290,10 @@ module.exports = function (Topics) {
topic.postcount = topic.postcount || 0;
db.sortedSetAdd('cid:' + cid + ':tids:posts', topic.postcount, tid, next);
},
function (next) {
var votes = (parseInt(topic.upvotes, 10) || 0) - (parseInt(topic.downvotes, 10) || 0);
db.sortedSetAdd('cid:' + cid + ':tids:votes', votes, tid, next);
},
], function (err) {
next(err);
});

View File

@@ -16,7 +16,7 @@ module.exports = {
var topicData;
async.waterfall([
function (next) {
db.getObjectFields('topic:' + tid, ['mainPid', 'cid'], next);
db.getObjectFields('topic:' + tid, ['mainPid', 'cid', 'pinned'], next);
},
function (_topicData, next) {
topicData = _topicData;
@@ -44,7 +44,11 @@ module.exports = {
db.sortedSetAdd('topics:votes', votes, tid, next);
},
function (next) {
db.sortedSetAdd('cid:' + topicData.cid + ':tids:votes', votes, tid, next);
if (parseInt(topicData.pinned, 10) !== 1) {
db.sortedSetAdd('cid:' + topicData.cid + ':tids:votes', votes, tid, next);
} else {
next();
}
},
], function (err) {
next(err);

View File

@@ -0,0 +1,53 @@
'use strict';
var async = require('async');
var batch = require('../../batch');
var db = require('../../database');
module.exports = {
name: 'Fix sort by votes for moved topics',
timestamp: Date.UTC(2018, 0, 8),
method: function (callback) {
var progress = this.progress;
batch.processSortedSet('topics:tid', function (tids, next) {
async.eachLimit(tids, 500, function (tid, _next) {
progress.incr();
var topicData;
async.waterfall([
function (next) {
db.getObjectFields('topic:' + tid, ['cid', 'oldCid', 'upvotes', 'downvotes', 'pinned'], next);
},
function (_topicData, next) {
topicData = _topicData;
if (!topicData.cid || !topicData.oldCid) {
return _next();
}
var upvotes = parseInt(topicData.upvotes, 10) || 0;
var downvotes = parseInt(topicData.downvotes, 10) || 0;
var votes = upvotes - downvotes;
async.series([
function (next) {
db.sortedSetRemove('cid:' + topicData.oldCid + ':tids:votes', tid, next);
},
function (next) {
if (parseInt(topicData.pinned, 10) !== 1) {
db.sortedSetAdd('cid:' + topicData.cid + ':tids:votes', votes, tid, next);
} else {
next();
}
},
], function (err) {
next(err);
});
},
], _next);
}, next);
}, {
progress: progress,
batch: 500,
}, callback);
},
};

View File

@@ -0,0 +1,22 @@
'use strict';
var async = require('async');
var privileges = require('../../privileges');
var db = require('../../database');
module.exports = {
name: 'Give vote privilege to registered-users on all categories',
timestamp: Date.UTC(2018, 0, 9),
method: function (callback) {
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) {
if (err) {
return callback(err);
}
async.eachSeries(cids, function (cid, next) {
privileges.categories.give(['posts:upvote', 'posts:downvote'], cid, 'registered-users', next);
}, callback);
});
},
};

View File

@@ -5,7 +5,7 @@
<th class="arrowed" colspan="3">
[[admin/manage/categories:privileges.section-viewing]]
</th>
<th class="arrowed" colspan="6">
<th class="arrowed" colspan="8">
[[admin/manage/categories:privileges.section-posting]]
</th>
<th class="arrowed" colspan="2">
@@ -61,7 +61,7 @@
<th class="arrowed" colspan="3">
[[admin/manage/categories:privileges.section-viewing]]
</th>
<th class="arrowed" colspan="6">
<th class="arrowed" colspan="8">
[[admin/manage/categories:privileges.section-posting]]
</th>
<th class="arrowed" colspan="2">