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