]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/middlewares/video-channels.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / helpers / middlewares / video-channels.ts
CommitLineData
453e83ea
C
1import * as express from 'express'
2import { VideoChannelModel } from '../../models/video/video-channel'
26d6bf65 3import { MChannelAccountDefault } from '@server/types/models'
2d53be02 4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
3e753302 5
453e83ea
C
6async function doesLocalVideoChannelNameExist (name: string, res: express.Response) {
7 const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
3e753302 8
453e83ea
C
9 return processVideoChannelExist(videoChannel, res)
10}
3e753302 11
453e83ea
C
12async function doesVideoChannelIdExist (id: number, res: express.Response) {
13 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
3e753302 14
453e83ea
C
15 return processVideoChannelExist(videoChannel, res)
16}
17
18async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
19 const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)
20
21 return processVideoChannelExist(videoChannel, res)
3e753302
C
22}
23
24// ---------------------------------------------------------------------------
25
26export {
453e83ea
C
27 doesLocalVideoChannelNameExist,
28 doesVideoChannelIdExist,
29 doesVideoChannelNameWithHostExist
30}
31
0283eaac 32function processVideoChannelExist (videoChannel: MChannelAccountDefault, res: express.Response) {
453e83ea 33 if (!videoChannel) {
2d53be02 34 res.status(HttpStatusCode.NOT_FOUND_404)
453e83ea
C
35 .json({ error: 'Video channel not found' })
36 .end()
37
38 return false
39 }
40
41 res.locals.videoChannel = videoChannel
42 return true
3e753302 43}