var db = require('./database'), utils = require('./../public/src/utils'), user = require('./user'), topics = require('./topics'), categories = require('./categories'), favourites = require('./favourites'), threadTools = require('./threadTools'), postTools = require('./postTools'), categories = require('./categories'), feed = require('./feed'), plugins = require('./plugins'), meta = require('./meta'), async = require('async'), path = require('path'), fs = require('fs'), nconf = require('nconf'), validator = require('validator'), winston = require('winston'), gravatar = require('gravatar'), S = require('string'); (function(Posts) { var customUserInfo = {}; Posts.create = function(uid, tid, content, callback) { if (uid === null) { return callback(new Error('invalid-user'), null); } async.waterfall([ function(next) { topics.isLocked(tid, next); }, function(locked, next) { if(locked) { return next(new Error('topic-locked')); } db.incrObjectField('global', 'nextPid', next); }, function(pid, next) { plugins.fireHook('filter:post.save', content, function(err, newContent) { next(err, pid, newContent) }); }, function(pid, newContent, next) { var timestamp = Date.now(), postData = { 'pid': pid, 'uid': uid, 'tid': tid, 'content': newContent, 'timestamp': timestamp, 'reputation': 0, 'editor': '', 'edited': 0, 'deleted': 0 }; db.setObject('post:' + pid, postData, function(err) { if(err) { return next(err); } db.incrObjectField('global', 'postCount'); topics.onNewPostMade(tid, pid, timestamp); categories.onNewPostMade(uid, tid, pid, timestamp); user.onNewPostMade(uid, tid, pid, timestamp); next(null, postData); }); }, function(postData, next) { plugins.fireHook('filter:post.get', postData, next); }, function(postData, next) { postTools.parse(postData.content, function(err, content) { if(err) { return next(err, null); } postData.content = content; plugins.fireHook('action:post.save', postData); db.searchIndex('post', content, postData.pid); next(null, postData); }); } ], callback); }; Posts.getPostsByTid = function(tid, start, end, callback) { db.getSortedSetRange('tid:' + tid + ':posts', start, end, function(err, pids) { if(err) { return callback(err); } if(!pids.length) { return callback(null, []); } plugins.fireHook('filter:post.getTopic', pids, function(err, posts) { if(err) { return callback(err); } if(!posts.length) { return callback(null, []); } Posts.getPostsByPids(pids, function(err, posts) { if(err) { return callback(err); } plugins.fireHook('action:post.gotTopic', posts); callback(null, posts); }); }); }); }; Posts.getPostsByPids = function(pids, callback) { var keys = []; for(var x=0, numPids=pids.length; x parseInt(meta.config.maximumFileSize, 10) * 1024) { return callback(new Error('File too big')); } var filename = 'upload-' + utils.generateUUID() + path.extname(file.name); var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename); fs.writeFile(uploadPath, buffer, function (err) { if(err) { return callback(err); } callback(null, { url: nconf.get('upload_url') + filename, name: file.name }); }); } Posts.reIndexPids = function(pids, callback) { function reIndex(pid, next) { Posts.getPostField(pid, 'content', function(err, content) { if(err) { return next(err); } db.searchRemove('post', pid, function() { if (content && content.length) { db.searchIndex('post', content, pid); } next(); }); }); } async.each(pids, reIndex, callback); } Posts.getFavourites = function(uid, callback) { db.getSortedSetRevRange('uid:' + uid + ':favourites', 0, -1, function(err, pids) { if (err) { return callback(err, null); } Posts.getPostSummaryByPids(pids, false, callback); }); } }(exports));