2014-11-09 01:12:24 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-06-13 14:02:54 +03:00
|
|
|
var async = require('async');
|
2017-05-26 01:39:40 -06:00
|
|
|
var _ = require('lodash');
|
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');
|
2017-05-04 16:32:51 -04:00
|
|
|
var groups = require('../groups');
|
2017-04-08 20:22:21 -06:00
|
|
|
var utils = require('../utils');
|
2014-11-09 01:12:24 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Posts) {
|
|
|
|
|
Posts.create = function (data, callback) {
|
2015-05-15 09:47:39 -04:00
|
|
|
// 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]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 16:00:39 +03:00
|
|
|
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([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-09 01:12:24 -05:00
|
|
|
db.incrObjectField('global', 'nextPid', next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-21 16:47:51 -04:00
|
|
|
if (data.ip && meta.config.trackIpPerPost) {
|
2014-12-05 13:07:13 -05:00
|
|
|
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-12-31 16:27:35 -05:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
},
|
2016-10-13 11:43:39 +02: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);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-09 01:12:24 -05:00
|
|
|
async.parallel([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-09 01:12:24 -05:00
|
|
|
user.onNewPostMade(postData, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-09 01:12:24 -05:00
|
|
|
topics.onNewPostMade(postData, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
},
|
2017-05-04 16:32:51 -04:00
|
|
|
function (next) {
|
|
|
|
|
groups.onNewPostMade(postData, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-09 01:12:24 -05:00
|
|
|
db.sortedSetAdd('posts:pid', timestamp, postData.pid, next);
|
|
|
|
|
},
|
2016-09-24 14:43:48 -05:00
|
|
|
function (next) {
|
|
|
|
|
if (!postData.toPid) {
|
|
|
|
|
return next(null);
|
|
|
|
|
}
|
2016-10-14 09:58:31 -05:00
|
|
|
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'),
|
2016-10-14 09:58:31 -05:00
|
|
|
], next);
|
2016-09-24 14:43:48 -05:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-09 01:12:24 -05:00
|
|
|
db.incrObjectField('global', 'postCount', next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2018-04-16 15:21:48 -04:00
|
|
|
async.apply(Posts.uploads.sync, postData.pid),
|
2016-10-13 11:43:39 +02:00
|
|
|
], function (err) {
|
2017-05-25 16:40:03 -04:00
|
|
|
next(err);
|
2014-11-09 01:12:24 -05:00
|
|
|
});
|
|
|
|
|
},
|
2017-05-25 16:40:03 -04:00
|
|
|
function (next) {
|
|
|
|
|
plugins.fireHook('filter:post.get', { post: postData, uid: data.uid }, next);
|
|
|
|
|
},
|
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);
|
|
|
|
|
};
|
|
|
|
|
};
|