Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
unify post deletion including removing old html, and add backlinks in…
Browse files Browse the repository at this point in the history
… the databse. will allow for more easily re-marking up posts in future and unlinking dead quotes from deleted posts or in cyclical threads
  • Loading branch information
fatchan committed Jul 1, 2019
1 parent 37ab2f5 commit 0b817b9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 81 deletions.
40 changes: 26 additions & 14 deletions db/posts.js
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;

},
Expand Down Expand Up @@ -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) => {
Expand Down
39 changes: 0 additions & 39 deletions helpers/build.js
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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`);
Expand Down
25 changes: 7 additions & 18 deletions models/forms/changeboardsettings.js
Expand Up @@ -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));
}
}
Expand All @@ -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);
}
Expand Down
7 changes: 6 additions & 1 deletion models/forms/deletepost.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 7 additions & 9 deletions models/forms/makepost.js
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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)));
}

Expand All @@ -301,6 +299,6 @@ console.log(`NEW POST -> ${successRedirect}`)
//finish building other pages
await Promise.all(parallelPromises);

console.log('--------------------------------')
console.log('--------------------------------');

}

0 comments on commit 0b817b9

Please sign in to comment.