@ -43,7 +43,6 @@ module.exports = async (req, res, next) => {
passwordPosts = res . locals . posts ;
passwordPosts = res . locals . posts ;
passwordPostMongoIds = postMongoIds ;
passwordPostMongoIds = postMongoIds ;
}
}
const messages = [ ] ;
const messages = [ ] ;
const combinedQuery = { } ;
const combinedQuery = { } ;
const passwordCombinedQuery = { } ;
const passwordCombinedQuery = { } ;
@ -76,14 +75,25 @@ module.exports = async (req, res, next) => {
const deleteIpPosts = await Posts . db . find ( query ) . toArray ( ) ;
const deleteIpPosts = await Posts . db . find ( query ) . toArray ( ) ;
res . locals . posts = res . locals . posts . concat ( deleteIpPosts ) ;
res . locals . posts = res . locals . posts . concat ( deleteIpPosts ) ;
if ( deleteIpPosts && deleteIpPosts . length > 0 ) {
if ( deleteIpPosts && deleteIpPosts . length > 0 ) {
const { message } = await deletePosts ( req , res , next , deleteIpPosts , req . params . board ) ;
const { action , message } = await deletePosts ( req , res , next , deleteIpPosts , req . params . board ) ;
messages . push ( message ) ;
messages . push ( message ) ;
if ( action ) {
aggregateNeeded = true ;
aggregateNeeded = true ;
} else {
req . body . delete _uiip _board = false ;
req . body . delete _ip _global = false ;
res . locals . actions . anyValid -- ;
}
}
}
} else if ( req . body . delete ) {
} else if ( req . body . delete ) {
const { message } = await deletePosts ( req , res , next , passwordPosts , req . params . board ) ;
const { action , message } = await deletePosts ( req , res , next , passwordPosts , req . params . board ) ;
messages . push ( message ) ;
messages . push ( message ) ;
if ( action ) {
aggregateNeeded = true ;
aggregateNeeded = true ;
} else {
req . body . delete = false ;
res . locals . actions . anyValid -- ;
}
} else {
} else {
// if it was getting deleted, we cant do any of these
// if it was getting deleted, we cant do any of these
if ( req . body . delete _file || req . body . unlink _file ) {
if ( req . body . delete _file || req . body . unlink _file ) {
@ -91,12 +101,19 @@ module.exports = async (req, res, next) => {
if ( action ) {
if ( action ) {
aggregateNeeded = true ;
aggregateNeeded = true ;
passwordCombinedQuery [ action ] = { ... passwordCombinedQuery [ action ] , ... query }
passwordCombinedQuery [ action ] = { ... passwordCombinedQuery [ action ] , ... query }
} else {
req . body . delete _file = false ;
req . body . unlink _file = false ;
res . locals . actions . anyValid -- ;
}
}
messages . push ( message ) ;
messages . push ( message ) ;
} else if ( req . body . spoiler ) {
} else if ( req . body . spoiler ) {
const { message , action , query } = spoilerPosts ( passwordPosts ) ;
const { message , action , query } = spoilerPosts ( passwordPosts ) ;
if ( action ) {
if ( action ) {
passwordCombinedQuery [ action ] = { ... passwordCombinedQuery [ action ] , ... query }
passwordCombinedQuery [ action ] = { ... passwordCombinedQuery [ action ] , ... query }
} else {
req . body . spoiler = false ;
res . locals . actions . anyValid -- ;
}
}
messages . push ( message ) ;
messages . push ( message ) ;
}
}
@ -105,6 +122,9 @@ module.exports = async (req, res, next) => {
const { message , action , query } = sagePosts ( res . locals . posts ) ;
const { message , action , query } = sagePosts ( res . locals . posts ) ;
if ( action ) {
if ( action ) {
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
} else {
req . body . sage = false ;
res . locals . actions . anyValid -- ;
}
}
messages . push ( message ) ;
messages . push ( message ) ;
}
}
@ -112,6 +132,9 @@ module.exports = async (req, res, next) => {
const { message , action , query } = lockPosts ( res . locals . posts ) ;
const { message , action , query } = lockPosts ( res . locals . posts ) ;
if ( action ) {
if ( action ) {
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
} else {
req . body . lock = false ;
res . locals . actions . anyValid -- ;
}
}
messages . push ( message ) ;
messages . push ( message ) ;
}
}
@ -119,6 +142,9 @@ module.exports = async (req, res, next) => {
const { message , action , query } = stickyPosts ( res . locals . posts ) ;
const { message , action , query } = stickyPosts ( res . locals . posts ) ;
if ( action ) {
if ( action ) {
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
} else {
req . body . sticky = false ;
res . locals . actions . anyValid -- ;
}
}
messages . push ( message ) ;
messages . push ( message ) ;
}
}
@ -133,6 +159,9 @@ module.exports = async (req, res, next) => {
const { message , action , query } = dismissReports ( res . locals . posts ) ;
const { message , action , query } = dismissReports ( res . locals . posts ) ;
if ( action ) {
if ( action ) {
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
} else {
req . body . dismiss = false ;
res . locals . actions . anyValid -- ;
}
}
messages . push ( message ) ;
messages . push ( message ) ;
}
}
@ -147,11 +176,15 @@ module.exports = async (req, res, next) => {
const { message , action , query } = dismissGlobalReports ( res . locals . posts ) ;
const { message , action , query } = dismissGlobalReports ( res . locals . posts ) ;
if ( action ) {
if ( action ) {
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
combinedQuery [ action ] = { ... combinedQuery [ action ] , ... query }
} else {
req . body . global _dismiss = false ;
res . locals . actions . anyValid -- ;
}
}
messages . push ( message ) ;
messages . push ( message ) ;
}
}
}
}
const bulkWrites = [ ]
if ( res . locals . actions . anyValid > 0 ) {
const bulkWrites = [ ] ;
if ( Object . keys ( combinedQuery ) . length > 0 ) {
if ( Object . keys ( combinedQuery ) . length > 0 ) {
bulkWrites . push ( {
bulkWrites . push ( {
'updateMany' : {
'updateMany' : {
@ -185,6 +218,10 @@ module.exports = async (req, res, next) => {
if ( ! boardThreadMap [ post . board ] ) {
if ( ! boardThreadMap [ post . board ] ) {
boardThreadMap [ post . board ] = [ ] ;
boardThreadMap [ post . board ] = [ ] ;
}
}
if ( ! post . thread ) {
//a thread was directly selected on this board, not just posts. so we handle deletes differently
boardThreadMap [ post . board ] [ 'selectedThreads' ] = true ;
}
boardThreadMap [ post . board ] . push ( post . thread || post . postId ) ;
boardThreadMap [ post . board ] . push ( post . thread || post . postId ) ;
}
}
@ -204,10 +241,10 @@ module.exports = async (req, res, next) => {
}
}
//get only posts (so we can use them for thread ids
//get only posts (so we can use them for thread ids
const postThreadsToUpdate = res . locals . posts . filter ( post => post . thread !== null ) ;
const selectedPosts = res . locals . posts . filter ( post => post . thread !== null ) ;
if ( aggregateNeeded ) {
if ( aggregateNeeded ) {
//recalculate replies and image counts
//recalculate replies and image counts
await Promise . all ( postThreadsToUpdate . map ( async ( post ) => {
await Promise . all ( selectedPosts . map ( async ( post ) => {
const replyCounts = await Posts . getReplyCounts ( post . board , post . thread ) ;
const replyCounts = await Posts . getReplyCounts ( post . board , post . thread ) ;
let replyposts = 0 ;
let replyposts = 0 ;
let replyfiles = 0 ;
let replyfiles = 0 ;
@ -237,7 +274,8 @@ module.exports = async (req, res, next) => {
'$or' : queryOrs
'$or' : queryOrs
} ) . toArray ( ) ;
} ) . toArray ( ) ;
//combine it with what we already had
//combine it with what we already had
threadsEachBoard = threadsEachBoard . concat ( res . locals . posts . filter ( post => post . thread === null ) )
const selectedThreads = res . locals . posts . filter ( post => post . thread === null )
threadsEachBoard = threadsEachBoard . concat ( selectedThreads )
//get the oldest and newest thread for each board to determine how to delete
//get the oldest and newest thread for each board to determine how to delete
const threadBounds = threadsEachBoard . reduce ( ( acc , curr ) => {
const threadBounds = threadsEachBoard . reduce ( ( acc , curr ) => {
@ -253,8 +291,10 @@ module.exports = async (req, res, next) => {
return acc ;
return acc ;
} , { } ) ;
} , { } ) ;
//now we need to delete outdated html
//if there are actions that can cause some rebuilding
//TODO: not do this for reports
//TODO: move this check earlier and move the db builkwrite earlier if possible
if ( res . locals . actions . anyBuild > 0 ) {
const parallelPromises = [ ]
const parallelPromises = [ ]
const boardNames = Object . keys ( threadBounds ) ;
const boardNames = Object . keys ( threadBounds ) ;
const buildBoards = { } ;
const buildBoards = { } ;
@ -265,7 +305,8 @@ module.exports = async (req, res, next) => {
} ) . toArray ( ) ;
} ) . toArray ( ) ;
multiBoards . forEach ( board => {
multiBoards . forEach ( board => {
buildBoards [ board . _id ] = board ;
buildBoards [ board . _id ] = board ;
} )
} ) ;
for ( let i = 0 ; i < boardNames . length ; i ++ ) {
for ( let i = 0 ; i < boardNames . length ; i ++ ) {
const boardName = boardNames [ i ] ;
const boardName = boardNames [ i ] ;
const bounds = threadBounds [ boardName ] ;
const bounds = threadBounds [ boardName ] ;
@ -282,10 +323,15 @@ module.exports = async (req, res, next) => {
parallelPromises . push ( buildBoardMultiple ( buildBoards [ boardName ] , 1 , afterPages ) ) ;
parallelPromises . push ( buildBoardMultiple ( buildBoards [ boardName ] , 1 , afterPages ) ) ;
} else {
} else {
const threadPageOldest = await Posts . getThreadPage ( boardName , bounds . oldest ) ;
const threadPageOldest = await Posts . getThreadPage ( boardName , bounds . oldest ) ;
const threadPageNewest = await Posts . getThreadPage ( boardName , bounds . newest ) ;
const threadPageNewest = bounds . oldest . postId === bounds . newest . postId ? threadPageOldest : await Posts . getThreadPage ( boardName , bounds . newest ) ;
if ( req . body . delete || req . body . delete _ip _board || req . body . delete _ip _global ) {
if ( req . body . delete || req . body . delete _ip _board || req . body . delete _ip _global ) {
//rebuild current and older pages for deletes
if ( ! boardThreadMap [ boardName ] . selectedThreads ) {
//onyl deleting posts from threads, so thread order wont change, thus we dont delete all pages after
parallelPromises . push ( buildBoardMultiple ( buildBoards [ boardName ] , threadPageNewest , threadPageOldest ) ) ;
} else {
//deleting threads, so we delete all pages after
parallelPromises . push ( buildBoardMultiple ( buildBoards [ boardName ] , threadPageNewest , afterPages ) ) ;
parallelPromises . push ( buildBoardMultiple ( buildBoards [ boardName ] , threadPageNewest , afterPages ) ) ;
}
} else if ( req . body . sticky ) { //else if -- if deleting, other actions are not executed/irrelevant
} else if ( req . body . sticky ) { //else if -- if deleting, other actions are not executed/irrelevant
//rebuild current and newer pages
//rebuild current and newer pages
parallelPromises . push ( buildBoardMultiple ( buildBoards [ boardName ] , 1 , threadPageOldest ) ) ;
parallelPromises . push ( buildBoardMultiple ( buildBoards [ boardName ] , 1 , threadPageOldest ) ) ;
@ -299,6 +345,8 @@ module.exports = async (req, res, next) => {
}
}
}
}
await Promise . all ( parallelPromises ) ;
await Promise . all ( parallelPromises ) ;
}
}
} catch ( err ) {
} catch ( err ) {
return next ( err ) ;
return next ( err ) ;