Files
NodeBB/src/posts/create.js

110 lines
2.6 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');
2014-11-09 01:12:24 -05:00
module.exports = function (Posts) {
2016-06-13 14:02:54 +03:00
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();
2014-11-09 01:12:24 -05:00
if (!uid && parseInt(uid, 10) !== 0) {
return callback(new Error('[[error:invalid-uid]]'));
}
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 = {
'pid': pid,
'uid': uid,
'tid': tid,
'content': content,
'timestamp': timestamp,
'deleted': 0
};
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) {
2016-08-27 14:06:14 +03:00
plugins.fireHook('filter:post.create', {post: postData, data: data}, next);
},
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);
}
db.sortedSetAdd('pid:' + postData.toPid + ':replies', timestamp, postData.pid, next);
},
function (next) {
2014-11-09 01:12:24 -05:00
db.incrObjectField('global', 'postCount', next);
}
], function (err) {
2014-11-09 01:12:24 -05:00
if (err) {
return next(err);
}
plugins.fireHook('filter:post.get', postData, next);
});
},
function (postData, next) {
2015-09-23 20:38:08 -04:00
plugins.fireHook('action:post.save', _.clone(postData));
2014-11-09 01:12:24 -05:00
next(null, postData);
}
], callback);
};
};