feat: rescheduling (editing ST) (#9445)

This commit is contained in:
gasoved
2021-03-31 00:51:23 +03:00
committed by GitHub
parent 6f51c46052
commit aa0137b1c4
7 changed files with 139 additions and 36 deletions

View File

@@ -32,7 +32,10 @@ Scheduled.handleExpired = async function () {
// Restore first to be not filtered for being deleted
// Restoring handles "updateRecentTid"
await Promise.all(topicsData.map(topicData => topics.restore(topicData.tid)));
await Promise.all([].concat(
topicsData.map(topicData => topics.restore(topicData.tid)),
topicsData.map(topicData => topics.updateLastPostTimeFromLastPid(topicData.tid))
));
await Promise.all([].concat(
sendNotifications(uids, topicsData),
@@ -55,6 +58,20 @@ Scheduled.pin = async function (tid, topicData) {
]);
};
Scheduled.reschedule = async function ({ cid, tid, timestamp, uid }) {
await Promise.all([
db.sortedSetsAdd([
'topics:scheduled',
`uid:${uid}:topics`,
'topics:tid',
`cid:${cid}:tids`,
`cid:${cid}:uid:${uid}:tids`,
], timestamp, tid),
shiftPostTimes(tid, timestamp),
]);
return topics.updateLastPostTimeFromLastPid(tid);
};
function unpin(tid, topicData) {
return [
topics.setTopicField(tid, 'pinned', 0),
@@ -79,26 +96,32 @@ async function sendNotifications(uids, topicsData) {
postData.topic = topicsData[idx];
});
return topicsData.map(
return Promise.all(topicsData.map(
(t, idx) => user.notifications.sendTopicNotificationToFollowers(t.uid, t, postsData[idx])
).concat(
topicsData.map(
(t, idx) => socketHelpers.notifyNew(t.uid, 'newTopic', { posts: [postsData[idx]], topic: t })
)
);
));
}
async function updateUserLastposttimes(uids, topicsData) {
const lastposttimes = (await user.getUsersFields(uids, ['lastposttime'])).map(u => u.lastposttime);
let timestampByUid = {};
let tstampByUid = {};
topicsData.forEach((tD) => {
timestampByUid[tD.uid] = timestampByUid[tD.uid] ? timestampByUid[tD.uid].concat(tD.timestamp) : [tD.timestamp];
tstampByUid[tD.uid] = tstampByUid[tD.uid] ? tstampByUid[tD.uid].concat(tD.lastposttime) : [tD.lastposttime];
});
timestampByUid = Object.fromEntries(
Object.entries(timestampByUid).filter(uidTimestamp => [uidTimestamp[0], Math.max(...uidTimestamp[1])])
tstampByUid = Object.fromEntries(
Object.entries(tstampByUid).map(uidTimestamp => [uidTimestamp[0], Math.max(...uidTimestamp[1])])
);
const uidsToUpdate = uids.filter((uid, idx) => timestampByUid[uid] > lastposttimes[idx]);
return uidsToUpdate.map(uid => user.setUserField(uid, 'lastposttime', String(timestampByUid[uid])));
const uidsToUpdate = uids.filter((uid, idx) => tstampByUid[uid] > lastposttimes[idx]);
return Promise.all(uidsToUpdate.map(uid => user.setUserField(uid, 'lastposttime', tstampByUid[uid])));
}
async function shiftPostTimes(tid, timestamp) {
const pids = (await posts.getPidsFromSet(`tid:${tid}:posts`, 0, -1, false));
// Leaving other related score values intact, since they reflect post order correctly, and it seems that's good enough
return db.setObjectBulk(pids.map(pid => `post:${pid}`), pids.map((_, idx) => ({ timestamp: timestamp + idx + 1 })));
}