mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 09:06:15 +01:00
refactor: post edit
This commit is contained in:
@@ -51,8 +51,7 @@ module.exports = function (Posts) {
|
|||||||
|
|
||||||
Posts.setPostFields = async function (pid, data) {
|
Posts.setPostFields = async function (pid, data) {
|
||||||
await db.setObject('post:' + pid, data);
|
await db.setObject('post:' + pid, data);
|
||||||
data.pid = pid;
|
plugins.fireHook('action:post.setFields', { data: { ...data, pid } });
|
||||||
plugins.fireHook('action:post.setFields', { data: data });
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var validator = require('validator');
|
const validator = require('validator');
|
||||||
var _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
var db = require('../database');
|
const db = require('../database');
|
||||||
var meta = require('../meta');
|
const meta = require('../meta');
|
||||||
var topics = require('../topics');
|
const topics = require('../topics');
|
||||||
var user = require('../user');
|
const user = require('../user');
|
||||||
var privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
var plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
var pubsub = require('../pubsub');
|
const pubsub = require('../pubsub');
|
||||||
var utils = require('../utils');
|
const utils = require('../utils');
|
||||||
var translator = require('../translator');
|
const translator = require('../translator');
|
||||||
|
|
||||||
module.exports = function (Posts) {
|
module.exports = function (Posts) {
|
||||||
pubsub.on('post:edit', function (pid) {
|
pubsub.on('post:edit', function (pid) {
|
||||||
@@ -23,27 +23,35 @@ module.exports = function (Posts) {
|
|||||||
if (!canEdit.flag) {
|
if (!canEdit.flag) {
|
||||||
throw new Error(canEdit.message);
|
throw new Error(canEdit.message);
|
||||||
}
|
}
|
||||||
let postData = await db.getObject('post:' + data.pid);
|
const postData = await Posts.getPostData(data.pid);
|
||||||
if (!postData) {
|
if (!postData) {
|
||||||
throw new Error('[[error:no-post]]');
|
throw new Error('[[error:no-post]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldContent = postData.content; // for diffing purposes
|
const oldContent = postData.content; // for diffing purposes
|
||||||
postData.content = data.content;
|
const now = Date.now();
|
||||||
postData.edited = Date.now();
|
const editPostData = {
|
||||||
postData.editor = data.uid;
|
content: data.content,
|
||||||
|
edited: now,
|
||||||
|
editor: data.uid,
|
||||||
|
};
|
||||||
if (data.handle) {
|
if (data.handle) {
|
||||||
postData.handle = data.handle;
|
editPostData.handle = data.handle;
|
||||||
}
|
}
|
||||||
const result = await plugins.fireHook('filter:post.edit', { req: data.req, post: postData, data: data, uid: data.uid });
|
|
||||||
postData = result.post;
|
const result = await plugins.fireHook('filter:post.edit', {
|
||||||
|
req: data.req,
|
||||||
|
post: editPostData,
|
||||||
|
data: data,
|
||||||
|
uid: data.uid,
|
||||||
|
});
|
||||||
|
|
||||||
const [editor, topic] = await Promise.all([
|
const [editor, topic] = await Promise.all([
|
||||||
user.getUserFields(data.uid, ['username', 'userslug']),
|
user.getUserFields(data.uid, ['username', 'userslug']),
|
||||||
editMainPost(data, postData),
|
editMainPost(data, postData),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await Posts.setPostFields(data.pid, postData);
|
await Posts.setPostFields(data.pid, result.post);
|
||||||
|
|
||||||
if (meta.config.enablePostHistory === 1) {
|
if (meta.config.enablePostHistory === 1) {
|
||||||
await Posts.diffs.save({
|
await Posts.diffs.save({
|
||||||
@@ -54,27 +62,28 @@ module.exports = function (Posts) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
await Posts.uploads.sync(data.pid);
|
await Posts.uploads.sync(data.pid);
|
||||||
|
const returnPostData = { ...postData, ...editPostData };
|
||||||
|
returnPostData.cid = topic.cid;
|
||||||
|
returnPostData.topic = topic;
|
||||||
|
returnPostData.editedISO = utils.toISOString(now);
|
||||||
|
|
||||||
postData.cid = topic.cid;
|
await topics.notifyFollowers(returnPostData, data.uid, {
|
||||||
postData.topic = topic;
|
|
||||||
|
|
||||||
await topics.notifyFollowers(postData, data.uid, {
|
|
||||||
type: 'post-edit',
|
type: 'post-edit',
|
||||||
bodyShort: translator.compile('notifications:user_edited_post', editor.username, postData.topic.title),
|
bodyShort: translator.compile('notifications:user_edited_post', editor.username, topic.title),
|
||||||
nid: 'edit_post:' + postData.pid + ':uid:' + data.uid,
|
nid: 'edit_post:' + data.pid + ':uid:' + data.uid,
|
||||||
});
|
});
|
||||||
|
|
||||||
plugins.fireHook('action:post.edit', { post: _.clone(postData), data: data, uid: data.uid });
|
plugins.fireHook('action:post.edit', { post: _.clone(returnPostData), data: data, uid: data.uid });
|
||||||
|
|
||||||
require('./cache').del(String(postData.pid));
|
require('./cache').del(String(postData.pid));
|
||||||
pubsub.publish('post:edit', String(postData.pid));
|
pubsub.publish('post:edit', String(postData.pid));
|
||||||
|
|
||||||
postData = await Posts.parsePost(postData);
|
await Posts.parsePost(returnPostData);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
topic: topic,
|
topic: topic,
|
||||||
editor: editor,
|
editor: editor,
|
||||||
post: postData,
|
post: returnPostData,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -119,7 +128,11 @@ module.exports = function (Posts) {
|
|||||||
}
|
}
|
||||||
await topics.validateTags(data.tags, topicData.cid);
|
await topics.validateTags(data.tags, topicData.cid);
|
||||||
|
|
||||||
const results = await plugins.fireHook('filter:topic.edit', { req: data.req, topic: newTopicData, data: data });
|
const results = await plugins.fireHook('filter:topic.edit', {
|
||||||
|
req: data.req,
|
||||||
|
topic: newTopicData,
|
||||||
|
data: data,
|
||||||
|
});
|
||||||
await db.setObject('topic:' + tid, results.topic);
|
await db.setObject('topic:' + tid, results.topic);
|
||||||
await topics.updateTopicTags(tid, data.tags);
|
await topics.updateTopicTags(tid, data.tags);
|
||||||
const tags = await topics.getTopicTagsObjects(tid);
|
const tags = await topics.getTopicTagsObjects(tid);
|
||||||
|
|||||||
Reference in New Issue
Block a user