From 1a186b865c3353f5000b2522d2f1f04bd07378ec Mon Sep 17 00:00:00 2001 From: fatchan Date: Tue, 28 May 2019 04:12:30 +0000 Subject: [PATCH] dont delete files since we hash them. keep for later --- db/posts.js | 11 +-- models/forms/actionhandler.js | 76 ++++++++++---------- models/forms/delete-post.js | 8 +-- models/forms/deletebanners.js | 2 - models/forms/deletepostsfiles.js | 40 ++++++----- views/includes/actionfooter.pug | 9 ++- views/includes/actionfooter_globalmanage.pug | 4 +- views/includes/actionfooter_manage.pug | 4 +- 8 files changed, 71 insertions(+), 83 deletions(-) diff --git a/db/posts.js b/db/posts.js index 15b9f787..83d06d44 100644 --- a/db/posts.js +++ b/db/posts.js @@ -2,7 +2,6 @@ const Mongo = require(__dirname+'/db.js') , Boards = require(__dirname+'/boards.js') - , deletePostFiles = require(__dirname+'/../helpers/files/deletepostfiles.js') , db = Mongo.client.db('jschan').collection('posts'); module.exports = { @@ -272,7 +271,7 @@ module.exports = { }, insertOne: async (board, data, thread) => { - if (data.thread !== null && data.email !== 'sage' && !thread.saged) { + if (data.thread !== null) { const filter = { 'postId': data.thread, 'board': board @@ -355,14 +354,6 @@ module.exports = { const threadPosts = await module.exports.getMultipleThreadPosts(board, threadIds); //combine them const postsAndThreads = threads.concat(threadPosts); - //get the filenames and delete all the files - let fileNames = []; - postsAndThreads.forEach(post => { - fileNames = fileNames.concat(post.files.map(x => x.filename)) - }); - if (fileNames.length > 0) { - await deletePostFiles(fileNames); - } //get the mongoIds and delete them all const postMongoIds = postsAndThreads.map(post => Mongo.ObjectId(post._id)); await module.exports.deleteMany(postMongoIds); diff --git a/models/forms/actionhandler.js b/models/forms/actionhandler.js index 78dd0360..cba84eab 100644 --- a/models/forms/actionhandler.js +++ b/models/forms/actionhandler.js @@ -49,23 +49,21 @@ module.exports = async (req, res, next) => { const passwordCombinedQuery = {}; let aggregateNeeded = false; try { - if (res.locals.hasPerms) { - // if getting global banned, board ban doesnt matter - if (req.body.global_ban) { - const { message, action, query } = await banPoster(req, res, next, null, res.locals.posts); - if (action) { - combinedQuery[action] = { ...combinedQuery[action], ...query} - } - messages.push(message); - } else if (req.body.ban) { - const { message, action, query } = await banPoster(req, res, next, req.params.board, res.locals.posts); - if (action) { - combinedQuery[action] = { ...combinedQuery[action], ...query} - } - messages.push(message); + // if getting global banned, board ban doesnt matter + if (req.body.global_ban) { + const { message, action, query } = await banPoster(req, res, next, null, res.locals.posts); + if (action) { + combinedQuery[action] = { ...combinedQuery[action], ...query} } + messages.push(message); + } else if (req.body.ban) { + const { message, action, query } = await banPoster(req, res, next, req.params.board, res.locals.posts); + if (action) { + combinedQuery[action] = { ...combinedQuery[action], ...query} + } + messages.push(message); } - if (res.locals.hasPerms && (req.body.delete_ip_board || req.body.delete_ip_global)) { + if (req.body.delete_ip_board || req.body.delete_ip_global) { const deletePostIps = res.locals.posts.map(x => x.ip); let query = { 'ip': { @@ -88,8 +86,8 @@ module.exports = async (req, res, next) => { aggregateNeeded = true; } else { // if it was getting deleted, we cant do any of these - if (req.body.delete_file) { - const { message, action, query } = await deletePostsFiles(passwordPosts); + if (req.body.delete_file || req.body.unlink_file) { + const { message, action, query } = await deletePostsFiles(passwordPosts, req.body.unlink_file); if (action) { aggregateNeeded = true; passwordCombinedQuery[action] = { ...passwordCombinedQuery[action], ...query} @@ -102,29 +100,27 @@ module.exports = async (req, res, next) => { } messages.push(message); } - if (res.locals.hasPerms) { - //lock, sticky, sage - if (req.body.sage) { - const { message, action, query } = sagePosts(res.locals.posts); - if (action) { - combinedQuery[action] = { ...combinedQuery[action], ...query} - } - messages.push(message); + //lock, sticky, sage + if (req.body.sage) { + const { message, action, query } = sagePosts(res.locals.posts); + if (action) { + combinedQuery[action] = { ...combinedQuery[action], ...query} } - if (req.body.lock) { - const { message, action, query } = lockPosts(res.locals.posts); - if (action) { - combinedQuery[action] = { ...combinedQuery[action], ...query} - } - messages.push(message); + messages.push(message); + } + if (req.body.lock) { + const { message, action, query } = lockPosts(res.locals.posts); + if (action) { + combinedQuery[action] = { ...combinedQuery[action], ...query} } - if (req.body.sticky) { - const { message, action, query } = stickyPosts(res.locals.posts); - if (action) { - combinedQuery[action] = { ...combinedQuery[action], ...query} - } - messages.push(message); + messages.push(message); + } + if (req.body.sticky) { + const { message, action, query } = stickyPosts(res.locals.posts); + if (action) { + combinedQuery[action] = { ...combinedQuery[action], ...query} } + messages.push(message); } // cannot report and dismiss at same time if (req.body.report) { @@ -133,7 +129,7 @@ module.exports = async (req, res, next) => { combinedQuery[action] = { ...combinedQuery[action], ...query} } messages.push(message); - } else if (res.locals.hasPerms && req.body.dismiss) { + } else if (req.body.dismiss) { const { message, action, query } = dismissReports(res.locals.posts); if (action) { combinedQuery[action] = { ...combinedQuery[action], ...query} @@ -147,7 +143,7 @@ module.exports = async (req, res, next) => { combinedQuery[action] = { ...combinedQuery[action], ...query} } messages.push(message); - } else if (res.locals.hasPerms && req.body.global_dismiss) { + } else if (req.body.global_dismiss) { const { message, action, query } = dismissGlobalReports(res.locals.posts); if (action) { combinedQuery[action] = { ...combinedQuery[action], ...query} @@ -293,7 +289,7 @@ module.exports = async (req, res, next) => { } else if (req.body.sticky) { //else if -- if deleting, other actions are not executed/irrelevant //rebuild current and newer pages parallelPromises.push(buildBoardMultiple(buildBoards[boardName], 1, threadPageOldest)); - } else if (req.body.lock || req.body.sage || req.body.spoiler || req.body.ban || req.body.global_ban) { + } else if (req.body.lock || req.body.sage || req.body.spoiler || req.body.ban || req.body.global_ban || req.body.unlink_file) { //rebuild inbewteen pages for things that dont cause page/thread movement //should rebuild only affected pages, but finding the page of all affected //threads could end up being slower/more resource intensive. this is simpler. diff --git a/models/forms/delete-post.js b/models/forms/delete-post.js index eee05592..04755730 100644 --- a/models/forms/delete-post.js +++ b/models/forms/delete-post.js @@ -1,7 +1,6 @@ 'use strict'; const uploadDirectory = require(__dirname+'/../../helpers/uploadDirectory.js') - , deletePostFiles = require(__dirname+'/../../helpers/files/deletepostfiles.js') , remove = require('fs-extra').remove , Mongo = require(__dirname+'/../../db/db.js') , Posts = require(__dirname+'/../../db/posts.js'); @@ -39,7 +38,7 @@ module.exports = async (req, res, next, posts, board) => { //combine them all into one array, there may be duplicates but it shouldnt matter const allPosts = posts.concat(threadPosts) - //delete posts from DB + //get all mongoids and delete posts from const postMongoIds = allPosts.map(post => Mongo.ObjectId(post._id)) const deletedPosts = await Posts.deleteMany(postMongoIds).then(result => result.deletedCount); @@ -49,11 +48,6 @@ module.exports = async (req, res, next, posts, board) => { fileNames = fileNames.concat(post.files.map(x => x.filename)) }) - //delete post files - if (fileNames.length > 0) { - await deletePostFiles(fileNames); - } - //hooray! return { message:`Deleted ${threads.length} threads and ${deletedPosts-threads.length} posts` }; diff --git a/models/forms/deletebanners.js b/models/forms/deletebanners.js index f8015408..84224289 100644 --- a/models/forms/deletebanners.js +++ b/models/forms/deletebanners.js @@ -12,8 +12,6 @@ module.exports = async (req, res, next) => { remove(`${uploadDirectory}banner/${filename}`); })); - // i dont think there is a way to get the number of array items removed with $pullAll - // so i cant return how many banners were deleted await Boards.removeBanners(req.params.board, req.body.checkedbanners); return res.render('message', { diff --git a/models/forms/deletepostsfiles.js b/models/forms/deletepostsfiles.js index b1d80988..beeb509d 100644 --- a/models/forms/deletepostsfiles.js +++ b/models/forms/deletepostsfiles.js @@ -3,7 +3,7 @@ const remove = require('fs-extra').remove , uploadDirectory = require(__dirname+'/../../helpers/uploadDirectory.js') -module.exports = async (posts) => { +module.exports = async (posts, unlinkOnly) => { //get filenames from all the posts let fileNames = []; @@ -13,25 +13,31 @@ module.exports = async (posts) => { if (fileNames.length === 0) { return { - message: 'No files to delete' + message: 'No files found' } } - //delete all the files using the filenames - await Promise.all(fileNames.map(async filename => { - //dont question it. - return Promise.all([ - remove(`${uploadDirectory}img/${filename}`), - remove(`${uploadDirectory}img/thumb-${filename.split('.')[0]}.png`) - ]) - })); + if (unlinkOnly) { + return { + message:`Unlinked ${fileNames.length} file(s) across ${posts.length} post(s)`, + action:'$set', + query: { + 'files': [] + } + }; + } else { + //delete all the files using the filenames + await Promise.all(fileNames.map(async filename => { + //dont question it. + return Promise.all([ + remove(`${uploadDirectory}img/${filename}`), + remove(`${uploadDirectory}img/thumb-${filename.split('.')[0]}.jpg`) + ]) + })); + return { + message:`Deleted ${fileNames.length} file(s) from server`, + }; + } - return { - message:`Deleted ${fileNames.length} file(s) across ${posts.length} post(s)`, - action:'$set', - query: { - 'files': [] - } - }; } diff --git a/views/includes/actionfooter.pug b/views/includes/actionfooter.pug index 5e938d6a..54c3997c 100644 --- a/views/includes/actionfooter.pug +++ b/views/includes/actionfooter.pug @@ -6,11 +6,11 @@ details.toggle-label input.post-check(type='checkbox', name='delete' value=1) | Delete label - input.post-check(type='checkbox', name='delete_file' value=1) - | Delete File Only + input.post-check(type='checkbox', name='unlink_file' value=1) + | Unlink Files label input.post-check(type='checkbox', name='spoiler' value=1) - | Spoiler Images + | Spoiler Files label input#password(type='text', name='password', placeholder='post password' autocomplete='off') label @@ -29,6 +29,9 @@ details.toggle-label label input.post-check(type='checkbox', name='delete_ip_global' value=1) | Delete from IP globally + label + input.post-check(type='checkbox', name='delete_file' value=1) + | Delete Files label input.post-check(type='checkbox', name='sticky' value=1) | Sticky diff --git a/views/includes/actionfooter_globalmanage.pug b/views/includes/actionfooter_globalmanage.pug index 90d9b47a..3d70d36d 100644 --- a/views/includes/actionfooter_globalmanage.pug +++ b/views/includes/actionfooter_globalmanage.pug @@ -7,10 +7,10 @@ details.toggle-label | Delete label input.post-check(type='checkbox', name='delete_file' value=1) - | Delete File Only + | Delete Files label input.post-check(type='checkbox', name='spoiler' value=1) - | Spoiler Images + | Spoiler Files label input.post-check(type='checkbox', name='delete_ip_global' value=1) | Delete from IP globally diff --git a/views/includes/actionfooter_manage.pug b/views/includes/actionfooter_manage.pug index 636e708c..d36f9770 100644 --- a/views/includes/actionfooter_manage.pug +++ b/views/includes/actionfooter_manage.pug @@ -7,10 +7,10 @@ details.toggle-label | Delete label input.post-check(type='checkbox', name='delete_file' value=1) - | Delete File Only + | Delete Files label input.post-check(type='checkbox', name='spoiler' value=1) - | Spoiler Images + | Spoiler Files label input.post-check(type='checkbox', name='global_report' value=1) | Global Report