From 92519122823b3fdb1dccf18702437d283f26bffe Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Wed, 17 Oct 2018 15:24:17 -0400 Subject: [PATCH] move methods from posts to posts/data --- src/posts.js | 107 +------------------------------------------ src/posts/data.js | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 106 deletions(-) create mode 100644 src/posts/data.js diff --git a/src/posts.js b/src/posts.js index 080a143f94..8390653e69 100644 --- a/src/posts.js +++ b/src/posts.js @@ -12,6 +12,7 @@ var plugins = require('./plugins'); var Posts = module.exports; +require('./posts/data')(Posts); require('./posts/create')(Posts); require('./posts/delete')(Posts); require('./posts/edit')(Posts); @@ -95,112 +96,6 @@ Posts.getPostSummariesFromSet = function (set, uid, start, stop, callback) { ], callback); }; -Posts.getPostData = function (pid, callback) { - async.waterfall([ - function (next) { - db.getObject('post:' + pid, next); - }, - function (data, next) { - plugins.fireHook('filter:post.getPostData', { post: data }, next); - }, - function (data, next) { - next(null, data.post); - }, - ], callback); -}; - -Posts.getPostsData = function (pids, callback) { - async.waterfall([ - function (next) { - db.getObjects(pids.map(pid => 'post:' + pid), next); - }, - function (data, next) { - plugins.fireHook('filter:post.getPostsData', { posts: data }, next); - }, - function (data, next) { - next(null, data.posts); - }, - ], callback); -}; - -Posts.getPostField = function (pid, field, callback) { - async.waterfall([ - function (next) { - Posts.getPostFields(pid, [field], next); - }, - function (data, next) { - next(null, data[field]); - }, - ], callback); -}; - -Posts.getPostFields = function (pid, fields, callback) { - async.waterfall([ - function (next) { - db.getObjectFields('post:' + pid, fields, next); - }, - function (data, next) { - data.pid = pid; - - plugins.fireHook('filter:post.getFields', { posts: [data], fields: fields }, next); - }, - function (data, next) { - next(null, (data && Array.isArray(data.posts) && data.posts.length) ? data.posts[0] : null); - }, - ], callback); -}; - -Posts.getPostsFields = function (pids, fields, callback) { - if (!Array.isArray(pids) || !pids.length) { - return callback(null, []); - } - - var keys = pids.map(function (pid) { - return 'post:' + pid; - }); - - async.waterfall([ - function (next) { - db.getObjectsFields(keys, fields, next); - }, - function (posts, next) { - plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next); - }, - function (data, next) { - next(null, (data && Array.isArray(data.posts)) ? data.posts : null); - }, - ], callback); -}; - -Posts.setPostField = function (pid, field, value, callback) { - async.waterfall([ - function (next) { - db.setObjectField('post:' + pid, field, value, next); - }, - function (next) { - var data = { - pid: pid, - }; - data[field] = value; - plugins.fireHook('action:post.setFields', { data: data }); - next(); - }, - ], callback); -}; - -Posts.setPostFields = function (pid, data, callback) { - async.waterfall([ - function (next) { - db.setObject('post:' + pid, data, next); - }, - function (next) { - data.pid = pid; - plugins.fireHook('action:post.setFields', { data: data }); - next(); - }, - ], callback); -}; - Posts.getPidIndex = function (pid, tid, topicPostSort, callback) { async.waterfall([ function (next) { diff --git a/src/posts/data.js b/src/posts/data.js new file mode 100644 index 0000000000..bc7df89f57 --- /dev/null +++ b/src/posts/data.js @@ -0,0 +1,114 @@ +'use strict'; + +var async = require('async'); + +var db = require('../database'); +var plugins = require('../plugins'); + +module.exports = function (Posts) { + Posts.getPostData = function (pid, callback) { + async.waterfall([ + function (next) { + db.getObject('post:' + pid, next); + }, + function (data, next) { + plugins.fireHook('filter:post.getPostData', { post: data }, next); + }, + function (data, next) { + next(null, data.post); + }, + ], callback); + }; + + Posts.getPostsData = function (pids, callback) { + async.waterfall([ + function (next) { + db.getObjects(pids.map(pid => 'post:' + pid), next); + }, + function (data, next) { + plugins.fireHook('filter:post.getPostsData', { posts: data }, next); + }, + function (data, next) { + next(null, data.posts); + }, + ], callback); + }; + + Posts.getPostField = function (pid, field, callback) { + async.waterfall([ + function (next) { + Posts.getPostFields(pid, [field], next); + }, + function (data, next) { + next(null, data[field]); + }, + ], callback); + }; + + Posts.getPostFields = function (pid, fields, callback) { + async.waterfall([ + function (next) { + db.getObjectFields('post:' + pid, fields, next); + }, + function (data, next) { + data.pid = pid; + + plugins.fireHook('filter:post.getFields', { posts: [data], fields: fields }, next); + }, + function (data, next) { + next(null, (data && Array.isArray(data.posts) && data.posts.length) ? data.posts[0] : null); + }, + ], callback); + }; + + Posts.getPostsFields = function (pids, fields, callback) { + if (!Array.isArray(pids) || !pids.length) { + return callback(null, []); + } + + var keys = pids.map(function (pid) { + return 'post:' + pid; + }); + + async.waterfall([ + function (next) { + db.getObjectsFields(keys, fields, next); + }, + function (posts, next) { + plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next); + }, + function (data, next) { + next(null, (data && Array.isArray(data.posts)) ? data.posts : null); + }, + ], callback); + }; + + Posts.setPostField = function (pid, field, value, callback) { + async.waterfall([ + function (next) { + db.setObjectField('post:' + pid, field, value, next); + }, + function (next) { + var data = { + pid: pid, + }; + data[field] = value; + plugins.fireHook('action:post.setFields', { data: data }); + next(); + }, + ], callback); + }; + + Posts.setPostFields = function (pid, data, callback) { + async.waterfall([ + function (next) { + db.setObject('post:' + pid, data, next); + }, + function (next) { + data.pid = pid; + plugins.fireHook('action:post.setFields', { data: data }); + next(); + }, + ], callback); + }; +};