'use strict'; 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')); } var timestamp = Date.now(), postData; async.waterfall([ function(next) { db.incrObjectField('global', 'nextPid', next); }, function(pid, next) { postData = { 'pid': pid, 'uid': uid, 'tid': tid, 'content': content, 'timestamp': timestamp, 'reputation': 0, 'votes': 0, 'editor': '', 'edited': 0, 'deleted': 0 }; if (toPid) { postData.toPid = toPid; } plugins.fireHook('filter:post.save', postData, next); }, function(postData, next) { db.setObject('post:' + postData.pid, postData, next); }, function(result, next) { db.sortedSetAdd('posts:pid', timestamp, postData.pid); db.incrObjectField('global', 'postCount'); topics.onNewPostMade(tid, postData.pid, timestamp); categories.onNewPostMade(uid, tid, postData.pid, timestamp); user.onNewPostMade(uid, tid, postData.pid, timestamp); plugins.fireHook('filter:post.get', postData, next); }, function(postData, next) { postTools.parse(postData.content, function(err, content) { if(err) { return next(err); } plugins.fireHook('action:post.save', postData); postData.content = content; next(null, postData); }); } ], callback); }; Posts.getPostsByTid = function(tid, start, end, reverse, callback) { db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange']('tid:' + tid + ':posts', start, end, function(err, pids) { if(err) { return callback(err); } if(!Array.isArray(pids) || !pids.length) { return callback(null, []); } Posts.getPostsByPids(pids, function(err, posts) { if(err) { return callback(err); } if(!Array.isArray(posts) || !posts.length) { return callback(null, []); } plugins.fireHook('filter:post.getPosts', {tid: tid, posts: posts}, function(err, data) { if(err) { return callback(err); } if(!data || !Array.isArray(data.posts)) { return callback(null, []); } callback(null, data.posts); }); }); }); }; Posts.getPostsByPids = function(pids, callback) { var keys = []; for(var x=0, numPids=pids.length; x