From 19b7cdb245524f5c4d0a789e4b2013560a9bae70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 6 Sep 2023 11:00:34 -0400 Subject: [PATCH] fix: #11981, post immediately when canceling scheduling --- src/posts/edit.js | 2 +- src/topics/scheduled.js | 46 ++++++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/posts/edit.js b/src/posts/edit.js index c75f7f2379..fed05baa63 100644 --- a/src/posts/edit.js +++ b/src/posts/edit.js @@ -186,7 +186,7 @@ module.exports = function (Posts) { 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())) { + if (isMain && isNaN(data.timestamp)) { throw new Error('[[error:invalid-data]]'); } } diff --git a/src/topics/scheduled.js b/src/topics/scheduled.js index bd79e1b07c..52d70366dc 100644 --- a/src/topics/scheduled.js +++ b/src/topics/scheduled.js @@ -26,6 +26,11 @@ Scheduled.handleExpired = async function () { return; } + await postTids(tids); + await db.sortedSetsRemoveRangeByScore([`topics:scheduled`], '-inf', now); +}; + +async function postTids(tids) { let topicsData = await topics.getTopicsData(tids); // Filter deleted topicsData = topicsData.filter(topicData => Boolean(topicData)); @@ -43,9 +48,8 @@ Scheduled.handleExpired = async function () { updateUserLastposttimes(uids, topicsData), updateGroupPosts(uids, topicsData), ...topicsData.map(topicData => unpin(topicData.tid, topicData)), - db.sortedSetsRemoveRangeByScore([`topics:scheduled`], '-inf', now) )); -}; +} // topics/tools.js#pin/unpin would block non-admins/mods, thus the local versions Scheduled.pin = async function (tid, topicData) { @@ -62,23 +66,27 @@ Scheduled.pin = async function (tid, topicData) { }; Scheduled.reschedule = async function ({ cid, tid, timestamp, uid }) { - const mainPid = await topics.getTopicField(tid, 'mainPid'); - await Promise.all([ - db.sortedSetsAdd([ - 'topics:scheduled', - `uid:${uid}:topics`, - 'topics:tid', - `cid:${cid}:uid:${uid}:tids`, - ], timestamp, tid), - posts.setPostField(mainPid, 'timestamp', timestamp), - db.sortedSetsAdd([ - 'posts:pid', - `uid:${uid}:posts`, - `cid:${cid}:uid:${uid}:pids`, - ], timestamp, mainPid), - shiftPostTimes(tid, timestamp), - ]); - return topics.updateLastPostTimeFromLastPid(tid); + if (timestamp < Date.now()) { + await postTids([tid]); + } else { + const mainPid = await topics.getTopicField(tid, 'mainPid'); + await Promise.all([ + db.sortedSetsAdd([ + 'topics:scheduled', + `uid:${uid}:topics`, + 'topics:tid', + `cid:${cid}:uid:${uid}:tids`, + ], timestamp, tid), + posts.setPostField(mainPid, 'timestamp', timestamp), + db.sortedSetsAdd([ + 'posts:pid', + `uid:${uid}:posts`, + `cid:${cid}:uid:${uid}:pids`, + ], timestamp, mainPid), + shiftPostTimes(tid, timestamp), + ]); + await topics.updateLastPostTimeFromLastPid(tid); + } }; function unpin(tid, topicData) {