parent
0e8ee31b90
commit
6fea46b6e2
@ -1,113 +1,81 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const db = require('../database');
|
||||||
var db = require('../database');
|
const meta = require('../meta');
|
||||||
var meta = require('../meta');
|
const privileges = require('../privileges');
|
||||||
var privileges = require('../privileges');
|
|
||||||
|
|
||||||
module.exports = function (User) {
|
module.exports = function (User) {
|
||||||
User.isReadyToPost = function (uid, cid, callback) {
|
User.isReadyToPost = async function (uid, cid) {
|
||||||
isReady(uid, cid, 'lastposttime', callback);
|
await isReady(uid, cid, 'lastposttime');
|
||||||
};
|
};
|
||||||
|
|
||||||
User.isReadyToQueue = function (uid, cid, callback) {
|
User.isReadyToQueue = async function (uid, cid) {
|
||||||
isReady(uid, cid, 'lastqueuetime', callback);
|
await isReady(uid, cid, 'lastqueuetime');
|
||||||
};
|
};
|
||||||
|
|
||||||
function isReady(uid, cid, field, callback) {
|
async function isReady(uid, cid, field) {
|
||||||
if (parseInt(uid, 10) === 0) {
|
if (parseInt(uid, 10) === 0) {
|
||||||
return callback();
|
return;
|
||||||
}
|
}
|
||||||
async.waterfall([
|
const [userData, isAdminOrMod] = await Promise.all([
|
||||||
function (next) {
|
User.getUserFields(uid, ['uid', 'banned', 'joindate', 'email', 'email:confirmed', 'reputation'].concat([field])),
|
||||||
async.parallel({
|
privileges.categories.isAdminOrMod(cid, uid),
|
||||||
userData: function (next) {
|
]);
|
||||||
User.getUserFields(uid, ['uid', 'banned', 'joindate', 'email', 'email:confirmed', 'reputation'].concat([field]), next);
|
|
||||||
},
|
|
||||||
isAdminOrMod: function (next) {
|
|
||||||
privileges.categories.isAdminOrMod(cid, uid, next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
if (!results.userData.uid) {
|
|
||||||
return next(new Error('[[error:no-user]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (results.isAdminOrMod) {
|
if (!userData.uid) {
|
||||||
return next();
|
throw new Error('[[error:no-user]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
var userData = results.userData;
|
|
||||||
|
|
||||||
if (userData.banned) {
|
if (isAdminOrMod) {
|
||||||
return next(new Error('[[error:user-banned]]'));
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (meta.config.requireEmailConfirmation && !userData['email:confirmed']) {
|
if (userData.banned) {
|
||||||
return next(new Error('[[error:email-not-confirmed]]'));
|
throw new Error('[[error:user-banned]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
var now = Date.now();
|
if (meta.config.requireEmailConfirmation && !userData['email:confirmed']) {
|
||||||
if (now - userData.joindate < meta.config.initialPostDelay * 1000) {
|
throw new Error('[[error:email-not-confirmed]]');
|
||||||
return next(new Error('[[error:user-too-new, ' + meta.config.initialPostDelay + ']]'));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var lasttime = userData[field] || 0;
|
var now = Date.now();
|
||||||
|
if (now - userData.joindate < meta.config.initialPostDelay * 1000) {
|
||||||
|
throw new Error('[[error:user-too-new, ' + meta.config.initialPostDelay + ']]');
|
||||||
|
}
|
||||||
|
|
||||||
if (meta.config.newbiePostDelay > 0 && meta.config.newbiePostDelayThreshold > userData.reputation && now - lasttime < meta.config.newbiePostDelay * 1000) {
|
var lasttime = userData[field] || 0;
|
||||||
return next(new Error('[[error:too-many-posts-newbie, ' + meta.config.newbiePostDelay + ', ' + meta.config.newbiePostDelayThreshold + ']]'));
|
|
||||||
} else if (now - lasttime < meta.config.postDelay * 1000) {
|
|
||||||
return next(new Error('[[error:too-many-posts, ' + meta.config.postDelay + ']]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
next();
|
if (meta.config.newbiePostDelay > 0 && meta.config.newbiePostDelayThreshold > userData.reputation && now - lasttime < meta.config.newbiePostDelay * 1000) {
|
||||||
},
|
throw new Error('[[error:too-many-posts-newbie, ' + meta.config.newbiePostDelay + ', ' + meta.config.newbiePostDelayThreshold + ']]');
|
||||||
], callback);
|
} else if (now - lasttime < meta.config.postDelay * 1000) {
|
||||||
|
throw new Error('[[error:too-many-posts, ' + meta.config.postDelay + ']]');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
User.onNewPostMade = function (postData, callback) {
|
User.onNewPostMade = async function (postData) {
|
||||||
async.series([
|
await User.addPostIdToUser(postData);
|
||||||
function (next) {
|
await User.incrementUserPostCountBy(postData.uid, 1);
|
||||||
User.addPostIdToUser(postData, next);
|
await User.setUserField(postData.uid, 'lastposttime', postData.timestamp);
|
||||||
},
|
await User.updateLastOnlineTime(postData.uid);
|
||||||
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 (postData, callback) {
|
User.addPostIdToUser = async function (postData) {
|
||||||
db.sortedSetsAdd([
|
await db.sortedSetsAdd([
|
||||||
'uid:' + postData.uid + ':posts',
|
'uid:' + postData.uid + ':posts',
|
||||||
'cid:' + postData.cid + ':uid:' + postData.uid + ':pids',
|
'cid:' + postData.cid + ':uid:' + postData.uid + ':pids',
|
||||||
], postData.timestamp, postData.pid, callback);
|
], postData.timestamp, postData.pid);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.incrementUserPostCountBy = function (uid, value, callback) {
|
User.incrementUserPostCountBy = async function (uid, value) {
|
||||||
callback = callback || function () {};
|
const newpostcount = await User.incrementUserFieldBy(uid, 'postcount', value);
|
||||||
async.waterfall([
|
if (parseInt(uid, 10) <= 0) {
|
||||||
function (next) {
|
return;
|
||||||
User.incrementUserFieldBy(uid, 'postcount', value, next);
|
}
|
||||||
},
|
await db.sortedSetAdd('users:postcount', newpostcount, uid);
|
||||||
function (newpostcount, next) {
|
|
||||||
if (parseInt(uid, 10) <= 0) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
db.sortedSetAdd('users:postcount', newpostcount, uid, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
User.getPostIds = function (uid, start, stop, callback) {
|
User.getPostIds = async function (uid, start, stop) {
|
||||||
db.getSortedSetRevRange('uid:' + uid + ':posts', start, stop, function (err, pids) {
|
const pids = await db.getSortedSetRevRange('uid:' + uid + ':posts', start, stop);
|
||||||
callback(err, Array.isArray(pids) ? pids : []);
|
return Array.isArray(pids) ? pids : [];
|
||||||
});
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue