mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.3 KiB
152 lines
4.3 KiB
5 years ago
|
'use strict';
|
||
|
|
||
5 years ago
|
const Posts = require(__dirname+'/../db/posts.js')
|
||
|
, Boards = require(__dirname+'/../db/boards.js')
|
||
|
, uploadDirectory = require(__dirname+'/files/uploadDirectory.js')
|
||
|
, render = require(__dirname+'/render.js');
|
||
5 years ago
|
|
||
5 years ago
|
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)
|
||
|
*/
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
module.exports = {
|
||
|
|
||
5 years ago
|
buildBanners: async(board) => {
|
||
|
console.log('building banners', `${board._id}/banners.html`);
|
||
|
return render(`${board._id}/banners.html`, 'banners.pug', {
|
||
5 years ago
|
board: board,
|
||
5 years ago
|
});
|
||
|
},
|
||
|
|
||
5 years ago
|
buildCatalog: async (board) => {
|
||
5 years ago
|
console.log('building catalog', `${board._id}/catalog.html`);
|
||
5 years ago
|
if (!board._id) {
|
||
|
board = await Boards.findOne(board);
|
||
|
}
|
||
5 years ago
|
const threads = await Posts.getCatalog(board._id);
|
||
|
return render(`${board._id}/catalog.html`, 'catalog.pug', {
|
||
|
board,
|
||
5 years ago
|
threads,
|
||
5 years ago
|
});
|
||
|
},
|
||
|
|
||
|
buildThread: async (threadId, board) => {
|
||
5 years ago
|
console.log('building thread', `${board._id || board}/thread/${threadId}.html`);
|
||
5 years ago
|
if (!board._id) {
|
||
|
board = await Boards.findOne(board);
|
||
|
}
|
||
|
const thread = await Posts.getThread(board._id, threadId)
|
||
|
if (!thread) {
|
||
5 years ago
|
return; //this thread may have been an OP that was deleted
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
addBacklinks(thread, false);
|
||
5 years ago
|
|
||
5 years ago
|
return render(`${board._id}/thread/${threadId}.html`, 'thread.pug', {
|
||
|
board,
|
||
5 years ago
|
thread,
|
||
5 years ago
|
});
|
||
|
},
|
||
|
|
||
|
buildBoard: async (board, page, maxPage=null) => {
|
||
5 years ago
|
console.log('building board page', `${board._id}/${page === 1 ? 'index' : page}.html`);
|
||
5 years ago
|
const threads = await Posts.getRecent(board._id, page);
|
||
|
if (!maxPage) {
|
||
5 years ago
|
maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10));
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
for (let k = 0; k < threads.length; k++) {
|
||
|
const thread = threads[k];
|
||
5 years ago
|
addBacklinks(thread, true);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
return render(`${board._id}/${page === 1 ? 'index' : page}.html`, 'board.pug', {
|
||
|
board,
|
||
|
threads,
|
||
|
maxPage,
|
||
5 years ago
|
page,
|
||
5 years ago
|
});
|
||
|
},
|
||
|
|
||
|
//building multiple pages (for rebuilds)
|
||
5 years ago
|
buildBoardMultiple: async (board, startpage=1, endpage) => {
|
||
|
const maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10));
|
||
5 years ago
|
if (endpage === 0) {
|
||
|
//deleted only/all posts, so only 1 page will remain
|
||
|
endpage = 1;
|
||
|
} else if (maxPage < endpage) {
|
||
|
//else just build up to the max page if it is greater than input page number
|
||
|
endpage = maxPage
|
||
|
}
|
||
5 years ago
|
const difference = endpage-startpage + 1; //+1 because for single pagemust be > 0
|
||
5 years ago
|
const threads = await Posts.getRecent(board._id, startpage, difference*Math.ceil(board.settings.threadLimit/10));
|
||
5 years ago
|
for (let k = 0; k < threads.length; k++) {
|
||
|
const thread = threads[k];
|
||
5 years ago
|
addBacklinks(thread, true);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
const buildArray = [];
|
||
5 years ago
|
console.log('multi building board pages', `${board._id}/ ${startpage === 1 ? 'index' : startpage} -> ${endpage === 1 ? 'index' : endpage} .html`);
|
||
5 years ago
|
for (let i = startpage; i <= endpage; i++) {
|
||
|
let spliceStart = (i-1)*10;
|
||
|
if (spliceStart > 0) {
|
||
|
spliceStart = spliceStart - 1;
|
||
|
}
|
||
|
buildArray.push(
|
||
|
render(`${board._id}/${i === 1 ? 'index' : i}.html`, 'board.pug', {
|
||
|
board,
|
||
|
threads: threads.splice(0,10),
|
||
|
maxPage,
|
||
|
page: i,
|
||
|
})
|
||
|
);
|
||
|
}
|
||
|
return Promise.all(buildArray);
|
||
|
},
|
||
|
|
||
|
buildHomepage: async () => {
|
||
|
const boards = await Boards.find();
|
||
|
return render('index.html', 'home.pug', {
|
||
5 years ago
|
boards,
|
||
5 years ago
|
});
|
||
|
},
|
||
|
|
||
|
buildChangePassword: () => {
|
||
|
return render('changepassword.html', 'changepassword.pug');
|
||
|
},
|
||
|
|
||
|
buildRegister: () => {
|
||
|
return render('register.html', 'register.pug');
|
||
|
},
|
||
|
|
||
5 years ago
|
buildCaptcha: () => {
|
||
|
return render('captcha.html', 'captcha.pug');
|
||
|
},
|
||
|
|
||
5 years ago
|
}
|