all files / src/user/ posts.js

80% Statements 40/50
63.64% Branches 21/33
83.33% Functions 15/18
80% Lines 40/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112              25×       25×   25×     25×     25×     25×       25×       25× 13×     12×   12×       12×       12× 12×       12×   12× 11×     10×       22×   22×     22×     22×     22×         22×     11×           22× 22× 22×     22×     22×                  
'use strict';
 
var async = require('async'),
	db = require('../database'),
	meta = require('../meta'),
	privileges = require('../privileges');
 
module.exports = function(User) {
 
	User.isReadyToPost = function(uid, cid, callback) {
		Iif (parseInt(uid, 10) === 0) {
			return callback();
		}
 
		async.parallel({
			userData: function(next) {
				User.getUserFields(uid, ['banned', 'lastposttime', 'joindate', 'email', 'email:confirmed', 'reputation'], next);
			},
			exists: function(next) {
				db.exists('user:' + uid, next);
			},
			isAdminOrMod: function(next) {
				privileges.categories.isAdminOrMod(cid, uid, next);
			}
		}, function(err, results) {
			Iif (err) {
				return callback(err);
			}
 
			Iif (!results.exists) {
				return callback(new Error('[[error:no-user]]'));
			}
 
			if (results.isAdminOrMod) {
				return callback();
			}
 
			var userData = results.userData;
 
			Iif (parseInt(userData.banned, 10) === 1) {
				return callback(new Error('[[error:user-banned]]'));
			}
 
			Iif (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) {
				return callback(new Error('[[error:email-not-confirmed]]'));
			}
 
			var now = Date.now();
			Iif (now - parseInt(userData.joindate, 10) < parseInt(meta.config.initialPostDelay, 10) * 1000) {
				return callback(new Error('[[error:user-too-new, ' + meta.config.initialPostDelay + ']]'));
			}
 
			var lastposttime = userData.lastposttime || 0;
 
			if (parseInt(meta.config.newbiePostDelay, 10) > 0 && parseInt(meta.config.newbiePostDelayThreshold, 10) > parseInt(userData.reputation, 10) && now - parseInt(lastposttime, 10) < parseInt(meta.config.newbiePostDelay, 10) * 1000) {
				return callback(new Error('[[error:too-many-posts-newbie, ' + meta.config.newbiePostDelay + ', ' + meta.config.newbiePostDelayThreshold + ']]'));
			} else if (now - parseInt(lastposttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) {
				return callback(new Error('[[error:too-many-posts, ' + meta.config.postDelay + ']]'));
			}
 
			callback();
		});
	};
 
	User.onNewPostMade = function(postData, callback) {
		async.series([
			function(next) {
				User.addPostIdToUser(postData.uid, postData.pid, postData.timestamp, next);
			},
			function(next) {
				User.incrementUserPostCountBy(postData.uid, 1, next);
			},
			function(next) {
				User.setUserField(postData.uid, 'lastposttime', postData.timestamp, next);
			},
			function(next) {
				User.updateLastOnlineTime(postData.uid, next);
			}
		], callback);
	};
 
	User.addPostIdToUser = function(uid, pid, timestamp, callback) {
		db.sortedSetAdd('uid:' + uid + ':posts', timestamp, pid, callback);
	};
 
	User.addTopicIdToUser = function(uid, tid, timestamp, callback) {
		async.parallel([
			async.apply(db.sortedSetAdd, 'uid:' + uid + ':topics', timestamp, tid),
			async.apply(User.incrementUserFieldBy, uid, 'topiccount', 1)
		], callback);
	};
 
	User.incrementUserPostCountBy = function(uid, value, callback) {
		callback = callback || function() {};
		User.incrementUserFieldBy(uid, 'postcount', value, function(err, newpostcount) {
			Iif (err) {
				return callback(err);
			}
			Iif (!parseInt(uid, 10)) {
				return callback();
			}
			db.sortedSetAdd('users:postcount', newpostcount, uid, callback);
		});
	};
 
	User.getPostIds = function(uid, start, stop, callback) {
		db.getSortedSetRevRange('uid:' + uid + ':posts', start, stop, function(err, pids) {
			callback(err, Array.isArray(pids) ? pids : []);
		});
	};
 
};