]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/shared/videos.ts
Fetch directly all video attributes for get API
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / shared / videos.ts
CommitLineData
3e753302 1import { Response } from 'express'
868fce62 2import { loadVideo, VideoLoadType } from '@server/lib/model-loaders'
10363c74
C
3import { VideoChannelModel } from '@server/models/video/video-channel'
4import { VideoFileModel } from '@server/models/video/video-file'
7eba5e1f
C
5import {
6 MUser,
7 MUserAccountId,
8 MVideoAccountLight,
ca4b4b2e 9 MVideoFormattableDetails,
7eba5e1f
C
10 MVideoFullLight,
11 MVideoIdThumbnail,
12 MVideoImmutable,
13 MVideoThumbnail,
14 MVideoWithRights
26d6bf65 15} from '@server/types/models'
10363c74
C
16import { HttpStatusCode } from '@shared/core-utils'
17import { UserRight } from '@shared/models'
3e753302 18
868fce62 19async function doesVideoExist (id: number | string, res: Response, fetchType: VideoLoadType = 'all') {
3e753302
C
20 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
21
868fce62 22 const video = await loadVideo(id, fetchType, userId)
3e753302
C
23
24 if (video === null) {
76148b27
RK
25 res.fail({
26 status: HttpStatusCode.NOT_FOUND_404,
27 message: 'Video not found'
28 })
3e753302
C
29 return false
30 }
31
453e83ea 32 switch (fetchType) {
ca4b4b2e
C
33 case 'for-api':
34 res.locals.videoAPI = video as MVideoFormattableDetails
35 break
36
453e83ea
C
37 case 'all':
38 res.locals.videoAll = video as MVideoFullLight
39 break
40
7eba5e1f
C
41 case 'only-immutable-attributes':
42 res.locals.onlyImmutableVideo = video as MVideoImmutable
43 break
44
453e83ea 45 case 'id':
7eba5e1f 46 res.locals.videoId = video as MVideoIdThumbnail
453e83ea
C
47 break
48
49 case 'only-video':
0283eaac 50 res.locals.onlyVideo = video as MVideoThumbnail
453e83ea
C
51 break
52
53 case 'only-video-with-rights':
54 res.locals.onlyVideoWithRights = video as MVideoWithRights
55 break
56 }
57
3e753302
C
58 return true
59}
60
8319d6ae
RK
61async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
62 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
76148b27
RK
63 res.fail({
64 status: HttpStatusCode.NOT_FOUND_404,
65 message: 'VideoFile matching Video not found'
66 })
8319d6ae
RK
67 return false
68 }
69
70 return true
71}
72
453e83ea 73async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
2cb03dc1 74 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
3e753302 75
2cb03dc1 76 if (videoChannel === null) {
76148b27 77 res.fail({ message: 'Unknown video "video channel" for this instance.' })
2cb03dc1
C
78 return false
79 }
80
81 // Don't check account id if the user can update any video
82 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
3e753302
C
83 res.locals.videoChannel = videoChannel
84 return true
85 }
86
2cb03dc1 87 if (videoChannel.Account.id !== user.Account.id) {
76148b27
RK
88 res.fail({
89 message: 'Unknown video "video channel" for this account.'
90 })
3e753302
C
91 return false
92 }
93
94 res.locals.videoChannel = videoChannel
95 return true
96}
97
af4ae64f 98function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) {
3e753302 99 // Retrieve the user who did the request
af4ae64f 100 if (onlyOwned && video.isOwned() === false) {
76148b27
RK
101 res.fail({
102 status: HttpStatusCode.FORBIDDEN_403,
103 message: 'Cannot manage a video of another server.'
104 })
3e753302
C
105 return false
106 }
107
108 // Check if the user can delete the video
109 // The user can delete it if he has the right
110 // Or if s/he is the video's account
111 const account = video.VideoChannel.Account
112 if (user.hasRight(right) === false && account.userId !== user.id) {
76148b27
RK
113 res.fail({
114 status: HttpStatusCode.FORBIDDEN_403,
115 message: 'Cannot manage a video of another user.'
116 })
3e753302
C
117 return false
118 }
119
120 return true
121}
122
123// ---------------------------------------------------------------------------
124
125export {
126 doesVideoChannelOfAccountExist,
127 doesVideoExist,
8319d6ae 128 doesVideoFileOfVideoExist,
3e753302
C
129 checkUserCanManageVideo
130}