Skip to content

Commit

Permalink
fix: #11981, post immediately when canceling scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Sep 6, 2023
1 parent d79f79e commit aa797f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/posts/edit.js
Expand Up @@ -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]]');
}
}
Expand Down
46 changes: 27 additions & 19 deletions src/topics/scheduled.js
Expand Up @@ -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));
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit aa797f2

Please sign in to comment.