diff --git a/db/posts.js b/db/posts.js index cce38801..f9b66252 100644 --- a/db/posts.js +++ b/db/posts.js @@ -2,6 +2,7 @@ const Mongo = require(__dirname+'/db.js') , Boards = require(__dirname+'/boards.js') + , deletePosts = require(__dirname+'/../models/forms/deletepost.js') , db = Mongo.client.db('jschan').collection('posts'); module.exports = { @@ -276,26 +277,47 @@ module.exports = { 'postId': data.thread, 'board': board } + //update thread reply and reply file count const query = { '$inc': { 'replyposts': 1, 'replyfiles': data.files.length } } + //if post email is not sage, and thread not saged, set bump date if (data.email !== 'sage' && !thread.saged) { query['$set'] = { 'bumped': Date.now() } } + //update the thread await db.updateOne(filter, query); } else { - //this is a new thread, so set the bump date - data.bumped = Date.now() + //this is a new thread so just set the bump date + data.bumped = Date.now(); } + //get the postId and add it to the post const postId = await Boards.getNextId(board); data.postId = postId; + + //insert the post itself await db.insertOne(data); + + //add backlinks to the posts this post quotes + if (data.quotes.length > 0) { + await db.updateMany({ + 'postId': { + '$in': data.quotes + }, + 'board': board + }, { + '$push': { + 'backlinks': postId + } + }); + } + return postId; }, @@ -344,20 +366,10 @@ module.exports = { 'sticky': -1, 'bumped': -1 }).skip(threadLimit).toArray(); - //if there are any if (threads.length === 0) { - return threads; + return; } - //get the postIds - const threadIds = threads.map(thread => thread.postId); - //get all the posts from those threads - const threadPosts = await module.exports.getMultipleThreadPosts(board, threadIds); - //combine them - const postsAndThreads = threads.concat(threadPosts); - //get the mongoIds and delete them all - const postMongoIds = postsAndThreads.map(post => Mongo.ObjectId(post._id)); - await module.exports.deleteMany(postMongoIds); - return threadIds; + await deletePosts(threads, board); }, deleteMany: (ids) => { diff --git a/helpers/build.js b/helpers/build.js index 7d2519d9..1dc6ee4f 100644 --- a/helpers/build.js +++ b/helpers/build.js @@ -7,35 +7,6 @@ const Mongo = require(__dirname+'/../db/db.js') , uploadDirectory = require(__dirname+'/files/uploadDirectory.js') , render = require(__dirname+'/render.js'); -function addBacklinks(thread, preview) { //preview means this is not the full thread - const postMap = new Map() - postMap.set(thread.postId, thread) - for (let i = 0; i < thread.replies.length; i++) { - const reply = thread.replies[i]; - postMap.set(reply.postId, reply); - } - for (let i = 0; i < thread.replies.length; i++) { - const reply = thread.replies[i]; - if (!reply.quotes) continue; - for (let j = 0; j < reply.quotes.length; j++) { - const quote = reply.quotes[j]; - if (postMap.has(quote)) { - const post = postMap.get(quote) - if (!post.backlinks) { - post.backlinks = []; - } - post.backlinks.push(reply.postId); - } else if (!preview) { - /* - quote was valid on post creation, but points to postID that has been deleted - or possibly removed from cyclical thread (whenever i implement those) - could re-markdown the post here to remove the quotes (or convert to greentext) - */ - } - } - } -} - module.exports = { buildBanners: async(board) => { @@ -67,8 +38,6 @@ console.log('building thread', `${board._id || board}/thread/${threadId}.html`); return; //this thread may have been an OP that was deleted } - addBacklinks(thread, false); - return render(`${board._id}/thread/${threadId}.html`, 'thread.pug', { board, thread, @@ -81,10 +50,6 @@ console.log('building board page', `${board._id}/${page === 1 ? 'index' : page}. if (maxPage == null) { maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10)); } - for (let k = 0; k < threads.length; k++) { - const thread = threads[k]; - addBacklinks(thread, true); - } return render(`${board._id}/${page === 1 ? 'index' : page}.html`, 'board.pug', { board, @@ -106,10 +71,6 @@ console.log('building board page', `${board._id}/${page === 1 ? 'index' : page}. } const difference = endpage-startpage + 1; //+1 because for single pagemust be > 0 const threads = await Posts.getRecent(board._id, startpage, difference*Math.ceil(board.settings.threadLimit/10)); - for (let k = 0; k < threads.length; k++) { - const thread = threads[k]; - addBacklinks(thread, true); - } const buildArray = []; console.log('multi building board pages', `${board._id}/ ${startpage === 1 ? 'index' : startpage} -> ${endpage === 1 ? 'index' : endpage} .html`); diff --git a/models/forms/changeboardsettings.js b/models/forms/changeboardsettings.js index 69774b0c..c4d58446 100644 --- a/models/forms/changeboardsettings.js +++ b/models/forms/changeboardsettings.js @@ -47,19 +47,14 @@ module.exports = async (req, res, next) => { const newMaxPage = Math.ceil(newSettings.threadLimit/10); if (newMaxPage < oldMaxPage) { //prune old threads - const prunedThreads = await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit); - if (prunedThreads.length > 0) { - //remove pruned threads html also - for (let i = 0; i < prunedThreads.length; i++) { - promises.push(remove(`${uploadDirectory}html/${req.params.board}/thread/${prunedThreads[i]}.html`)); - } - //remove board page html for pages > newMaxPage - for (let i = newMaxPage+1; i <= oldMaxPage; i++) { - promises.push(remove(`${uploadDirectory}html/${req.params.board}/${i}.html`)); - } - //rebuild valid board pages for page numbers to be <= newMaxPage + const { action } = await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit); + //remove board page html for pages > newMaxPage + for (let i = newMaxPage+1; i <= oldMaxPage; i++) { + promises.push(remove(`${uploadDirectory}html/${req.params.board}/${i}.html`)); + } + if (action) { + //rebuild valid board pages for page numbers, and catalog for prunedthreads promises.push(buildBoardMultiple(res.locals.board, 1, newMaxPage)); - //rebuild catalog since some threads were pruned promises.push(buildCatalog(res.locals.board)); } } @@ -68,12 +63,6 @@ module.exports = async (req, res, next) => { promises.push(remove(`${uploadDirectory}html/${req.params.board}/`)); } -/* disabled since homepage is built daily on schedule - if (oldSettings.name !== newSettings.name || oldSettings.description !== newSettings.description) { - promises.push(buildHomepage()) - } -*/ - if (promises.length > 0) { await Promise.all(promises); } diff --git a/models/forms/deletepost.js b/models/forms/deletepost.js index 2e94794c..d405c003 100644 --- a/models/forms/deletepost.js +++ b/models/forms/deletepost.js @@ -5,7 +5,7 @@ const uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory. , Mongo = require(__dirname+'/../../db/db.js') , Posts = require(__dirname+'/../../db/posts.js'); -module.exports = async (req, res, next, posts, board) => { +module.exports = async (posts, board) => { //filter to threads const threads = posts.filter(x => x.thread == null); @@ -38,6 +38,11 @@ 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) +//NOTE: this is where, when implemented, file ref counts would be decremented +//NOTE: this is where, when implemented, re-marking up posts that quoted deleted posts would be done +//could use a destructuring with Array.reduce when i need to get files array, backlinks array and mongoId array +//instead of doing 3 maps or big for loop + //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); diff --git a/models/forms/makepost.js b/models/forms/makepost.js index 45167a41..f2d537c6 100644 --- a/models/forms/makepost.js +++ b/models/forms/makepost.js @@ -250,7 +250,8 @@ module.exports = async (req, res, next) => { files, 'reports': [], 'globalreports': [], - quotes + quotes, //posts this post replies to + 'backlinks': [], //posts replying to this post } if (!req.body.thread) { @@ -267,8 +268,8 @@ module.exports = async (req, res, next) => { const postId = await Posts.insertOne(req.params.board, data, thread); const successRedirect = `/${req.params.board}/thread/${req.body.thread || postId}.html#${postId}`; -console.log('--------------------------------') -console.log(`NEW POST -> ${successRedirect}`) +console.log('--------------------------------'); +console.log(`NEW POST -> ${successRedirect}`); //build just the thread they need to see first and send them immediately await buildThread(data.thread || postId, res.locals.board); @@ -287,11 +288,8 @@ console.log(`NEW POST -> ${successRedirect}`) parallelPromises.push(buildBoardMultiple(res.locals.board, 1, threadPage)); } } else { - //new thread, rebuild all pages and prunes old threads - const prunedThreads = await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit); - for (let i = 0; i < prunedThreads.length; i++) { - parallelPromises.push(remove(`${uploadDirectory}html/${req.params.board}/thread/${prunedThreads[i]}.html`)); - } + //new thread, prunes any old threads before rebuilds + await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit); parallelPromises.push(buildBoardMultiple(res.locals.board, 1, Math.ceil(res.locals.board.settings.threadLimit/10))); } @@ -301,6 +299,6 @@ console.log(`NEW POST -> ${successRedirect}`) //finish building other pages await Promise.all(parallelPromises); -console.log('--------------------------------') +console.log('--------------------------------'); }