Try to integrate post moving a bit better into the existing combined actions flow

Bugfix some move issues
merge-requests/341/head
Thomas Lynch 1 year ago
parent 66b21f93b7
commit a1ccd6f267
  1. 10
      controllers/forms/actions.js
  2. 38
      db/posts.js
  3. 31
      models/forms/actionhandler.js
  4. 3
      models/forms/makepost.js
  5. 7
      models/forms/moveposts.js

@ -48,7 +48,7 @@ module.exports = {
{ result: async () => {
if (req.body.move && req.body.move_to_thread) {
const moveBoard = req.body.move_to_board || req.params.board;
res.locals.destinationThread = await Posts.threadExists(moveBoard, req.body.move_to_thread);
res.locals.destinationThread = await Posts.getPost(moveBoard, req.body.move_to_thread);
return res.locals.destinationThread != null;
}
return true;
@ -57,10 +57,12 @@ module.exports = {
if (!res.locals.user || !res.locals.user.username) {
return false;
}
if (req.body.move && req.body.move_to_board) {
if (req.body.move && req.body.move_to_board
&& req.body.move_to_board !== req.params.board) {
const destinationBoard = await Boards.findOne(req.body.move_to_board);
if (res.locals.permissions.hasAny(Permissions.MANAGE_GLOBAL_GENERAL, Permissions.MANAGE_BOARD_GENERAL)
|| destinationBoard.staff[res.locals.user.username] != null) {
if (res.locals.permissions.get(Permissions.MANAGE_GLOBAL_GENERAL)
|| (res.locals.permissions.get(Permissions.MANAGE_BOARD_GENERAL)
&& destinationBoard.staff[res.locals.user.username] != null)) {
res.locals.destinationBoard = destinationBoard;
}
return res.locals.destinationBoard != null;

@ -6,6 +6,8 @@ const Mongo = require(__dirname+'/db.js')
, Boards = require(__dirname+'/boards.js')
, Stats = require(__dirname+'/stats.js')
, Permissions = require(__dirname+'/../lib/permission/permissions.js')
, { randomBytes } = require('crypto')
, randomBytesAsync = require('util').promisify(randomBytes)
, db = Mongo.db.collection('posts')
, config = require(__dirname+'/../lib/misc/config.js');
@ -794,7 +796,7 @@ module.exports = {
return db.deleteMany();
},
move: async (postMongoIds, crossBoard, destinationThreadId, destinationBoard=null) => {
move: async (postMongoIds, crossBoard, destinationThreadId, destinationBoard) => {
let bulkWrites = []
, newDestinationThreadId = destinationThreadId;
if (crossBoard) {
@ -802,7 +804,7 @@ module.exports = {
const lastId = await Boards.getNextId(destinationBoard, false, postMongoIds.length);
//if moving board and no destination thread, pick the starting ID of the amount we incremented
if (!destinationThreadId) {
newDestinationThreadId = lastId - (postMongoIds.length-1);
newDestinationThreadId = lastId;
}
bulkWrites = postMongoIds.map((postMongoId, index) => ({
'updateOne': {
@ -811,7 +813,7 @@ module.exports = {
},
'update': {
'$set': {
'postId': newDestinationThreadId + index,
'postId': lastId + index,
}
}
}
@ -850,13 +852,13 @@ module.exports = {
'update': {
'$set': {
'thread': null,
'replyposts': postMongoIds.length-1,
'replyfiles': 0, //TODO
'sticky': 0, //TODO (tbh we might just wanna set this to 0)
'locked': 0, //TODO
'bumplocked': 0, //TODO
'cyclic': 0, //TOOD
// 'salt': '',
'replyposts': 0,
'replyfiles': 0,
'sticky': 0,
'locked': 0,
'bumplocked': 0,
'cyclic': 0,
'salt': (await randomBytesAsync(128)).toString('base64'),
}
}
}
@ -867,22 +869,8 @@ module.exports = {
return { movedPosts, destinationThreadId: newDestinationThreadId };
},
threadExists: (board, thread) => {
return db.findOne({
'board': board,
'postId': thread,
'thread': null,
}, {
'projection': {
'_id': 1,
'postId': 1,
'salt': 1,
}
});
},
threadExistsMiddleware: async (req, res, next) => {
const thread = await module.exports.threadExists(req.params.board, req.params.id);
const thread = await module.exports.getPost(req.params.board, req.params.id);
if (!thread) {
return res.status(404).render('404');
}

@ -198,6 +198,11 @@ module.exports = async (req, res, next) => {
if (action) {
modlogActions.push('Moved');
recalculateThreadMetadata = true;
if (res.locals.destinationBoard && res.locals.destinationThread) {
res.locals.posts.push(res.locals.destinationThread);
({ boardThreadMap, numPagesBeforeActions, affectedBoardNames } = await getAffectedBoards(res.locals.posts, deleting));
minimalThreadsMap = await Posts.getMinimalThreads(affectedBoardNames);
}
}
messages.push(message);
@ -338,7 +343,8 @@ module.exports = async (req, res, next) => {
}
modlog[post.board].postLinks.push({
postId: post.postId,
thread: req.body.move ? req.body.move_to_thread : post.thread,
thread: res.locals.destinationThread ? res.locals.destinationThread.postId : post.thread,
board: res.locals.destinationBoard ? res.locals.destinationBoard._id : post.board,
});
}
const modlogDocuments = [];
@ -401,9 +407,14 @@ module.exports = async (req, res, next) => {
//recalculate replies and image counts if necessary
if (recalculateThreadMetadata) {
const selectedPosts = res.locals.posts.filter(p => p.thread !== null);
const selectedPosts = res.locals.posts
.filter(p => p.thread !== null);
if (selectedPosts.length > 0) {
const replyOrs = selectedPosts.map(p => ({ board: p.board, thread: p.thread }));
const replyOrs = selectedPosts
.map(p => ({ board: p.board, thread: p.thread }));
if (req.body.move && res.locals.destinationBoard && res.locals.destinationThread) {
replyOrs.push({ board: res.locals.destinationThread.board, thread: res.locals.destinationThread.postId });
}
const threadReplyAggregates = await Posts.getThreadAggregates(replyOrs);
const bulkWrites = [];
const threads = threadsEachBoard;
@ -460,6 +471,7 @@ module.exports = async (req, res, next) => {
Using the proper ordering of threads, to account for sticky, bumplocks, etc.
*/
const pageBounds = threadsEachBoard.reduce((acc, t) => {
if (!minimalThreadsMap[t.board]) { return acc; }
if (!acc[t.board]) { acc[t.board] = { first: null, last: null }; }
const threadIndex = minimalThreadsMap[t.board].findIndex(p => p.postId === t.postId);
const threadPage = Math.max(1, Math.ceil((threadIndex+1)/10));
@ -526,19 +538,6 @@ module.exports = async (req, res, next) => {
'endpage': numPagesAfterActions,
}
});
if (res.locals.destinationBoard && res.locals.destinationBoard._id !== req.params.board) {
//cross board move happened, rebuild the other board also
const crossBoardMoveName = res.locals.destinationBoard._id;
const crossBoardMovePages = Math.ceil((await Posts.getPages(crossBoardMoveName)) / 10);
buildQueue.push({
'task': 'buildBoardMultiple',
'options': {
'board': res.locals.destinationBoard,
'startpage': 1,
'endpage': crossBoardMovePages,
}
});
}
} else {

@ -59,7 +59,8 @@ module.exports = async (req, res) => {
sageOnlyEmail, forceAnon, replyLimit, disableReplySubject,
captchaMode, lockMode, allowedFileTypes, customFlags, geoFlags, fileR9KMode, messageR9KMode } = res.locals.board.settings;
if (!isStaffOrGlobal
&& res.locals.country //permission for this or nah?
// && !res.locals.permissions.get(Permissions.BYPASS_FILTERS) //TODO: new permission for "bypass blocks" or something
&& res.locals.country
&& blockedCountries.includes(res.locals.country.code)) {
await deleteTempFiles(req).catch(console.error);
return dynamicResponse(req, res, 403, 'message', {

@ -88,7 +88,8 @@ module.exports = async (req, res) => {
}
//increase file/reply count in thread we are moving the posts to
if (res.locals.destinationThread) {
if (!res.locals.destinationBoard) {
//recalculateThreadMetadata will handle cross board moves
const { replyposts, replyfiles } = res.locals.posts.reduce((acc, p) => {
acc.replyposts += 1;
acc.replyfiles += p.files.length;
@ -98,7 +99,7 @@ module.exports = async (req, res) => {
'updateOne': {
'filter': {
'postId': req.body.move_to_thread,
'board': req.body.move_to_board || req.params.board,
'board': req.params.board,
},
'update': {
'$inc': {
@ -123,7 +124,7 @@ module.exports = async (req, res) => {
//no destination thread specified (making new thread from posts), need to fetch OP as destinationThread for remarkup/salt
if (!res.locals.destinationThread) {
res.locals.destinationThread = await Posts.threadExists(destinationBoard, destinationThreadId);
res.locals.destinationThread = await Posts.getPost(destinationBoard, destinationThreadId);
}
//get posts that quoted moved posts so we can remarkup them

Loading…
Cancel
Save