diff --git a/db/posts.js b/db/posts.js index 96fb48cc..8b457a45 100644 --- a/db/posts.js +++ b/db/posts.js @@ -474,6 +474,36 @@ module.exports = { }, + getBoardReportCounts: (boards) => { + return db.aggregate([ + { + '$match': { + 'board': { + '$in': boards + }, + 'reports.0': { + '$exists': true + }, + } + }, { + '$group': { + '_id': '$board', + 'count': { + '$sum': 1 + } + } + } + ]).toArray(); + }, + + getGlobalReportsCount: () => { + return db.countDocuments({ + 'globalreports.0': { + '$exists': true + } + }) + }, + getReports: (board) => { return db.find({ 'reports.0': { diff --git a/models/pages/account.js b/models/pages/account.js index 51a7c3c3..128ed615 100644 --- a/models/pages/account.js +++ b/models/pages/account.js @@ -1,12 +1,39 @@ 'use strict'; +const Posts = require(__dirname+'/../../db/posts.js'); + module.exports = async (req, res, next) => { + const userBoards = res.locals.user.ownedBoards.concat(res.locals.user.modBoards); + let boardReportCountMap = {}; + let globalReportCount = 0; + + if (userBoards.length > 0) { + let boardReportCounts; + try { + ([boardReportCounts, globalReportCount] = await Promise.all([ + Posts.getBoardReportCounts(userBoards), + res.locals.user.authLevel <= 1 ? Posts.getGlobalReportsCount() : 0 + ])); + } catch (err) { + return next(err) + } + + if (boardReportCounts && boardReportCounts.length > 0) { + boardReportCountMap = boardReportCounts.reduce((acc, val) => { + acc[val._id] = val.count; + return acc; + }, boardReportCountMap); + } + } + res .set('Cache-Control', 'private, max-age=5') .render('account', { csrf: req.csrfToken(), user: res.locals.user, + boardReportCountMap, + globalReportCount, }); } diff --git a/views/pages/account.pug b/views/pages/account.pug index f2669af2..a99ef7a9 100644 --- a/views/pages/account.pug +++ b/views/pages/account.pug @@ -16,6 +16,8 @@ block content a(href='/globalmanage/recent.html') Global management | - a(href=`/globalmanage/reports.html`) Reports + if globalReportCount > 0 + b (#{globalReportCount}) | , a(href=`/globalmanage/bans.html`) Bans | , @@ -52,6 +54,8 @@ block content a(href=`/${b}/manage/recent.html`) Recent | , a(href=`/${b}/manage/reports.html`) Reports + if boardReportCountMap[b] + b (#{boardReportCountMap[b]}) | , a(href=`/${b}/manage/bans.html`) Bans | , @@ -77,6 +81,8 @@ block content a(href=`/${b}/manage/recent.html`) Recent | , a(href=`/${b}/manage/reports.html`) Reports + if boardReportCountMap[b] + b (#{boardReportCountMap[b]}) | , a(href=`/${b}/manage/bans.html`) Bans | ,