var db = require('./database'), utils = require('./../public/src/utils'), user = require('./user'), topics = require('./topics'), favourites = require('./favourites'), postTools = require('./postTools'), categories = require('./categories'), 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(data, callback) { var uid = data.uid, tid = data.tid, content = data.content, toPid = data.toPid; 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', 'votes': '0', 'editor': '', 'edited': 0, 'deleted': 0 }; if (toPid) { postData['toPid'] = toPid; } db.setObject('post:' + pid, postData, function(err) { if(err) { return next(err); } db.sortedSetAdd('posts:pid', timestamp, pid); 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); } postData.content = content; plugins.fireHook('action:post.save', postData); next(null, postData); }); } ], callback); }; Posts.getPostsByTid = function(tid, start, end, reverse, callback) { if (typeof reverse === 'function') { callback = reverse; reverse = false; } db[reverse ? 'getSortedSetRevRange' : '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); require('./file').saveFileToLocal(filename, file.path, function(err, upload) { if(err) { return callback(err); } callback(null, { url: upload.url, name: file.name }); }); } } // this function should really be called User.getFavouritePosts Posts.getFavourites = function(uid, start, end, callback) { db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) { if (err) { return callback(err); } Posts.getPostSummaryByPids(pids, false, function(err, posts) { if(err) { return callback(err); } if(!posts || !posts.length) { return callback(null, { posts: [], nextStart: 0}); } db.sortedSetRevRank('uid:' + uid + ':favourites', posts[posts.length - 1].pid, function(err, rank) { if(err) { return calllback(err); } var favourites = { posts: posts, nextStart: parseInt(rank, 10) + 1 }; callback(null, favourites); }); }); }); } Posts.getPidPage = function(pid, uid, callback) { if(!pid) { return callback(new Error('invalid-pid')); } var index = 0; async.waterfall([ function(next) { Posts.getPostField(pid, 'tid', next); }, function(tid, next) { topics.getPids(tid, next); }, function(pids, next) { index = pids.indexOf(pid.toString()); if(index === -1) { return next(new Error('pid not found')); } next(); }, function(next) { user.getSettings(uid, next); }, function(settings, next) { next(null, Math.ceil((index + 1) / settings.postsPerPage)); } ], callback); }; Posts.getPidIndex = function(pid, callback) { Posts.getPostField(pid, 'tid', function(err, tid) { if(err) { return callback(err); } db.sortedSetRank('tid:' + tid + ':posts', pid, callback); }); } }(exports));