|
|
|
@ -29,15 +29,13 @@ module.exports = function (Posts) {
|
|
|
|
|
throw new Error('[[error:no-post]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const topicData = await topics.getTopicFields(postData.tid, ['cid', 'title', 'timestamp', 'scheduled']);
|
|
|
|
|
const topicData = await topics.getTopicFields(postData.tid, ['cid', 'mainPid', 'title', 'timestamp', 'scheduled', 'slug']);
|
|
|
|
|
|
|
|
|
|
await scheduledTopicCheck(data, topicData);
|
|
|
|
|
|
|
|
|
|
const oldContent = postData.content; // for diffing purposes
|
|
|
|
|
// For posts in scheduled topics, if edited before, use edit timestamp
|
|
|
|
|
const postTimestamp = topicData.scheduled ? (postData.edited || postData.timestamp) + 1 : Date.now();
|
|
|
|
|
const editPostData = {
|
|
|
|
|
content: data.content,
|
|
|
|
|
edited: postTimestamp,
|
|
|
|
|
editor: data.uid,
|
|
|
|
|
};
|
|
|
|
|
const editPostData = getEditPostData(data, topicData, postData);
|
|
|
|
|
|
|
|
|
|
if (data.handle) {
|
|
|
|
|
editPostData.handle = data.handle;
|
|
|
|
|
}
|
|
|
|
@ -62,7 +60,7 @@ module.exports = function (Posts) {
|
|
|
|
|
uid: data.uid,
|
|
|
|
|
oldContent: oldContent,
|
|
|
|
|
newContent: data.content,
|
|
|
|
|
edited: postTimestamp,
|
|
|
|
|
edited: editPostData.edited,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
await Posts.uploads.sync(data.pid);
|
|
|
|
@ -73,7 +71,7 @@ module.exports = function (Posts) {
|
|
|
|
|
const returnPostData = { ...postData, ...result.post };
|
|
|
|
|
returnPostData.cid = topic.cid;
|
|
|
|
|
returnPostData.topic = topic;
|
|
|
|
|
returnPostData.editedISO = utils.toISOString(postTimestamp);
|
|
|
|
|
returnPostData.editedISO = utils.toISOString(editPostData.edited);
|
|
|
|
|
returnPostData.changed = oldContent !== data.content;
|
|
|
|
|
|
|
|
|
|
await topics.notifyFollowers(returnPostData, data.uid, {
|
|
|
|
@ -100,7 +98,7 @@ module.exports = function (Posts) {
|
|
|
|
|
const { tid } = postData;
|
|
|
|
|
const title = data.title ? data.title.trim() : '';
|
|
|
|
|
|
|
|
|
|
const isMain = await Posts.isMain(data.pid);
|
|
|
|
|
const isMain = parseInt(data.pid, 10) === parseInt(topicData.mainPid, 10);
|
|
|
|
|
if (!isMain) {
|
|
|
|
|
return {
|
|
|
|
|
tid: tid,
|
|
|
|
@ -116,6 +114,7 @@ module.exports = function (Posts) {
|
|
|
|
|
cid: topicData.cid,
|
|
|
|
|
uid: postData.uid,
|
|
|
|
|
mainPid: data.pid,
|
|
|
|
|
timestamp: rescheduling(data, topicData) ? data.timestamp : topicData.timestamp,
|
|
|
|
|
};
|
|
|
|
|
if (title) {
|
|
|
|
|
newTopicData.title = title;
|
|
|
|
@ -141,9 +140,12 @@ module.exports = function (Posts) {
|
|
|
|
|
await topics.updateTopicTags(tid, data.tags);
|
|
|
|
|
const tags = await topics.getTopicTagsObjects(tid);
|
|
|
|
|
|
|
|
|
|
if (rescheduling(data, topicData)) {
|
|
|
|
|
await topics.scheduled.reschedule(newTopicData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newTopicData.tags = data.tags;
|
|
|
|
|
newTopicData.oldTitle = topicData.title;
|
|
|
|
|
newTopicData.timestamp = topicData.timestamp;
|
|
|
|
|
const renamed = translator.escape(validator.escape(String(title))) !== topicData.title;
|
|
|
|
|
plugins.hooks.fire('action:topic.edit', { topic: newTopicData, uid: data.uid });
|
|
|
|
|
return {
|
|
|
|
@ -152,10 +154,49 @@ module.exports = function (Posts) {
|
|
|
|
|
uid: postData.uid,
|
|
|
|
|
title: validator.escape(String(title)),
|
|
|
|
|
oldTitle: topicData.title,
|
|
|
|
|
slug: newTopicData.slug,
|
|
|
|
|
slug: newTopicData.slug || topicData.slug,
|
|
|
|
|
isMainPost: true,
|
|
|
|
|
renamed: renamed,
|
|
|
|
|
rescheduled: rescheduling(data, topicData),
|
|
|
|
|
tags: tags,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function scheduledTopicCheck(data, topicData) {
|
|
|
|
|
if (!topicData.scheduled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const canSchedule = await privileges.categories.can('topics:schedule', topicData.cid, data.uid);
|
|
|
|
|
if (!canSchedule) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
|
|
|
|
const isMain = parseInt(data.pid, 10) === parseInt(topicData.mainPid, 10);
|
|
|
|
|
if (isMain && (isNaN(data.timestamp) || data.timestamp < Date.now())) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getEditPostData(data, topicData, postData) {
|
|
|
|
|
const editPostData = {
|
|
|
|
|
content: data.content,
|
|
|
|
|
editor: data.uid,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// For posts in scheduled topics, if edited before, use edit timestamp
|
|
|
|
|
editPostData.edited = topicData.scheduled ? (postData.edited || postData.timestamp) + 1 : Date.now();
|
|
|
|
|
|
|
|
|
|
// if rescheduling the main post
|
|
|
|
|
if (rescheduling(data, topicData)) {
|
|
|
|
|
// For main posts, use timestamp coming from user (otherwise, it is ignored)
|
|
|
|
|
editPostData.edited = data.timestamp;
|
|
|
|
|
editPostData.timestamp = data.timestamp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return editPostData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function rescheduling(data, topicData) {
|
|
|
|
|
const isMain = parseInt(data.pid, 10) === parseInt(topicData.mainPid, 10);
|
|
|
|
|
return isMain && topicData.scheduled && topicData.timestamp !== data.timestamp;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|