You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.6 KiB
118 lines
3.6 KiB
3 years ago
|
// comment.js
|
||
|
// Copyright (C) 2021 Digital Telepresence, LLC
|
||
|
// License: Apache-2.0
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
const DTP_COMPONENT_NAME = 'comment';
|
||
|
|
||
|
const express = require('express');
|
||
|
const numeral = require('numeral');
|
||
|
|
||
|
const { SiteController, SiteError } = require('../../lib/site-lib');
|
||
|
|
||
|
class CommentController extends SiteController {
|
||
|
|
||
|
constructor (dtp) {
|
||
|
super(dtp, DTP_COMPONENT_NAME);
|
||
|
}
|
||
|
|
||
|
async start ( ) {
|
||
|
const { dtp } = this;
|
||
|
const { limiter: limiterService, session: sessionService } = dtp.services;
|
||
|
|
||
|
const authRequired = sessionService.authCheckMiddleware({ requiredLogin: true });
|
||
|
|
||
|
const router = express.Router();
|
||
|
dtp.app.use('/comment', router);
|
||
|
|
||
|
router.use(async (req, res, next) => {
|
||
|
res.locals.currentView = DTP_COMPONENT_NAME;
|
||
|
return next();
|
||
|
});
|
||
|
|
||
|
router.param('commentId', this.populateCommentId.bind(this));
|
||
|
|
||
|
router.post('/:commentId/vote', authRequired, this.postVote.bind(this));
|
||
|
|
||
|
router.delete('/:commentId',
|
||
|
authRequired,
|
||
|
limiterService.create(limiterService.config.comment.deleteComment),
|
||
|
this.deleteComment.bind(this),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
async populateCommentId (req, res, next, commentId) {
|
||
|
const { comment: commentService } = this.dtp.services;
|
||
|
try {
|
||
|
res.locals.comment = await commentService.getById(commentId);
|
||
|
if (!res.locals.comment) {
|
||
|
return next(new SiteError(404, 'Comment not found'));
|
||
|
}
|
||
|
return next();
|
||
|
} catch (error) {
|
||
|
this.log.error('failed to populate commentId', { commentId, error });
|
||
|
return next(error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async postVote (req, res) {
|
||
|
const { contentVote: contentVoteService } = this.dtp.services;
|
||
|
try {
|
||
|
const displayList = this.createDisplayList('comment-vote');
|
||
|
const { message, stats } = await contentVoteService.recordVote(req.user, 'Comment', res.locals.comment, req.body.vote);
|
||
|
displayList.setTextContent(
|
||
|
`button[data-comment-id="${res.locals.comment._id}"][data-vote="up"] span.dtp-item-value`,
|
||
|
numeral(stats.upvoteCount).format(stats.upvoteCount > 1000 ? '0,0.0a' : '0,0'),
|
||
|
);
|
||
|
displayList.setTextContent(
|
||
|
`button[data-comment-id="${res.locals.comment._id}"][data-vote="down"] span.dtp-item-value`,
|
||
|
numeral(stats.downvoteCount).format(stats.upvoteCount > 1000 ? '0,0.0a' : '0,0'),
|
||
|
);
|
||
|
displayList.showNotification(message, 'success', 'bottom-center', 3000);
|
||
|
res.status(200).json({ success: true, displayList });
|
||
|
} catch (error) {
|
||
|
this.log.error('failed to process comment vote', { error });
|
||
|
return res.status(error.statusCode || 500).json({
|
||
|
success: false,
|
||
|
message: error.message,
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async deleteComment (req, res) {
|
||
|
const { comment: commentService } = this.dtp.services;
|
||
|
try {
|
||
|
const displayList = this.createDisplayList('add-recipient');
|
||
|
|
||
|
await commentService.remove(res.locals.comment, 'removed');
|
||
|
|
||
|
let selector = `article[data-comment-id="${res.locals.comment._id}"] .comment-content`;
|
||
|
displayList.setTextContent(selector, 'Comment removed');
|
||
|
|
||
|
displayList.showNotification(
|
||
|
'Comment removed successfully',
|
||
|
'success',
|
||
|
'bottom-center',
|
||
|
5000,
|
||
|
);
|
||
|
|
||
|
res.status(200).json({ success: true, displayList });
|
||
|
} catch (error) {
|
||
|
this.log.error('failed to remove comment', { error });
|
||
|
return res.status(error.statusCode || 500).json({
|
||
|
success: false,
|
||
|
message: error.message
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
slug: 'comment',
|
||
|
name: 'comment',
|
||
|
create: async (dtp) => {
|
||
|
let controller = new CommentController(dtp);
|
||
|
return controller;
|
||
|
},
|
||
|
};
|