mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
lots of cleanup and refactor
This commit is contained in:
17
src/posts.js
17
src/posts.js
@@ -439,26 +439,23 @@ var db = require('./database'),
|
|||||||
|
|
||||||
Posts.reIndexPids = function(pids, callback) {
|
Posts.reIndexPids = function(pids, callback) {
|
||||||
|
|
||||||
function reIndex(pid, callback) {
|
function reIndex(pid, next) {
|
||||||
|
|
||||||
Posts.getPostField(pid, 'content', function(err, content) {
|
Posts.getPostField(pid, 'content', function(err, content) {
|
||||||
db.searchRemove('post', pid, function() {
|
if(err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.searchRemove('post', pid, function() {
|
||||||
if (content && content.length) {
|
if (content && content.length) {
|
||||||
db.searchIndex('post', content, pid);
|
db.searchIndex('post', content, pid);
|
||||||
}
|
}
|
||||||
callback(null);
|
next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async.each(pids, reIndex, function(err) {
|
async.each(pids, reIndex, callback);
|
||||||
if (err) {
|
|
||||||
callback(err, null);
|
|
||||||
} else {
|
|
||||||
callback(null, 'Posts reindexed');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Posts.getFavourites = function(uid, callback) {
|
Posts.getFavourites = function(uid, callback) {
|
||||||
|
|||||||
161
src/topics.js
161
src/topics.js
@@ -53,7 +53,6 @@ var async = require('async'),
|
|||||||
|
|
||||||
user.addTopicIdToUser(uid, tid, timestamp);
|
user.addTopicIdToUser(uid, tid, timestamp);
|
||||||
|
|
||||||
// in future it may be possible to add topics to several categories, so leaving the door open here.
|
|
||||||
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
|
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
|
||||||
db.incrObjectField('category:' + cid, 'topic_count');
|
db.incrObjectField('category:' + cid, 'topic_count');
|
||||||
db.incrObjectField('global', 'topicCount');
|
db.incrObjectField('global', 'topicCount');
|
||||||
@@ -66,25 +65,14 @@ var async = require('async'),
|
|||||||
};
|
};
|
||||||
|
|
||||||
Topics.post = function(uid, title, content, cid, callback) {
|
Topics.post = function(uid, title, content, cid, callback) {
|
||||||
|
|
||||||
categoryTools.privileges(cid, uid, function(err, privileges) {
|
|
||||||
|
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
} else if(!privileges.write) {
|
|
||||||
return callback(new Error('no-privileges'));
|
|
||||||
} else if (!cid) {
|
|
||||||
return callback(new Error('invalid-cid'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (title) {
|
if (title) {
|
||||||
title = title.trim();
|
title = title.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) {
|
if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) {
|
||||||
return callback(new Error('title-too-short'), null);
|
return callback(new Error('title-too-short'));
|
||||||
} else if(title.length > parseInt(meta.config.maximumTitleLength, 10)) {
|
} else if(title.length > parseInt(meta.config.maximumTitleLength, 10)) {
|
||||||
return callback(new Error('title-too-long'), null);
|
return callback(new Error('title-too-long'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content) {
|
if (content) {
|
||||||
@@ -92,49 +80,50 @@ var async = require('async'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!content || content.length < meta.config.miminumPostLength) {
|
if (!content || content.length < meta.config.miminumPostLength) {
|
||||||
return callback(new Error('content-too-short'), null);
|
return callback(new Error('content-too-short'));
|
||||||
}
|
}
|
||||||
|
|
||||||
user.getUserField(uid, 'lastposttime', function(err, lastposttime) {
|
if (!cid) {
|
||||||
|
return callback(new Error('invalid-cid'));
|
||||||
|
}
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
function(next) {
|
||||||
|
categoryTools.privileges(cid, uid, next);
|
||||||
|
},
|
||||||
|
function(privileges, next) {
|
||||||
|
if(!privileges.write) {
|
||||||
|
return next(new Error('no-privileges'));
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
user.isReadyToPost(uid, next);
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
Topics.create(uid, title, cid, next);
|
||||||
|
},
|
||||||
|
function(tid, next) {
|
||||||
|
Topics.reply(tid, uid, content, next);
|
||||||
|
},
|
||||||
|
function(postData, next) {
|
||||||
|
threadTools.toggleFollow(postData.tid, uid);
|
||||||
|
next(null, postData);
|
||||||
|
},
|
||||||
|
function(postData, next) {
|
||||||
|
Topics.getTopicForCategoryView(postData.tid, uid, function(err, topicData) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!lastposttime) {
|
|
||||||
lastposttime = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Date.now() - lastposttime < meta.config.postDelay * 1000) {
|
|
||||||
return callback(new Error('too-many-posts'), null);
|
|
||||||
}
|
|
||||||
|
|
||||||
Topics.create(uid, title, cid, function(err, tid) {
|
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
Topics.reply(tid, uid, content, function(err, postData) {
|
|
||||||
|
|
||||||
if(err) {
|
|
||||||
return callback(err, null);
|
|
||||||
} else if(!postData) {
|
|
||||||
return callback(new Error('invalid-post'), null);
|
|
||||||
}
|
|
||||||
|
|
||||||
threadTools.toggleFollow(tid, uid);
|
|
||||||
|
|
||||||
Topics.getTopicForCategoryView(tid, uid, function(topicData) {
|
|
||||||
topicData.unreplied = 1;
|
topicData.unreplied = 1;
|
||||||
|
next(null, {
|
||||||
callback(null, {
|
|
||||||
topicData: topicData,
|
topicData: topicData,
|
||||||
postData: postData
|
postData: postData
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
});
|
], callback);
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Topics.reply = function(tid, uid, content, callback) {
|
Topics.reply = function(tid, uid, content, callback) {
|
||||||
@@ -152,18 +141,7 @@ var async = require('async'),
|
|||||||
next();
|
next();
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
user.getUserField(uid, 'lastposttime', next);
|
user.isReadyToPost(uid, next);
|
||||||
},
|
|
||||||
function(lastposttime, next) {
|
|
||||||
if(!lastposttime) {
|
|
||||||
lastposttime = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Date.now() - parseInt(lastposttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) {
|
|
||||||
return next(new Error('too-many-posts'), null);
|
|
||||||
}
|
|
||||||
|
|
||||||
next();
|
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
if (content) {
|
if (content) {
|
||||||
@@ -235,17 +213,15 @@ var async = require('async'),
|
|||||||
pids.sort();
|
pids.sort();
|
||||||
var mainPid = pids[0];
|
var mainPid = pids[0];
|
||||||
|
|
||||||
posts.getPostData(mainPid, function(err, postData) {
|
async.parallel({
|
||||||
if(err) {
|
postData: function(callback) {
|
||||||
return callback(err);
|
posts.getPostData(mainPid, callback);
|
||||||
|
},
|
||||||
|
cid: function(callback) {
|
||||||
|
posts.getCidByPid(mainPid, callback);
|
||||||
}
|
}
|
||||||
|
}, function(err, results) {
|
||||||
posts.getCidByPid(mainPid, function(err, cid) {
|
Topics.create(results.postData.uid, title, results.cid, function(err, tid) {
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
Topics.create(postData.uid, title, cid, function(err, tid) {
|
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
@@ -255,13 +231,15 @@ var async = require('async'),
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
Topics.getTopicData(tid, function(err, topicData) {
|
Topics.getTopicData(tid, callback);
|
||||||
callback(err, topicData);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function move(pid, next) {
|
function move(pid, next) {
|
||||||
postTools.privileges(pid, uid, function(err, privileges) {
|
postTools.privileges(pid, uid, function(err, privileges) {
|
||||||
|
if(err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
if(privileges.editable) {
|
if(privileges.editable) {
|
||||||
Topics.movePostToTopic(pid, tid, next);
|
Topics.movePostToTopic(pid, tid, next);
|
||||||
} else {
|
} else {
|
||||||
@@ -271,7 +249,6 @@ var async = require('async'),
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Topics.movePostToTopic = function(pid, tid, callback) {
|
Topics.movePostToTopic = function(pid, tid, callback) {
|
||||||
@@ -418,6 +395,10 @@ var async = require('async'),
|
|||||||
|
|
||||||
Topics.getCategoryData = function(tid, callback) {
|
Topics.getCategoryData = function(tid, callback) {
|
||||||
Topics.getTopicField(tid, 'cid', function(err, cid) {
|
Topics.getTopicField(tid, 'cid', function(err, cid) {
|
||||||
|
if(err) {
|
||||||
|
callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
categories.getCategoryData(cid, callback);
|
categories.getCategoryData(cid, callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -838,15 +819,12 @@ var async = require('async'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getTeaser(next) {
|
function getTeaser(next) {
|
||||||
Topics.getTeaser(tid, function(err, teaser) {
|
Topics.getTeaser(tid, next);
|
||||||
if (err) teaser = {};
|
|
||||||
next(null, teaser);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel([getTopicData, getReadStatus, getTeaser], function(err, results) {
|
async.parallel([getTopicData, getReadStatus, getTeaser], function(err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw new Error(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var topicData = results[0],
|
var topicData = results[0],
|
||||||
@@ -863,7 +841,7 @@ var async = require('async'),
|
|||||||
topicData.teaser_timestamp = utils.toISOString(teaser.timestamp);
|
topicData.teaser_timestamp = utils.toISOString(teaser.timestamp);
|
||||||
topicData.teaser_userpicture = teaser.picture;
|
topicData.teaser_userpicture = teaser.picture;
|
||||||
|
|
||||||
callback(topicData);
|
callback(null, topicData);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1151,33 +1129,20 @@ var async = require('async'),
|
|||||||
Topics.reIndexTopic = function(tid, callback) {
|
Topics.reIndexTopic = function(tid, callback) {
|
||||||
Topics.getPids(tid, function(err, pids) {
|
Topics.getPids(tid, function(err, pids) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
return callback(err);
|
||||||
} else {
|
|
||||||
posts.reIndexPids(pids, function(err) {
|
|
||||||
if (err) {
|
|
||||||
callback(err);
|
|
||||||
} else {
|
|
||||||
callback(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
posts.reIndexPids(pids, callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Topics.reIndexAll = function(callback) {
|
Topics.reIndexAll = function(callback) {
|
||||||
db.getSetMembers('topics:tid', function(err, tids) {
|
db.getSetMembers('topics:tid', function(err, tids) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err, null);
|
return callback(err);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
async.each(tids, Topics.reIndexTopic, function(err) {
|
async.each(tids, Topics.reIndexTopic, callback);
|
||||||
if (err) {
|
|
||||||
callback(err, null);
|
|
||||||
} else {
|
|
||||||
callback(null, 'All topics reindexed.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
17
src/user.js
17
src/user.js
@@ -348,6 +348,23 @@ var bcrypt = require('bcrypt'),
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
User.isReadyToPost = function(uid, callback) {
|
||||||
|
User.getUserField(uid, 'lastposttime', function(err, lastposttime) {
|
||||||
|
if(err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!lastposttime) {
|
||||||
|
lastposttime = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Date.now() - parseInt(lastposttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) {
|
||||||
|
return callback(new Error('too-many-posts'));
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
User.isEmailAvailable = function(email, callback) {
|
User.isEmailAvailable = function(email, callback) {
|
||||||
db.isObjectField('email:uid', email, function(err, exists) {
|
db.isObjectField('email:uid', email, function(err, exists) {
|
||||||
callback(err, !exists);
|
callback(err, !exists);
|
||||||
|
|||||||
Reference in New Issue
Block a user