Files
NodeBB/src/posts/data.js

94 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-10-17 15:24:17 -04:00
'use strict';
var async = require('async');
var db = require('../database');
var plugins = require('../plugins');
2018-10-21 19:33:46 -04:00
var utils = require('../utils');
2018-10-17 15:24:17 -04:00
2018-10-21 19:33:46 -04:00
const intFields = [
'uid', 'pid', 'tid', 'deleted', 'timestamp',
'upvotes', 'downvotes', 'deleterUid', 'edited',
];
2018-10-20 16:31:16 -04:00
2018-10-17 15:24:17 -04:00
module.exports = function (Posts) {
2018-10-20 16:31:16 -04:00
Posts.getPostsFields = function (pids, fields, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
2018-10-17 15:24:17 -04:00
async.waterfall([
function (next) {
2018-10-21 19:33:46 -04:00
const keys = pids.map(pid => 'post:' + pid);
2018-10-20 16:31:16 -04:00
if (fields.length) {
db.getObjectsFields(keys, fields, next);
} else {
db.getObjects(keys, next);
}
2018-10-17 15:24:17 -04:00
},
2018-10-20 16:31:16 -04:00
function (posts, next) {
plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next);
2018-10-17 15:24:17 -04:00
},
function (data, next) {
2018-10-20 16:31:16 -04:00
data.posts.forEach(modifyPost);
next(null, Array.isArray(data.posts) ? data.posts : null);
2018-10-17 15:24:17 -04:00
},
], callback);
};
2018-10-20 16:31:16 -04:00
Posts.getPostData = function (pid, callback) {
Posts.getPostsFields([pid], [], function (err, posts) {
callback(err, posts && posts.length ? posts[0] : null);
});
};
2018-10-17 15:24:17 -04:00
Posts.getPostsData = function (pids, callback) {
2018-10-20 16:31:16 -04:00
Posts.getPostsFields(pids, [], callback);
2018-10-17 15:24:17 -04:00
};
Posts.getPostField = function (pid, field, callback) {
2018-10-20 16:31:16 -04:00
Posts.getPostFields(pid, [field], function (err, post) {
callback(err, post ? post[field] : null);
});
2018-10-17 15:24:17 -04:00
};
Posts.getPostFields = function (pid, fields, callback) {
2018-10-20 16:31:16 -04:00
Posts.getPostsFields([pid], fields, function (err, posts) {
callback(err, posts ? posts[0] : null);
2018-10-17 15:24:17 -04:00
});
};
Posts.setPostField = function (pid, field, value, callback) {
2018-10-20 16:31:16 -04:00
Posts.setPostFields(pid, { [field]: value }, callback);
2018-10-17 15:24:17 -04:00
};
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);
};
};
2018-10-20 16:31:16 -04:00
function modifyPost(post) {
if (post) {
intFields.forEach(field => db.parseIntField(post, field));
2018-10-21 19:33:46 -04:00
if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) {
post.votes = post.upvotes - post.downvotes;
}
if (post.hasOwnProperty('timestamp')) {
post.timestampISO = utils.toISOString(post.timestamp);
}
if (post.hasOwnProperty('edited')) {
post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : '';
}
2018-10-20 16:31:16 -04:00
}
}