mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-28 01:26:16 +01:00
feat: #7743, posts module
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var _ = require('lodash');
|
||||
|
||||
var meta = require('../meta');
|
||||
@@ -13,102 +12,73 @@ var groups = require('../groups');
|
||||
var utils = require('../utils');
|
||||
|
||||
module.exports = function (Posts) {
|
||||
Posts.create = function (data, callback) {
|
||||
Posts.create = async function (data) {
|
||||
// This is an internal method, consider using Topics.reply instead
|
||||
var uid = data.uid;
|
||||
var tid = data.tid;
|
||||
var content = data.content.toString();
|
||||
var timestamp = data.timestamp || Date.now();
|
||||
var isMain = data.isMain || false;
|
||||
const uid = data.uid;
|
||||
const tid = data.tid;
|
||||
const content = data.content.toString();
|
||||
const timestamp = data.timestamp || Date.now();
|
||||
const isMain = data.isMain || false;
|
||||
|
||||
if (!uid && parseInt(uid, 10) !== 0) {
|
||||
return callback(new Error('[[error:invalid-uid]]'));
|
||||
throw new Error('[[error:invalid-uid]]');
|
||||
}
|
||||
|
||||
if (data.toPid && !utils.isNumber(data.toPid)) {
|
||||
return callback(new Error('[[error:invalid-pid]]'));
|
||||
throw new Error('[[error:invalid-pid]]');
|
||||
}
|
||||
|
||||
var postData;
|
||||
const pid = await db.incrObjectField('global', 'nextPid');
|
||||
let postData = {
|
||||
pid: pid,
|
||||
uid: uid,
|
||||
tid: tid,
|
||||
content: content,
|
||||
timestamp: timestamp,
|
||||
deleted: 0,
|
||||
};
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.incrObjectField('global', 'nextPid', next);
|
||||
},
|
||||
function (pid, next) {
|
||||
postData = {
|
||||
pid: pid,
|
||||
uid: uid,
|
||||
tid: tid,
|
||||
content: content,
|
||||
timestamp: timestamp,
|
||||
deleted: 0,
|
||||
};
|
||||
if (data.toPid) {
|
||||
postData.toPid = data.toPid;
|
||||
}
|
||||
if (data.ip && meta.config.trackIpPerPost) {
|
||||
postData.ip = data.ip;
|
||||
}
|
||||
if (data.handle && !parseInt(uid, 10)) {
|
||||
postData.handle = data.handle;
|
||||
}
|
||||
|
||||
if (data.toPid) {
|
||||
postData.toPid = data.toPid;
|
||||
}
|
||||
let result = await plugins.fireHook('filter:post.create', { post: postData, data: data });
|
||||
postData = result.post;
|
||||
await db.setObject('post:' + postData.pid, postData);
|
||||
|
||||
if (data.ip && meta.config.trackIpPerPost) {
|
||||
postData.ip = data.ip;
|
||||
}
|
||||
const topicData = await topics.getTopicFields(tid, ['cid', 'pinned']);
|
||||
postData.cid = topicData.cid;
|
||||
|
||||
if (data.handle && !parseInt(uid, 10)) {
|
||||
postData.handle = data.handle;
|
||||
}
|
||||
await Promise.all([
|
||||
db.sortedSetAdd('posts:pid', timestamp, postData.pid),
|
||||
db.incrObjectField('global', 'postCount'),
|
||||
user.onNewPostMade(postData),
|
||||
topics.onNewPostMade(postData),
|
||||
categories.onNewPostMade(topicData.cid, topicData.pinned, postData),
|
||||
groups.onNewPostMade(postData),
|
||||
addReplyTo(postData, timestamp),
|
||||
Posts.uploads.sync(postData.pid),
|
||||
]);
|
||||
|
||||
plugins.fireHook('filter:post.create', { post: postData, data: data }, next);
|
||||
},
|
||||
function (data, next) {
|
||||
postData = data.post;
|
||||
db.setObject('post:' + postData.pid, postData, next);
|
||||
},
|
||||
function (next) {
|
||||
topics.getTopicFields(tid, ['cid', 'pinned'], next);
|
||||
},
|
||||
function (topicData, next) {
|
||||
postData.cid = topicData.cid;
|
||||
async.parallel([
|
||||
function (next) {
|
||||
user.onNewPostMade(postData, next);
|
||||
},
|
||||
function (next) {
|
||||
topics.onNewPostMade(postData, next);
|
||||
},
|
||||
function (next) {
|
||||
categories.onNewPostMade(topicData.cid, topicData.pinned, postData, next);
|
||||
},
|
||||
function (next) {
|
||||
groups.onNewPostMade(postData, next);
|
||||
},
|
||||
function (next) {
|
||||
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),
|
||||
async.apply(db.incrObjectField, 'post:' + postData.toPid, 'replies'),
|
||||
], next);
|
||||
},
|
||||
function (next) {
|
||||
db.incrObjectField('global', 'postCount', next);
|
||||
},
|
||||
async.apply(Posts.uploads.sync, postData.pid),
|
||||
], function (err) {
|
||||
next(err);
|
||||
});
|
||||
},
|
||||
function (next) {
|
||||
plugins.fireHook('filter:post.get', { post: postData, uid: data.uid }, next);
|
||||
},
|
||||
function (data, next) {
|
||||
data.post.isMain = isMain;
|
||||
plugins.fireHook('action:post.save', { post: _.clone(data.post) });
|
||||
next(null, data.post);
|
||||
},
|
||||
], callback);
|
||||
result = await plugins.fireHook('filter:post.get', { post: postData, uid: data.uid });
|
||||
result.post.isMain = isMain;
|
||||
plugins.fireHook('action:post.save', { post: _.clone(result.post) });
|
||||
return result.post;
|
||||
};
|
||||
|
||||
async function addReplyTo(postData, timestamp) {
|
||||
if (!postData.toPid) {
|
||||
return;
|
||||
}
|
||||
await Promise.all([
|
||||
db.sortedSetAdd('pid:' + postData.toPid + ':replies', timestamp, postData.pid),
|
||||
db.incrObjectField('post:' + postData.toPid, 'replies'),
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user