Files
NodeBB/src/user/posts.js

114 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
2016-05-18 19:02:43 +03:00
var async = require('async');
var db = require('../database');
var meta = require('../meta');
var privileges = require('../privileges');
module.exports = function (User) {
User.isReadyToPost = function (uid, cid, callback) {
2017-10-31 09:43:11 -04:00
isReady(uid, cid, 'lastposttime', callback);
};
User.isReadyToQueue = function (uid, cid, callback) {
isReady(uid, cid, 'lastqueuetime', callback);
};
function isReady(uid, cid, field, callback) {
if (parseInt(uid, 10) === 0) {
return callback();
}
2017-05-26 00:02:20 -04:00
async.waterfall([
function (next) {
async.parallel({
userData: function (next) {
2017-10-31 09:43:11 -04:00
User.getUserFields(uid, ['uid', 'banned', 'joindate', 'email', 'email:confirmed', 'reputation'].concat([field]), next);
2017-05-26 00:02:20 -04:00
},
isAdminOrMod: function (next) {
privileges.categories.isAdminOrMod(cid, uid, next);
},
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-26 00:02:20 -04:00
function (results, next) {
2018-10-25 17:02:59 -04:00
if (!results.userData.uid) {
2017-05-26 00:02:20 -04:00
return next(new Error('[[error:no-user]]'));
}
2017-05-26 00:02:20 -04:00
if (results.isAdminOrMod) {
return next();
}
2017-05-26 00:02:20 -04:00
var userData = results.userData;
2018-10-25 17:02:59 -04:00
if (userData.banned) {
2017-05-26 00:02:20 -04:00
return next(new Error('[[error:user-banned]]'));
}
if (meta.config.requireEmailConfirmation && !userData['email:confirmed']) {
2017-05-26 00:02:20 -04:00
return next(new Error('[[error:email-not-confirmed]]'));
}
2017-05-26 00:02:20 -04:00
var now = Date.now();
if (now - userData.joindate < meta.config.initialPostDelay * 1000) {
2017-05-26 00:02:20 -04:00
return next(new Error('[[error:user-too-new, ' + meta.config.initialPostDelay + ']]'));
}
2015-09-25 01:09:14 -04:00
2017-10-31 09:43:11 -04:00
var lasttime = userData[field] || 0;
if (meta.config.newbiePostDelay > 0 && meta.config.newbiePostDelayThreshold > userData.reputation && now - lasttime < meta.config.newbiePostDelay * 1000) {
2017-05-26 00:02:20 -04:00
return next(new Error('[[error:too-many-posts-newbie, ' + meta.config.newbiePostDelay + ', ' + meta.config.newbiePostDelayThreshold + ']]'));
} else if (now - lasttime < meta.config.postDelay * 1000) {
2017-05-26 00:02:20 -04:00
return next(new Error('[[error:too-many-posts, ' + meta.config.postDelay + ']]'));
}
2017-05-26 00:02:20 -04:00
next();
},
], callback);
2017-10-31 09:43:11 -04:00
}
User.onNewPostMade = function (postData, callback) {
async.series([
function (next) {
User.addPostIdToUser(postData, next);
},
function (next) {
User.incrementUserPostCountBy(postData.uid, 1, next);
},
function (next) {
User.setUserField(postData.uid, 'lastposttime', postData.timestamp, next);
2016-03-01 22:00:28 +02:00
},
function (next) {
2016-03-01 22:00:28 +02:00
User.updateLastOnlineTime(postData.uid, next);
2017-02-17 19:31:21 -07:00
},
], callback);
};
User.addPostIdToUser = function (postData, callback) {
db.sortedSetsAdd([
'uid:' + postData.uid + ':posts',
'cid:' + postData.cid + ':uid:' + postData.uid + ':pids',
], postData.timestamp, postData.pid, callback);
};
User.incrementUserPostCountBy = function (uid, value, callback) {
callback = callback || function () {};
2017-05-26 00:02:20 -04:00
async.waterfall([
function (next) {
User.incrementUserFieldBy(uid, 'postcount', value, next);
},
function (newpostcount, next) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
2017-05-26 00:02:20 -04:00
return next();
}
db.sortedSetAdd('users:postcount', newpostcount, uid, next);
},
], callback);
};
User.getPostIds = function (uid, start, stop, callback) {
db.getSortedSetRevRange('uid:' + uid + ':posts', start, stop, function (err, pids) {
callback(err, Array.isArray(pids) ? pids : []);
});
};
2017-02-18 02:30:48 -07:00
};