From 2720a692cf98ea02991c45c8885690dd009435bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 28 Apr 2023 10:10:34 -0400 Subject: [PATCH] fix: #11530, fix topic rescheduling don't display scheduled posts in group page when topic is rescheduled update post sorted sets with new timestamp when post is published update group posts zset fix markTopicRead if topic was read while it was still hidden --- src/groups/posts.js | 6 +++--- src/topics/scheduled.js | 17 +++++++++++++++++ src/topics/unread.js | 6 ++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/groups/posts.js b/src/groups/posts.js index b378f66be6..7e834ab773 100644 --- a/src/groups/posts.js +++ b/src/groups/posts.js @@ -6,7 +6,7 @@ const posts = require('../posts'); module.exports = function (Groups) { Groups.onNewPostMade = async function (postData) { - if (!parseInt(postData.uid, 10)) { + if (!parseInt(postData.uid, 10) || postData.timestamp > Date.now()) { return; } @@ -26,7 +26,7 @@ module.exports = function (Groups) { }; async function truncateMemberPosts(groupName) { - let lastPid = await db.getSortedSetRevRange(`group:${groupName}:member:pids`, 10, 10); + let lastPid = await db.getSortedSetRevRangeByScore(`group:${groupName}:member:pids`, 10, 1, Date.now(), '-inf'); lastPid = lastPid[0]; if (!parseInt(lastPid, 10)) { return; @@ -37,7 +37,7 @@ module.exports = function (Groups) { Groups.getLatestMemberPosts = async function (groupName, max, uid) { const [allPids, groupData] = await Promise.all([ - db.getSortedSetRevRange(`group:${groupName}:member:pids`, 0, max - 1), + db.getSortedSetRevRangeByScore(`group:${groupName}:member:pids`, 0, max, Date.now(), '-inf'), Groups.getGroupFields(groupName, ['memberPostCids']), ]); const cids = groupData.memberPostCidsArray; diff --git a/src/topics/scheduled.js b/src/topics/scheduled.js index a386de8869..bd79e1b07c 100644 --- a/src/topics/scheduled.js +++ b/src/topics/scheduled.js @@ -8,6 +8,7 @@ const db = require('../database'); const posts = require('../posts'); const socketHelpers = require('../socket.io/helpers'); const topics = require('./index'); +const groups = require('../groups'); const user = require('../user'); const Scheduled = module.exports; @@ -40,6 +41,7 @@ Scheduled.handleExpired = async function () { await Promise.all([].concat( sendNotifications(uids, topicsData), updateUserLastposttimes(uids, topicsData), + updateGroupPosts(uids, topicsData), ...topicsData.map(topicData => unpin(topicData.tid, topicData)), db.sortedSetsRemoveRangeByScore([`topics:scheduled`], '-inf', now) )); @@ -69,6 +71,11 @@ Scheduled.reschedule = async function ({ cid, tid, timestamp, uid }) { `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); @@ -124,6 +131,16 @@ async function updateUserLastposttimes(uids, topicsData) { return Promise.all(uidsToUpdate.map(uid => user.setUserField(uid, 'lastposttime', tstampByUid[uid]))); } +async function updateGroupPosts(uids, topicsData) { + const postsData = await posts.getPostsData(topicsData.map(t => t && t.mainPid)); + await Promise.all(postsData.map(async (post, i) => { + if (topicsData[i]) { + post.cid = topicsData[i].cid; + await groups.onNewPostMade(post); + } + })); +} + 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 diff --git a/src/topics/unread.js b/src/topics/unread.js index 8bff23380a..63563525d5 100644 --- a/src/topics/unread.js +++ b/src/topics/unread.js @@ -291,14 +291,16 @@ module.exports = function (Topics) { db.sortedSetScores(`uid:${uid}:tids_read`, tids), ]); - const topics = topicScores.filter((t, i) => t.lastposttime && (!userScores[i] || userScores[i] < t.lastposttime)); + const now = Date.now(); + const topics = topicScores.filter( + (t, i) => t.lastposttime && (!userScores[i] || userScores[i] < t.lastposttime || userScores[i] > now) + ); tids = topics.map(t => t.tid); if (!tids.length) { return false; } - const now = Date.now(); const scores = topics.map(topic => (topic.scheduled ? topic.lastposttime : now)); const [topicData] = await Promise.all([ Topics.getTopicsFields(tids, ['cid']),