// post.js // Copyright (C) 2021 Digital Telepresence, LLC // License: Apache-2.0 'use strict'; const express = require('express'); // const multer = require('multer'); const { SiteController, SiteError } = require('../../lib/site-lib'); class AuthorController extends SiteController { constructor (dtp) { super(dtp, module.exports); } async start ( ) { const { dtp } = this; const { limiter: limiterService } = dtp.services; const { author: authorLimiter } = limiterService.config; // const upload = multer({ dest: `/tmp/dtp-sites/${this.dtp.config.site.domainKey}`}); const router = express.Router(); dtp.app.use('/author', router); router.use( async (req, res, next) => { res.locals.currentView = 'author'; if (!req.user || !req.user.permissions.canAuthorPages) { return next(new SiteError(403, 'Author privileges are required')); } return next(); }, ); router.get('/post', limiterService.createMiddleware(authorLimiter.getPostIndex), this.getPostHome.bind(this), ); router.get('/', limiterService.createMiddleware(authorLimiter.getIndex), this.getAuthorHome.bind(this), ); } async getPostHome (req, res, next) { const { post: postService } = this.dtp.services; try { res.locals.drafts = await postService.getForAuthor(req.user, ['draft'], { skip: 0, cpp: 5 }); res.locals.archive = await postService.getForAuthor(req.user, ['archived'], { skip: 0, cpp: 5 }); res.locals.pagination = this.getPaginationParameters(req, 20); res.locals.published = await postService.getForAuthor(req.user, ['published'], res.locals.pagination); this.log.debug('published posts for author', { count: res.locals.published.totalPostCount }); res.render('author/post/index'); } catch (error) { this.log.error('failed to render Author dashboard', { error }); return next(error); } } async getAuthorHome (req, res, next) { const { /*comment: commentService,*/ post: postService } = this.dtp.services; try { res.locals.drafts = await postService.getForAuthor(req.user, ['draft'], { skip: 0, cpp: 5 }); res.locals.published = await postService.getForAuthor(req.user, ['published'], { skip: 0, cpp: 5 }); res.locals.pagination = this.getPaginationParameters(req, 20); res.locals.authorComments = await postService.getCommentsForAuthor(req.user, res.locals.pagination); res.render('author/index'); } catch (error) { this.log.error('failed to render Author dashboard', { error }); return next(error); } } } module.exports = { slug: 'author', name: 'author', create: async (dtp) => { let controller = new AuthorController(dtp); return controller; }, };