You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.4 KiB
JavaScript

'use strict';
var async = require('async');
var db = require('../database');
var plugins = require('../plugins');
var utils = require('../utils');
const intFields = [
'uid', 'pid', 'tid', 'deleted', 'timestamp',
'upvotes', 'downvotes', 'deleterUid', 'edited',
];
6 years ago
module.exports = function (Posts) {
6 years ago
Posts.getPostsFields = function (pids, fields, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
async.waterfall([
function (next) {
const keys = pids.map(pid => 'post:' + pid);
6 years ago
if (fields.length) {
db.getObjectsFields(keys, fields, next);
} else {
db.getObjects(keys, next);
}
},
6 years ago
function (posts, next) {
plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next);
},
function (data, next) {
data.posts.forEach(post => modifyPost(post, fields));
6 years ago
next(null, Array.isArray(data.posts) ? data.posts : null);
},
], callback);
};
6 years ago
Posts.getPostData = function (pid, callback) {
Posts.getPostsFields([pid], [], function (err, posts) {
callback(err, posts && posts.length ? posts[0] : null);
});
};
Posts.getPostsData = function (pids, callback) {
6 years ago
Posts.getPostsFields(pids, [], callback);
};
Posts.getPostField = function (pid, field, callback) {
6 years ago
Posts.getPostFields(pid, [field], function (err, post) {
callback(err, post ? post[field] : null);
});
};
Posts.getPostFields = function (pid, fields, callback) {
6 years ago
Posts.getPostsFields([pid], fields, function (err, posts) {
callback(err, posts ? posts[0] : null);
});
};
Posts.setPostField = function (pid, field, value, callback) {
6 years ago
Posts.setPostFields(pid, { [field]: value }, 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);
};
};
6 years ago
function modifyPost(post, fields) {
6 years ago
if (post) {
db.parseIntFields(post, intFields, fields);
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) : '';
}
6 years ago
}
}