mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 19:42:24 +02:00
Move middleware utils in middlewares
helpers modules should not import models
This commit is contained in:
parent
5e08989ede
commit
10363c74c1
68 changed files with 345 additions and 342 deletions
|
@ -1,9 +1,5 @@
|
|||
import * as express from 'express'
|
||||
import validator from 'validator'
|
||||
import { VideoCommentModel } from '@server/models/video/video-comment'
|
||||
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
||||
import { MVideoId } from '@server/types/models'
|
||||
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
||||
|
||||
const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
|
||||
|
||||
|
@ -11,74 +7,8 @@ function isValidVideoCommentText (value: string) {
|
|||
return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT)
|
||||
}
|
||||
|
||||
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 {
|
||||
isValidVideoCommentText,
|
||||
doesVideoCommentThreadExist,
|
||||
doesVideoCommentExist,
|
||||
doesCommentIdExist
|
||||
isValidVideoCommentText
|
||||
}
|
||||
|
|
|
@ -2,9 +2,6 @@ import 'multer'
|
|||
import validator from 'validator'
|
||||
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants'
|
||||
import { exists, isFileValid } from './misc'
|
||||
import * as express from 'express'
|
||||
import { VideoImportModel } from '../../models/video/video-import'
|
||||
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
||||
|
||||
function isVideoImportTargetUrlValid (url: string) {
|
||||
const isURLOptions = {
|
||||
|
@ -32,26 +29,10 @@ function isVideoImportTorrentFile (files: { [ fieldname: string ]: Express.Multe
|
|||
return isFileValid(files, videoTorrentImportRegex, 'torrentfile', CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.FILE_SIZE.max, true)
|
||||
}
|
||||
|
||||
async function doesVideoImportExist (id: number, res: express.Response) {
|
||||
const videoImport = await VideoImportModel.loadAndPopulateVideo(id)
|
||||
|
||||
if (!videoImport) {
|
||||
res.fail({
|
||||
status: HttpStatusCode.NOT_FOUND_404,
|
||||
message: 'Video import not found'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoImport = videoImport
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
isVideoImportStateValid,
|
||||
isVideoImportTargetUrlValid,
|
||||
doesVideoImportExist,
|
||||
isVideoImportTorrentFile
|
||||
}
|
||||
|
|
|
@ -1,26 +1,9 @@
|
|||
import { Response } from 'express'
|
||||
import { VideoChangeOwnershipModel } from '../../models/video/video-change-ownership'
|
||||
import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership'
|
||||
import { MUserId } from '@server/types/models'
|
||||
import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership'
|
||||
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
||||
|
||||
export async function doesChangeVideoOwnershipExist (idArg: number | string, res: Response) {
|
||||
const id = parseInt(idArg + '', 10)
|
||||
const videoChangeOwnership = await VideoChangeOwnershipModel.load(id)
|
||||
|
||||
if (!videoChangeOwnership) {
|
||||
res.fail({
|
||||
status: HttpStatusCode.NOT_FOUND_404,
|
||||
message: 'Video change ownership not found'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoChangeOwnership = videoChangeOwnership
|
||||
return true
|
||||
}
|
||||
|
||||
export function checkUserCanTerminateOwnershipChange (user: MUserId, videoChangeOwnership: MVideoChangeOwnershipFull, res: Response) {
|
||||
function checkUserCanTerminateOwnershipChange (user: MUserId, videoChangeOwnership: MVideoChangeOwnershipFull, res: Response) {
|
||||
if (videoChangeOwnership.NextOwner.userId === user.id) {
|
||||
return true
|
||||
}
|
||||
|
@ -31,3 +14,7 @@ export function checkUserCanTerminateOwnershipChange (user: MUserId, videoChange
|
|||
})
|
||||
return false
|
||||
}
|
||||
|
||||
export {
|
||||
checkUserCanTerminateOwnershipChange
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue