'use strict'; var async = require('async'), path = require('path'), fs = require('fs'), nconf = require('nconf'), validator = require('validator'), winston = require('winston'), gravatar = require('gravatar'), S = require('string'), db = require('./database'), utils = require('../public/src/utils'), user = require('./user'), groups = require('./groups'), topics = require('./topics'), favourites = require('./favourites'), postTools = require('./postTools'), privileges = require('./privileges'), categories = require('./categories'), plugins = require('./plugins'), meta = require('./meta'), emitter = require('./emitter'), websockets = require('./socket.io'); (function(Posts) { require('./posts/recent')(Posts); require('./posts/delete')(Posts); Posts.create = function(data, callback) { var uid = data.uid, tid = data.tid, content = data.content, timestamp = data.timestamp || Date.now(); if (uid === null) { return callback(new Error('[[error:invalid-uid]]')); } var 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 (data.toPid) { postData.toPid = data.toPid; } plugins.fireHook('filter:post.save', postData, next); }, function(postData, next) { db.setObject('post:' + postData.pid, postData, next); }, function(next) { async.parallel([ function(next) { user.onNewPostMade(postData, next); }, function(next) { topics.onNewPostMade(postData, next); }, function(next) { categories.onNewPostMade(postData, next); }, function(next) { db.sortedSetAdd('posts:pid', timestamp, postData.pid, next); }, function(next) { db.incrObjectField('global', 'postCount', next); } ], function(err) { if (err) { return next(err); } 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, set, start, end, uid, reverse, callback) { Posts.getPidsFromSet(set, start, end, reverse, function(err, pids) { if(err) { return callback(err); } if(!Array.isArray(pids) || !pids.length) { return callback(null, []); } Posts.getPostsByPids(pids, uid, callback); }); }; Posts.getPidsFromSet = function(set, start, end, reverse, callback) { if (isNaN(start) || isNaN(end)) { return callback(null, []); } db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, end, callback); }; Posts.getPostsByPids = function(pids, uid, callback) { var keys = []; for(var x=0, numPids=pids.length; x