]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-ownership-changes.ts
Translated using Weblate (Persian)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-ownership-changes.ts
1 import express from 'express'
2 import { param } from 'express-validator'
3 import { isIdValid } from '@server/helpers/custom-validators/misc'
4 import { checkUserCanTerminateOwnershipChange } from '@server/helpers/custom-validators/video-ownership'
5 import { AccountModel } from '@server/models/account/account'
6 import { MVideoWithAllFiles } from '@server/types/models'
7 import { HttpStatusCode, UserRight, VideoChangeOwnershipAccept, VideoChangeOwnershipStatus, VideoState } from '@shared/models'
8 import {
9 areValidationErrors,
10 checkUserCanManageVideo,
11 checkUserQuota,
12 doesChangeVideoOwnershipExist,
13 doesVideoChannelOfAccountExist,
14 doesVideoExist,
15 isValidVideoIdParam
16 } from '../shared'
17
18 const videosChangeOwnershipValidator = [
19 isValidVideoIdParam('videoId'),
20
21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
22 if (areValidationErrors(req, res)) return
23 if (!await doesVideoExist(req.params.videoId, res)) return
24
25 // Check if the user who did the request is able to change the ownership of the video
26 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.CHANGE_VIDEO_OWNERSHIP, res)) return
27
28 const nextOwner = await AccountModel.loadLocalByName(req.body.username)
29 if (!nextOwner) {
30 res.fail({ message: 'Changing video ownership to a remote account is not supported yet' })
31 return
32 }
33
34 res.locals.nextOwner = nextOwner
35 return next()
36 }
37 ]
38
39 const videosTerminateChangeOwnershipValidator = [
40 param('id')
41 .custom(isIdValid),
42
43 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 if (areValidationErrors(req, res)) return
45 if (!await doesChangeVideoOwnershipExist(req.params.id, res)) return
46
47 // Check if the user who did the request is able to change the ownership of the video
48 if (!checkUserCanTerminateOwnershipChange(res.locals.oauth.token.User, res.locals.videoChangeOwnership, res)) return
49
50 const videoChangeOwnership = res.locals.videoChangeOwnership
51
52 if (videoChangeOwnership.status !== VideoChangeOwnershipStatus.WAITING) {
53 res.fail({
54 status: HttpStatusCode.FORBIDDEN_403,
55 message: 'Ownership already accepted or refused'
56 })
57 return
58 }
59
60 return next()
61 }
62 ]
63
64 const videosAcceptChangeOwnershipValidator = [
65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 const body = req.body as VideoChangeOwnershipAccept
67 if (!await doesVideoChannelOfAccountExist(body.channelId, res.locals.oauth.token.User, res)) return
68
69 const videoChangeOwnership = res.locals.videoChangeOwnership
70
71 const video = videoChangeOwnership.Video
72
73 if (!await checkCanAccept(video, res)) return
74
75 return next()
76 }
77 ]
78
79 export {
80 videosChangeOwnershipValidator,
81 videosTerminateChangeOwnershipValidator,
82 videosAcceptChangeOwnershipValidator
83 }
84
85 // ---------------------------------------------------------------------------
86
87 async function checkCanAccept (video: MVideoWithAllFiles, res: express.Response): Promise<boolean> {
88 if (video.isLive) {
89
90 if (video.state !== VideoState.WAITING_FOR_LIVE) {
91 res.fail({
92 status: HttpStatusCode.BAD_REQUEST_400,
93 message: 'You can accept an ownership change of a published live.'
94 })
95
96 return false
97 }
98
99 return true
100 }
101
102 const user = res.locals.oauth.token.User
103
104 if (!await checkUserQuota(user, video.getMaxQualityFile().size, res)) return false
105
106 return true
107 }