From 10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 3 Jun 2021 17:33:44 +0200 Subject: Move middleware utils in middlewares helpers modules should not import models --- .../validators/shared/video-comments.ts | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 server/middlewares/validators/shared/video-comments.ts (limited to 'server/middlewares/validators/shared/video-comments.ts') diff --git a/server/middlewares/validators/shared/video-comments.ts b/server/middlewares/validators/shared/video-comments.ts new file mode 100644 index 000000000..83ea15c98 --- /dev/null +++ b/server/middlewares/validators/shared/video-comments.ts @@ -0,0 +1,73 @@ +import * as express from 'express' +import { VideoCommentModel } from '@server/models/video/video-comment' +import { MVideoId } from '@server/types/models' +import { HttpStatusCode } from '@shared/core-utils' + +async function doesVideoCommentThreadExist (idArg: number | string, video: MVideoId, res: express.Response) { + const id = parseInt(idArg + '', 10) + const videoComment = await VideoCommentModel.loadById(id) + + if (!videoComment) { + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video comment thread not found' + }) + return false + } + + if (videoComment.videoId !== video.id) { + res.fail({ message: 'Video comment is not associated to this video.' }) + return false + } + + if (videoComment.inReplyToCommentId !== null) { + res.fail({ message: 'Video comment is not a thread.' }) + return false + } + + res.locals.videoCommentThread = videoComment + return true +} + +async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) { + const id = parseInt(idArg + '', 10) + const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) + + if (!videoComment) { + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video comment thread not found' + }) + return false + } + + if (videoComment.videoId !== video.id) { + res.fail({ message: 'Video comment is not associated to this video.' }) + return false + } + + res.locals.videoCommentFull = videoComment + return true +} + +async function doesCommentIdExist (idArg: number | string, res: express.Response) { + const id = parseInt(idArg + '', 10) + const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) + + if (!videoComment) { + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video comment thread not found' + }) + return false + } + + res.locals.videoCommentFull = videoComment + return true +} + +export { + doesVideoCommentThreadExist, + doesVideoCommentExist, + doesCommentIdExist +} -- cgit v1.2.3