From 5de8a55abce53108bc1024f1194457c6528bd11e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 20 Feb 2018 10:41:11 +0100 Subject: [PATCH] Handle line feeds in comments --- client/src/app/shared/misc/utils.ts | 9 ++++++++- .../+video-watch/comment/video-comment.component.ts | 2 +- .../videos/+video-watch/comment/video-comment.service.ts | 7 +++++-- server/middlewares/validators/video-comments.ts | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/client/src/app/shared/misc/utils.ts b/client/src/app/shared/misc/utils.ts index e2e4c5b36..64bc69b0d 100644 --- a/client/src/app/shared/misc/utils.ts +++ b/client/src/app/shared/misc/utils.ts @@ -88,6 +88,12 @@ function objectToFormData (obj: any, form?: FormData, namespace?: string) { return fd } +function lineFeedToHtml (obj: object, keyToNormalize: string) { + return immutableAssign(obj, { + [keyToNormalize]: obj[keyToNormalize].replace(/\r?\n|\r/g, '
') + }) +} + export { viewportHeight, getParameterByName, @@ -97,5 +103,6 @@ export { isInSmallView, isInMobileView, immutableAssign, - objectToFormData + objectToFormData, + lineFeedToHtml } diff --git a/client/src/app/videos/+video-watch/comment/video-comment.component.ts b/client/src/app/videos/+video-watch/comment/video-comment.component.ts index 7c664ca60..9176de80f 100644 --- a/client/src/app/videos/+video-watch/comment/video-comment.component.ts +++ b/client/src/app/videos/+video-watch/comment/video-comment.component.ts @@ -90,7 +90,7 @@ export class VideoCommentComponent implements OnInit, OnChanges { private init () { this.sanitizedCommentHTML = sanitizeHtml(this.comment.text, { - allowedTags: [ 'p', 'span' ] + allowedTags: [ 'p', 'span', 'br' ] }) this.newParentComments = this.parentComments.concat([ this.comment ]) diff --git a/client/src/app/videos/+video-watch/comment/video-comment.service.ts b/client/src/app/videos/+video-watch/comment/video-comment.service.ts index c42f55496..14d32b1aa 100644 --- a/client/src/app/videos/+video-watch/comment/video-comment.service.ts +++ b/client/src/app/videos/+video-watch/comment/video-comment.service.ts @@ -2,6 +2,7 @@ import { HttpClient, HttpParams } from '@angular/common/http' import { Injectable } from '@angular/core' import 'rxjs/add/operator/catch' import 'rxjs/add/operator/map' +import { immutableAssign, lineFeedToHtml } from '@app/shared/misc/utils' import { Observable } from 'rxjs/Observable' import { ResultList } from '../../../../../../shared/models' import { @@ -26,16 +27,18 @@ export class VideoCommentService { addCommentThread (videoId: number | string, comment: VideoCommentCreate) { const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads' + const normalizedComment = lineFeedToHtml(comment, 'text') - return this.authHttp.post(url, comment) + return this.authHttp.post(url, normalizedComment) .map(data => this.extractVideoComment(data['comment'])) .catch(this.restExtractor.handleError) } addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) { const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId + const normalizedComment = lineFeedToHtml(comment, 'text') - return this.authHttp.post(url, comment) + return this.authHttp.post(url, normalizedComment) .map(data => this.extractVideoComment(data['comment'])) .catch(this.restExtractor.handleError) } diff --git a/server/middlewares/validators/video-comments.ts b/server/middlewares/validators/video-comments.ts index 63804da30..227bc1fca 100644 --- a/server/middlewares/validators/video-comments.ts +++ b/server/middlewares/validators/video-comments.ts @@ -43,7 +43,7 @@ const addVideoCommentThreadValidator = [ body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params }) + logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params, body: req.body }) if (areValidationErrors(req, res)) return if (!await isVideoExist(req.params.videoId, res)) return @@ -59,7 +59,7 @@ const addVideoCommentReplyValidator = [ body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params }) + logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params, body: req.body }) if (areValidationErrors(req, res)) return if (!await isVideoExist(req.params.videoId, res)) return