From 263f479b204da93e78d357b7c0c1bbc01d0172e7 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 9 Nov 2014 01:12:24 -0500 Subject: [PATCH] posts.js refactor pt1 --- src/posts.js | 412 +----------------------------------------- src/posts/category.js | 55 ++++++ src/posts/create.js | 86 +++++++++ src/posts/summary.js | 140 ++++++++++++++ src/posts/user.js | 163 +++++++++++++++++ 5 files changed, 451 insertions(+), 405 deletions(-) create mode 100644 src/posts/category.js create mode 100644 src/posts/create.js create mode 100644 src/posts/summary.js create mode 100644 src/posts/user.js diff --git a/src/posts.js b/src/posts.js index c5756610fd..ddb87d5a18 100644 --- a/src/posts.js +++ b/src/posts.js @@ -1,107 +1,25 @@ 'use strict'; var async = require('async'), - path = require('path'), - fs = require('fs'), - nconf = require('nconf'), _ = require('underscore'), - 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'); + plugins = require('./plugins'); (function(Posts) { - require('./posts/recent')(Posts); + + require('./posts/create')(Posts); require('./posts/delete')(Posts); + require('./posts/user')(Posts); + require('./posts/category')(Posts); + require('./posts/summary')(Posts); + require('./posts/recent')(Posts); require('./posts/flags')(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) { - plugins.fireHook('action:post.save', postData); - next(null, postData); - } - ], callback); - }; - Posts.exists = function(pid, callback) { db.isSortedSetMember('posts:pid', pid, callback); }; @@ -167,184 +85,6 @@ var async = require('async'), }); }; - Posts.getUserInfoForPosts = function(uids, uid, callback) { - async.parallel({ - groups: function(next) { - groups.getUserGroups(uids, next); - }, - userData: function(next) { - user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status'], next); - }, - online: function(next) { - websockets.isUsersOnline(uids, next); - } - }, function(err, results) { - if (err) { - return callback(err); - } - - var userData = results.userData; - for(var i=0; i