backlinking refactor, remove helmet from server since its basically useless

merge-requests/208/head
fatchan 5 years ago
parent 36a3755dca
commit 5e1e0f7ef9
  1. 115
      build.js
  2. 27
      server.js

@ -5,6 +5,36 @@ const Posts = require(__dirname+'/db/posts.js')
, uploadDirectory = require(__dirname+'/helpers/uploadDirectory.js')
, render = require(__dirname+'/helpers/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 = {
buildCatalog: async (board) => {
@ -29,32 +59,7 @@ module.exports = {
return; //this thread may have been an OP that was deleted
}
/*
temporary, jsut seeing how well this works
*/
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);
}
}
}
/*
temporary, jsut seeing how well this works
*/
addBacklinks(thread, false);
return render(`${board._id}/thread/${threadId}.html`, 'thread.pug', {
board,
@ -68,35 +73,12 @@ module.exports = {
if (!maxPage) {
maxPage = Math.ceil((await Posts.getPages(board._id)) / 10);
}
/*
temporary, jsut seeing how well this works
*/
for (let k = 0; k < threads.length; k++) {
const thread = threads[k];
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);
}
}
}
addBacklinks(thread, true);
}
/*
temporary, jsut seeing how well this works
*/
return render(`${board._id}/${page === 1 ? 'index' : page}.html`, 'board.pug', {
board,
threads,
@ -117,35 +99,12 @@ module.exports = {
}
const difference = endpage-startpage + 1; //+1 because for single pagemust be > 0
const threads = await Posts.getRecent(board._id, startpage, difference*10);
/*
temporary, jsut seeing how well this works
*/
for (let k = 0; k < threads.length; k++) {
const thread = threads[k];
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);
}
}
}
addBacklinks(thread, true);
}
/*
temporary, jsut seeing how well this works
*/
const buildArray = [];
for (let i = startpage; i <= endpage; i++) {
//console.log('multi building board page', `${board._id}/${i === 1 ? 'index' : i}.html`);

@ -3,12 +3,11 @@
process.on('uncaughtException', console.error);
process.on('unhandledRejection', console.error);
const express = require('express')
, session = require('express-session')
const express = require('express')
, session = require('express-session')
, MongoStore = require('connect-mongo')(session)
, path = require('path')
, app = express()
, helmet = require('helmet')
, path = require('path')
, app = express()
, bodyParser = require('body-parser')
, cookieParser = require('cookie-parser')
, configs = require(__dirname+'/configs/main.json')
@ -19,12 +18,14 @@ const express = require('express')
// let db connect
await Mongo.connect();
// parse forms and allow file uploads
// parse forms
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//parse cookies
app.use(cookieParser());
// session store
app.set('trust proxy', 1);
app.use(session({
secret: configs.sessionSecret,
store: new MongoStore({ db: Mongo.client.db('sessions') }),
@ -36,10 +37,9 @@ const express = require('express')
sameSite: 'lax',
}
}));
app.use(cookieParser());
// csurf and helmet
app.use(helmet());
//trust proxy for nginx
app.set('trust proxy', 1);
//referer header check
app.use((req, res, next) => {
@ -84,11 +84,11 @@ const express = require('express')
// listen
const server = app.listen(configs.port, '127.0.0.1', () => {
console.log(`Listening on port ${configs.port}`);
console.log(`listening on port ${configs.port}`);
//let PM2 know that this is ready (for graceful reloads)
if (typeof process.send === 'function') { //make sure we are a child process
console.info('Sending ready signal to PM2')
console.info('sending ready signal to PM2')
process.send('ready');
}
@ -96,7 +96,7 @@ const express = require('express')
process.on('SIGINT', () => {
console.info('SIGINT signal received.')
console.info('SIGINT signal received')
// Stops the server from accepting new connections and finishes existing connections.
server.close((err) => {
@ -109,6 +109,7 @@ const express = require('express')
}
// close database connection
console.info('closing db connection')
Mongo.client.close();
// now close without error

Loading…
Cancel
Save