// post.js // Copyright (C) 2021 Digital Telepresence, LLC // License: Apache-2.0 'use strict'; const DTP_COMPONENT_NAME = 'post'; const express = require('express'); const multer = require('multer'); const { SiteController, SiteError } = require('../../lib/site-lib'); class PostController extends SiteController { constructor (dtp) { super(dtp, DTP_COMPONENT_NAME); } async start ( ) { const { dtp } = this; const { comment: commentService, limiter: limiterService, session: sessionService, } = dtp.services; const authRequired = sessionService.authCheckMiddleware({ requiredLogin: true }); const upload = multer({ dest: `/tmp/dtp-sites/${this.dtp.config.site.domainKey}`}); const router = express.Router(); dtp.app.use('/post', router); router.use(this.dtp.services.gabTV.channelMiddleware()); router.use(async (req, res, next) => { res.locals.currentView = 'home'; return next(); }); router.param('postSlug', this.populatePostSlug.bind(this)); router.param('commentId', commentService.populateCommentId.bind(commentService)); router.post('/:postSlug/comment/:commentId/block-author', authRequired, upload.none(), this.postBlockCommentAuthor.bind(this)); router.post('/:postSlug/comment', authRequired, upload.none(), this.postComment.bind(this)); router.get('/:postSlug/comment', limiterService.create(limiterService.config.post.getComments), this.getComments.bind(this), ); router.get('/:postSlug', limiterService.create(limiterService.config.post.getView), this.getView.bind(this), ); router.get('/', limiterService.create(limiterService.config.post.getIndex), this.getIndex.bind(this), ); } async populatePostSlug (req, res, next, postSlug) { const { post: postService } = this.dtp.services; try { res.locals.post = await postService.getBySlug(postSlug); if (!res.locals.post) { throw new SiteError(404, 'Post not found'); } return next(); } catch (error) { this.log.error('failed to populate postSlug', { postSlug, error }); return next(error); } } async postBlockCommentAuthor (req, res) { const { user: userService } = this.dtp.services; try { const displayList = this.createDisplayList('add-recipient'); await userService.blockUser(req.user._id, req.body.userId); displayList.showNotification( 'Comment author blocked', 'success', 'bottom-center', 4000, ); res.status(200).json({ success: true, displayList }); } catch (error) { this.log.error('failed to report comment', { error }); return res.status(error.statusCode || 500).json({ success: false, message: error.message, }); } } async postComment (req, res) { const { comment: commentService } = this.dtp.services; try { const displayList = this.createDisplayList('add-recipient'); res.locals.comment = await commentService.create(req.user, 'Post', res.locals.post, req.body); displayList.setInputValue('textarea#content', ''); displayList.setTextContent('#comment-character-count', '0'); let viewModel = Object.assign({ }, req.app.locals); viewModel = Object.assign(viewModel, res.locals); const html = await commentService.renderTemplate('comment', viewModel); if (req.body.replyTo) { const replyListSelector = `.dtp-reply-list-container[data-comment-id="${req.body.replyTo}"]`; displayList.addElement(replyListSelector, 'afterBegin', html); displayList.removeAttribute(replyListSelector, 'hidden'); } else { displayList.addElement('ul#post-comment-list', 'afterBegin', html); } displayList.showNotification( 'Comment created', 'success', 'bottom-center', 4000, ); res.status(200).json({ success: true, displayList }); } catch (error) { res.status(error.statusCode || 500).json({ success: false, message: error.message }); } } async getComments (req, res) { const { comment: commentService } = this.dtp.services; try { const displayList = this.createDisplayList('add-recipient'); if (req.query.buttonId) { displayList.removeElement(`li.dtp-load-more[data-button-id="${req.query.buttonId}"]`); } Object.assign(res.locals, req.app.locals); res.locals.countPerPage = parseInt(req.query.cpp || "20", 10); if (res.locals.countPerPage < 1) { res.locals.countPerPage = 1; } if (res.locals.countPerPage > 20) { res.locals.countPerPage = 20; } res.locals.pagination = this.getPaginationParameters(req, res.locals.countPerPage); res.locals.comments = await commentService.getForResource( res.locals.post, ['published', 'mod-warn', 'mod-removed', 'removed'], res.locals.pagination, ); const html = await commentService.renderTemplate('commentList', res.locals); const replyList = `ul#post-comment-list`; displayList.addElement(replyList, 'beforeEnd', html); res.status(200).json({ success: true, displayList }); } catch (error) { this.log.error('failed to fetch more commnets', { postId: res.locals.post._id, error }); return res.status(error.statusCode || 500).json({ success: false, message: error.message, }); } } async getView (req, res, next) { const { comment: commentService, resource: resourceService } = this.dtp.services; try { await resourceService.recordView(req, 'Post', res.locals.post._id); res.locals.countPerPage = 20; res.locals.pagination = this.getPaginationParameters(req, res.locals.countPerPage); if (req.query.comment) { res.locals.featuredComment = await commentService.getById(req.query.comment); } res.locals.comments = await commentService.getForResource( res.locals.post, ['published', 'mod-warn', 'mod-removed', 'removed'], res.locals.pagination, ); res.render('post/view'); } catch (error) { this.log.error('failed to service post view', { postId: res.locals.post._id, error }); return next(error); } } async getIndex (req, res, next) { const { post: postService } = this.dtp.services; try { res.locals.pagination = this.getPaginationParameters(req, 20); res.locals.posts = await postService.getPosts(res.locals.pagination); res.render('post/index'); } catch (error) { return next(error); } } } module.exports = { slug: 'post', name: 'post', create: async (dtp) => { let controller = new PostController(dtp); return controller; }, };