Files
NodeBB/src/posts/edit.js

181 lines
4.5 KiB
JavaScript
Raw Normal View History

'use strict';
2016-08-27 14:06:14 +03:00
var async = require('async');
var validator = require('validator');
2017-05-26 01:39:40 -06:00
var _ = require('lodash');
2016-08-27 14:06:14 +03:00
var db = require('../database');
var meta = require('../meta');
2016-08-27 14:06:14 +03:00
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('../utils');
var translator = require('../translator');
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) {
2018-02-16 16:41:06 -05:00
var oldContent; // for diffing purposes
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;
2018-02-16 16:41:06 -05:00
oldContent = postData.content;
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;
2017-06-16 14:25:40 -04:00
2015-09-23 20:20:30 -04:00
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;
2017-06-16 14:25:40 -04:00
Posts.setPostFields(data.pid, postData, next);
},
2018-02-16 16:41:06 -05:00
function (next) {
if (parseInt(meta.config.enablePostHistory || 1, 10) !== 1) {
return setImmediate(next);
}
2018-02-16 16:41:06 -05:00
Posts.diffs.save(data.pid, oldContent, data.content, next);
},
async.apply(Posts.uploads.sync, data.pid),
2017-06-16 14:25:40 -04:00
function (next) {
postData.cid = results.topic.cid;
2016-11-11 17:28:32 +03:00
postData.topic = results.topic;
2018-05-07 12:23:04 -04:00
plugins.fireHook('action:post.edit', { post: _.clone(postData), data: data, uid: data.uid });
2015-09-23 20:20:30 -04:00
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() : '';
2017-05-25 16:40:03 -04:00
var topicData;
var results;
async.waterfall([
function (next) {
async.parallel({
topic: function (next) {
topics.getTopicFields(tid, ['cid', 'title', 'timestamp'], next);
},
isMain: function (next) {
Posts.isMain(data.pid, next);
},
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-25 16:40:03 -04:00
function (_results, next) {
results = _results;
if (!results.isMain) {
return callback(null, {
tid: tid,
cid: results.topic.cid,
isMainPost: false,
renamed: false,
});
}
2017-05-25 16:40:03 -04:00
topicData = {
tid: tid,
2015-06-25 15:15:33 -04:00
cid: results.topic.cid,
2017-05-25 16:40:03 -04:00
uid: postData.uid,
mainPid: data.pid,
};
if (title) {
topicData.title = title;
topicData.slug = tid + '/' + (utils.slugify(title) || 'topic');
}
topicData.thumb = data.thumb || '';
data.tags = data.tags || [];
2017-06-16 14:25:40 -04:00
if (!data.tags.length) {
return next(null, true);
}
privileges.categories.can('topics:tag', topicData.cid, data.uid, next);
},
function (canTag, next) {
if (!canTag) {
return next(new Error('[[error:no-privileges]]'));
}
2017-05-25 16:40:03 -04:00
plugins.fireHook('filter:topic.edit', { req: data.req, topic: topicData, data: data }, next);
},
function (results, next) {
db.setObject('topic:' + tid, results.topic, next);
},
function (next) {
2018-01-16 18:20:57 -05:00
topics.updateTopicTags(tid, data.tags, next);
2017-05-25 16:40:03 -04:00
},
function (next) {
topics.getTopicTagsObjects(tid, next);
},
function (tags, next) {
topicData.tags = data.tags;
topicData.oldTitle = results.topic.title;
topicData.timestamp = results.topic.timestamp;
var renamed = translator.escape(validator.escape(String(title))) !== results.topic.title;
2017-05-25 16:40:03 -04:00
plugins.fireHook('action:topic.edit', { topic: topicData, uid: data.uid });
next(null, {
tid: tid,
cid: topicData.cid,
uid: postData.uid,
title: validator.escape(String(title)),
oldTitle: results.topic.title,
slug: topicData.slug,
isMainPost: true,
renamed: renamed,
2017-05-25 16:40:03 -04:00
tags: tags,
});
2017-05-25 16:40:03 -04:00
},
], callback);
}
};