move methods from posts to posts/data

v1.18.x
Baris Usakli 6 years ago
parent baf91089a6
commit 9251912282

@ -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) {

@ -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);
};
};
Loading…
Cancel
Save