@ -3,6 +3,7 @@
const uploadDirectory = require ( _ _dirname + '/../../helpers/files/uploadDirectory.js' )
const uploadDirectory = require ( _ _dirname + '/../../helpers/files/uploadDirectory.js' )
, { remove } = require ( 'fs-extra' )
, { remove } = require ( 'fs-extra' )
, { Posts } = require ( _ _dirname + '/../../db/' )
, { Posts } = require ( _ _dirname + '/../../db/' )
, Socketio = require ( _ _dirname + '/../../socketio.js' )
, quoteHandler = require ( _ _dirname + '/../../helpers/posting/quotes.js' )
, quoteHandler = require ( _ _dirname + '/../../helpers/posting/quotes.js' )
, { markdown } = require ( _ _dirname + '/../../helpers/posting/markdown.js' )
, { markdown } = require ( _ _dirname + '/../../helpers/posting/markdown.js' )
, { createHash } = require ( 'crypto' )
, { createHash } = require ( 'crypto' )
@ -20,21 +21,18 @@ module.exports = async (req, res) => {
return acc ;
return acc ;
} , { threads : [ ] , postIds : [ ] , postMongoIds : [ ] } ) ;
} , { threads : [ ] , postIds : [ ] , postMongoIds : [ ] } ) ;
//console.log(threads, postIds, postMongoIds)
//maybe should filter these? because it will include threads from which child posts are already fetched in the action handler, unlike the deleteposts model
const moveEmits = res . locals . posts . reduce ( ( acc , post ) => {
acc . push ( {
room : ` ${ post . board } - ${ post . thread || post . postId } ` ,
postId : post . postId ,
} ) ;
return acc ;
} , [ ] ) ;
const backlinkRebuilds = new Set ( ) ;
const backlinkRebuilds = new Set ( ) ;
const bulkWrites = [ ] ;
const bulkWrites = [ ] ;
if ( threads . length > 0 ) {
//threads moved, so their html/json doesnt need to exist anymore
await Promise . all ( threads . map ( thread => {
return Promise . all ( [
remove ( ` ${ uploadDirectory } /html/ ${ thread . board } /thread/ ${ thread . postId } .html ` ) ,
remove ( ` ${ uploadDirectory } /json/ ${ thread . board } /thread/ ${ thread . postId } .json ` )
] ) ;
} ) ) ;
}
//remove backlinks from selected posts that link to unselected posts
//remove backlinks from selected posts that link to unselected posts
bulkWrites . push ( {
bulkWrites . push ( {
'updateMany' : {
'updateMany' : {
@ -95,6 +93,7 @@ module.exports = async (req, res) => {
acc . replyfiles += p . files . length ;
acc . replyfiles += p . files . length ;
return acc ;
return acc ;
} , { replyposts : 0 , replyfiles : 0 } ) ;
} , { replyposts : 0 , replyfiles : 0 } ) ;
bulkWrites . push ( {
bulkWrites . push ( {
'updateOne' : {
'updateOne' : {
'filter' : {
'filter' : {
@ -112,6 +111,11 @@ module.exports = async (req, res) => {
const movedPosts = await Posts . move ( postMongoIds , req . body . move _to _thread ) . then ( result => result . modifiedCount ) ;
const movedPosts = await Posts . move ( postMongoIds , req . body . move _to _thread ) . then ( result => result . modifiedCount ) ;
//emit markPost moves
for ( let i = 0 ; i < moveEmits . length ; i ++ ) {
Socketio . emitRoom ( moveEmits [ i ] . room , 'markPost' , { postId : moveEmits [ i ] . postId , type : 'move' , mark : 'Moved' } ) ;
}
//get posts that quoted moved posts so we can remarkup them
//get posts that quoted moved posts so we can remarkup them
if ( backlinkRebuilds . size > 0 ) {
if ( backlinkRebuilds . size > 0 ) {
const remarkupPosts = await Posts . globalGetPosts ( [ ... backlinkRebuilds ] ) ;
const remarkupPosts = await Posts . globalGetPosts ( [ ... backlinkRebuilds ] ) ;
@ -160,8 +164,6 @@ module.exports = async (req, res) => {
} ) ) ;
} ) ) ;
}
}
//console.log(require('util').inspect(bulkWrites, {depth:null}))
/ *
/ *
- post A quotes B , then A is moved to another thread : WORKS ( removes backlink on B )
- post A quotes B , then A is moved to another thread : WORKS ( removes backlink on B )
- moving post A back into thread with B and backlink gets readded : WORKS
- moving post A back into thread with B and backlink gets readded : WORKS
@ -174,6 +176,16 @@ module.exports = async (req, res) => {
await Posts . db . bulkWrite ( bulkWrites ) ;
await Posts . db . bulkWrite ( bulkWrites ) ;
}
}
//delete html/json for no longer existing threads, because op was moved
if ( threads . length > 0 ) {
await Promise . all ( threads . map ( thread => {
return Promise . all ( [
remove ( ` ${ uploadDirectory } /html/ ${ thread . board } /thread/ ${ thread . postId } .html ` ) ,
remove ( ` ${ uploadDirectory } /json/ ${ thread . board } /thread/ ${ thread . postId } .json ` )
] ) ;
} ) ) ;
}
return {
return {
message : 'Moved posts' ,
message : 'Moved posts' ,
action : movedPosts > 0 ,
action : movedPosts > 0 ,