@ -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