Files
NodeBB/src/posts/edit.js

156 lines
3.7 KiB
JavaScript
Raw Normal View History

'use strict';
2016-08-27 14:06:14 +03:00
var async = require('async');
var validator = require('validator');
var _ = require('underscore');
var db = require('../database');
var topics = require('../topics');
var user = require('../user');
var privileges = require('../privileges');
var plugins = require('../plugins');
var cache = require('./cache');
var pubsub = require('../pubsub');
var utils = require('../../public/src/utils');
module.exports = function (Posts) {
pubsub.on('post:edit', function (pid) {
2015-07-16 14:07:28 -04:00
cache.del(pid);
});
Posts.edit = function (data, callback) {
var postData;
2015-09-23 20:20:30 -04:00
var results;
async.waterfall([
function (next) {
privileges.posts.canEdit(data.pid, data.uid, next);
},
2015-09-23 20:20:30 -04:00
function (canEdit, next) {
2016-08-10 23:55:49 +03:00
if (!canEdit.flag) {
return next(new Error(canEdit.message));
}
Posts.getPostData(data.pid, next);
},
2015-09-23 20:20:30 -04:00
function (_postData, next) {
2015-10-02 16:39:07 -04:00
if (!_postData) {
return next(new Error('[[error:no-post]]'));
}
2016-08-27 14:06:14 +03:00
postData = _postData;
postData.content = data.content;
2016-08-27 14:06:14 +03:00
postData.edited = Date.now();
postData.editor = data.uid;
2016-08-27 14:06:14 +03:00
if (data.handle) {
postData.handle = data.handle;
}
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:post.edit', { req: data.req, post: postData, data: data, uid: data.uid }, next);
},
2015-09-23 20:20:30 -04:00
function (result, next) {
postData = result.post;
2016-08-27 14:06:14 +03:00
Posts.setPostFields(data.pid, postData, next);
2015-09-23 20:20:30 -04:00
},
function (next) {
async.parallel({
editor: function (next) {
2015-09-23 20:20:30 -04:00
user.getUserFields(data.uid, ['username', 'userslug'], next);
},
topic: function (next) {
2015-09-23 20:20:30 -04:00
editMainPost(data, postData, next);
2017-02-17 19:31:21 -07:00
},
2015-09-23 20:20:30 -04:00
}, next);
},
function (_results, next) {
results = _results;
postData.cid = results.topic.cid;
2016-11-11 17:28:32 +03:00
postData.topic = results.topic;
2015-09-23 20:20:30 -04:00
plugins.fireHook('action:post.edit', _.clone(postData));
2016-08-31 21:39:02 +03:00
cache.del(String(postData.pid));
pubsub.publish('post:edit', String(postData.pid));
2015-09-23 20:20:30 -04:00
Posts.parsePost(postData, next);
},
function (postData, next) {
results.post = postData;
next(null, results);
2017-02-17 19:31:21 -07:00
},
2015-09-23 20:20:30 -04:00
], callback);
};
function editMainPost(data, postData, callback) {
var tid = postData.tid;
var title = data.title ? data.title.trim() : '';
async.parallel({
topic: function (next) {
2015-06-25 15:15:33 -04:00
topics.getTopicFields(tid, ['cid', 'title'], next);
},
isMain: function (next) {
Posts.isMain(data.pid, next);
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
if (err) {
return callback(err);
}
if (!results.isMain) {
return callback(null, {
tid: tid,
2015-06-25 15:15:33 -04:00
cid: results.topic.cid,
isMainPost: false,
2017-02-17 19:31:21 -07:00
renamed: false,
});
}
var topicData = {
tid: tid,
2015-06-25 15:15:33 -04:00
cid: results.topic.cid,
uid: postData.uid,
2017-02-17 19:31:21 -07:00
mainPid: data.pid,
};
if (title) {
topicData.title = title;
2016-03-22 19:51:31 -05:00
topicData.slug = tid + '/' + (utils.slugify(title) || 'topic');
}
2016-08-27 14:06:14 +03:00
topicData.thumb = data.thumb || '';
data.tags = data.tags || [];
async.waterfall([
function (next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:topic.edit', { req: data.req, topic: topicData, data: data }, next);
2016-08-27 14:06:14 +03:00
},
function (results, next) {
2016-03-09 18:06:05 +02:00
db.setObject('topic:' + tid, results.topic, next);
},
function (next) {
topics.updateTags(tid, data.tags, next);
},
function (next) {
topics.getTopicTagsObjects(tid, next);
},
function (tags, next) {
topicData.tags = data.tags;
2016-10-16 02:52:47 -04:00
topicData.oldTitle = results.topic.title;
plugins.fireHook('action:topic.edit', topicData);
next(null, {
tid: tid,
2015-06-25 15:15:33 -04:00
cid: results.topic.cid,
uid: postData.uid,
title: validator.escape(String(title)),
2015-06-25 15:15:33 -04:00
oldTitle: results.topic.title,
slug: topicData.slug,
2015-06-25 15:15:33 -04:00
isMainPost: true,
renamed: title !== results.topic.title,
2017-02-17 19:31:21 -07:00
tags: tags,
});
2017-02-17 19:31:21 -07:00
},
], callback);
});
}
};