refactor msTimes and use for ban times, scheduler and dates for posts/day

merge-requests/208/head
fatchan 5 years ago
parent 3abbe9b2c7
commit 4df62dcb09
  1. 25
      db/boards.js
  2. 4
      ecosystem.config.js
  3. 33
      helpers/build.js
  4. 10
      helpers/mstime.js
  5. 2
      models/forms/banposter.js
  6. 17
      schedules.js

@ -62,7 +62,7 @@ module.exports = {
getNextId: async (board) => {
const increment = await db.collection('counters').findOneAndUpdate(
const increment = await db.collection('boards').findOneAndUpdate(
{
'_id': board
},
@ -72,30 +72,13 @@ module.exports = {
}
},
{
'upsert': true
}
);
return increment.value.sequence_value;
},
deleteIncrement: async (board) => {
return db.collection('counters').findOneAndUpdate(
{
'_id': board
},
{
'$set': {
'projection': {
'sequence_value': 1
}
},
{
'upsert': true
}
);
return increment.value.sequence_value;
}
},
}

@ -18,8 +18,8 @@ module.exports = {
NODE_ENV: 'production'
}
}, {
name: 'deleter',
script: 'deletescheduler.js',
name: 'schedules',
script: 'schedules.js',
instances: 1,
autorestart: true,
watch: false,

@ -1,6 +1,8 @@
'use strict';
const Posts = require(__dirname+'/../db/posts.js')
const Mongo = require(__dirname+'/../db/db.js')
, msTime = require(__dirname+'/mstime.js')
, Posts = require(__dirname+'/../db/posts.js')
, Boards = require(__dirname+'/../db/boards.js')
, uploadDirectory = require(__dirname+'/files/uploadDirectory.js')
, render = require(__dirname+'/render.js');
@ -76,10 +78,9 @@ console.log('building thread', `${board._id || board}/thread/${threadId}.html`);
buildBoard: async (board, page, maxPage=null) => {
console.log('building board page', `${board._id}/${page === 1 ? 'index' : page}.html`);
const threads = await Posts.getRecent(board._id, page);
if (!maxPage) {
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);
@ -131,6 +132,32 @@ console.log('multi building board pages', `${board._id}/ ${startpage === 1 ? 'in
buildHomepage: async () => {
const boards = await Boards.find();
const yesterday = Math.floor((Date.now() - msTime.day)/1000);
const yesterdayObjectId = Mongo.ObjectId.createFromTime(yesterday);
const ppd = await Posts.db.aggregate([
{
'$match': {
'_id': {
'$gt': yesterdayObjectId
}
}
},
{
'$group': {
'_id': '$board',
'ppd': { '$sum': 1 },
}
},
]).toArray().then(res => {
return res.reduce((acc, item) => {
acc[item._id] = item.ppd;
return acc;
}, {});
});
for (let i = 0; i < boards.length; i++) {
const board = boards[i];
board.ppd = ppd[board._id] || 0;
}
return render('index.html', 'home.pug', {
boards,
});

@ -0,0 +1,10 @@
'use strict';
module.exports = { //times in milliseconds
'year': 31536000000,
'month': 2592000000,
'week': 604800000,
'day': 86400000,
'hour': 3600000,
'minute': 60000
};

@ -5,7 +5,7 @@ const Bans = require(__dirname+'/../../db/bans.js')
module.exports = async (req, res, next) => {
const banDate = new Date();
const banExpiry = new Date(req.body.ban_duration ? banDate.getTime() + req.body.ban_duration : 8640000000000000);
const banExpiry = new Date(req.body.ban_duration ? banDate.getTime() + req.body.ban_duration : 8640000000000000); //perm if none or malformed input
const banReason = req.body.ban_reason || 'No reason specified';
const banBoard = req.body.global_ban ? null : req.params.board;

@ -5,7 +5,9 @@ const util = require('util')
, unlink = util.promisify(fs.unlink)
, stat = util.promisify(fs.stat)
, readdir = util.promisify(fs.readdir)
, uploadDirectory = require(__dirname+'/helpers/uploadDirectory.js');
, uploadDirectory = require(__dirname+'/helpers/files/uploadDirectory.js')
, msTime = require(__dirname+'/helpers/mstime.js')
, Mongo = require(__dirname+'/db/db.js')
async function deleteCaptchas() {
@ -31,5 +33,14 @@ async function deleteCaptchas() {
}
deleteCaptchas();
setInterval(deleteCaptchas, 6*1000*60);
(async () => {
await Mongo.connect();
const { buildHomepage } = require(__dirname+'/helpers/build.js');
buildHomepage();
setInterval(buildHomepage, msTime.hour); //hourly rebuild homepage for posts/day
deleteCaptchas();
setInterval(deleteCaptchas, msTime.minute*6); //delete files for expired captchas
})();
Loading…
Cancel
Save