diff --git a/db/modlogs.js b/db/modlogs.js index 0c49b495..be491d85 100644 --- a/db/modlogs.js +++ b/db/modlogs.js @@ -5,16 +5,39 @@ const Mongo = require(__dirname+'/db.js') module.exports = { - getFirst: (board) => { - return db.find({ - 'board': board._id - }).sort({_id:1}).limit(1).toArray(); - }, - - getLast: (board) => { - return db.find({ - 'board': board._id - }).sort({_id:-1}).limit(1).toArray(); + getDates: (board) => { + return db.aggregate([ + { + '$match': { + 'board': board._id + } + }, + { + '$project': { + 'year': { + '$year': '$_id' + }, + 'month': { + '$month': '$_id' + }, + 'day': { + '$dayOfMonth': '$_id' + } + } + }, + { + '$group': { + '_id': null, + 'dates': { + '$addToSet': { + 'year': '$year', + 'month': '$month', + 'day': '$day', + } + } + } + }, + ]).toArray().then(res => res[0].dates); }, findBetweenDate: (board, start, end) => { diff --git a/gulpfile.js b/gulpfile.js index e1fced30..79c797eb 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -96,6 +96,8 @@ async function wipe() { , Captchas.db.dropIndexes() , Ratelimits.db.dropIndexes() , Posts.db.dropIndexes() + , Modlogs.db.dropIndexes() + , Modlogs.db.createIndex({ 'board': 1 }) , Files.db.createIndex({ 'count': 1 }) , Bans.db.createIndex({ "expireAt": 1 }, { expireAfterSeconds: 0 }) //custom expiry, i.e. it will expire when current date > than this date , Captchas.db.createIndex({ "expireAt": 1 }, { expireAfterSeconds: 300 }) //captchas valid for 5 minutes diff --git a/helpers/build.js b/helpers/build.js index 4247602f..8bd2110f 100644 --- a/helpers/build.js +++ b/helpers/build.js @@ -2,7 +2,6 @@ const Mongo = require(__dirname+'/../db/db.js') , msTime = require(__dirname+'/mstime.js') - , dateArray = require(__dirname+'/datearray.js') , { Posts, Files, Boards, News, Modlogs } = require(__dirname+'/../db/') , render = require(__dirname+'/render.js'); @@ -136,17 +135,8 @@ module.exports = { buildModLogList: async (board) => { const label = `/${board._id}/logs.html`; console.time(label); - let dates = [] - const [ firstLog, lastLog ] = await Promise.all([ - Modlogs.getFirst(board), - Modlogs.getLast(board) - ]); - if (firstLog.length > 0 && lastLog.length > 0) { - const firstLogDate = firstLog[0].date; - firstLogDate.setHours(1,0,0,0); - const lastLogDate = lastLog[0].date; - dates = dateArray(firstLogDate, lastLogDate).reverse(); - } + const dates = (await Modlogs.getDates(board)).reverse(); +console.log(dates) await render(label, 'modloglist.pug', { board, dates diff --git a/models/pages/modloglist.js b/models/pages/modloglist.js index 8bb3b81b..67996628 100644 --- a/models/pages/modloglist.js +++ b/models/pages/modloglist.js @@ -1,8 +1,6 @@ 'use strict'; -const { Posts, Modlogs } = require(__dirname+'/../../db/') - , { buildModLogList } = require(__dirname+'/../../helpers/build.js') - , dateArray = require(__dirname+'/../../helpers/datearray.js') +const { buildModLogList } = require(__dirname+'/../../helpers/build.js') , uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js'); module.exports = async (req, res, next) => { diff --git a/views/pages/modloglist.pug b/views/pages/modloglist.pug index d6913c1b..e9c8cf09 100644 --- a/views/pages/modloglist.pug +++ b/views/pages/modloglist.pug @@ -24,9 +24,9 @@ block content for date in dates tr - - const day = ('0'+date.getDate()).slice(-2); - const month = ('0'+(date.getMonth()+1)).slice(-2); - const year = date.getFullYear(); + const day = ('0'+date.day).slice(-2); + const month = ('0'+date.month).slice(-2); + const year = date.year; const logName = `${month}-${day}-${year}`; td: a(href=`logs/${logName}.html`) #{logName} hr(size=1)