Files
NodeBB/src/posts/create.js

116 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-11-09 01:12:24 -05:00
'use strict';
2016-06-13 14:02:54 +03:00
var async = require('async');
var _ = require('underscore');
2014-11-09 01:12:24 -05:00
2016-06-13 14:02:54 +03:00
var meta = require('../meta');
var db = require('../database');
var plugins = require('../plugins');
var user = require('../user');
var topics = require('../topics');
var categories = require('../categories');
var utils = require('../utils');
2014-11-09 01:12:24 -05:00
module.exports = function (Posts) {
Posts.create = function (data, callback) {
// This is an internal method, consider using Topics.reply instead
2016-06-13 14:02:54 +03:00
var uid = data.uid;
var tid = data.tid;
var content = data.content.toString();
var timestamp = data.timestamp || Date.now();
2017-01-16 23:40:25 +00:00
var isMain = data.isMain || false;
2014-11-09 01:12:24 -05:00
if (!uid && parseInt(uid, 10) !== 0) {
return callback(new Error('[[error:invalid-uid]]'));
}
if (data.toPid && !utils.isNumber(data.toPid)) {
return callback(new Error('[[error:invalid-pid]]'));
}
2014-11-09 01:12:24 -05:00
var postData;
async.waterfall([
function (next) {
2014-11-09 01:12:24 -05:00
db.incrObjectField('global', 'nextPid', next);
},
function (pid, next) {
2014-11-09 01:12:24 -05:00
postData = {
2017-02-18 01:19:20 -07:00
pid: pid,
uid: uid,
tid: tid,
content: content,
timestamp: timestamp,
deleted: 0,
2014-11-09 01:12:24 -05:00
};
if (data.toPid) {
postData.toPid = data.toPid;
}
2014-12-05 13:07:13 -05:00
if (data.ip && parseInt(meta.config.trackIpPerPost, 10) === 1) {
postData.ip = data.ip;
}
2016-08-27 14:06:14 +03:00
if (data.handle && !parseInt(uid, 10)) {
2015-01-01 13:05:58 -05:00
postData.handle = data.handle;
}
2014-11-09 01:12:24 -05:00
plugins.fireHook('filter:post.save', postData, next);
},
function (postData, next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:post.create', { post: postData, data: data }, next);
2016-08-27 14:06:14 +03:00
},
function (data, next) {
2016-08-27 14:06:14 +03:00
postData = data.post;
2014-11-09 01:12:24 -05:00
db.setObject('post:' + postData.pid, postData, next);
},
function (next) {
2014-11-09 01:12:24 -05:00
async.parallel([
function (next) {
2014-11-09 01:12:24 -05:00
user.onNewPostMade(postData, next);
},
function (next) {
2014-11-09 01:12:24 -05:00
topics.onNewPostMade(postData, next);
},
function (next) {
topics.getTopicFields(tid, ['cid', 'pinned'], function (err, topicData) {
2015-03-15 01:12:13 -04:00
if (err) {
return next(err);
}
postData.cid = topicData.cid;
categories.onNewPostMade(topicData.cid, topicData.pinned, postData, next);
});
2014-11-09 01:12:24 -05:00
},
function (next) {
2014-11-09 01:12:24 -05:00
db.sortedSetAdd('posts:pid', timestamp, postData.pid, next);
},
function (next) {
if (!postData.toPid) {
return next(null);
}
async.parallel([
async.apply(db.sortedSetAdd, 'pid:' + postData.toPid + ':replies', timestamp, postData.pid),
2017-02-17 19:31:21 -07:00
async.apply(db.incrObjectField, 'post:' + postData.toPid, 'replies'),
], next);
},
function (next) {
2014-11-09 01:12:24 -05:00
db.incrObjectField('global', 'postCount', next);
2017-02-17 19:31:21 -07:00
},
], function (err) {
2014-11-09 01:12:24 -05:00
if (err) {
return next(err);
}
2017-04-19 12:07:37 -04:00
plugins.fireHook('filter:post.get', { post: postData, uid: data.uid }, next);
2014-11-09 01:12:24 -05:00
});
},
2017-04-19 13:09:16 -04:00
function (data, next) {
data.post.isMain = isMain;
plugins.fireHook('action:post.save', { post: _.clone(data.post) });
next(null, data.post);
2017-02-17 19:31:21 -07:00
},
2014-11-09 01:12:24 -05:00
], callback);
};
};