1 import * as express from 'express'
2 import { param } from 'express-validator'
3 import { isIdOrUUIDValid } from '@server/helpers/custom-validators/misc'
4 import { checkUserCanTerminateOwnershipChange } from '@server/helpers/custom-validators/video-ownership'
5 import { logger } from '@server/helpers/logger'
6 import { isAbleToUploadVideo } from '@server/lib/user'
7 import { AccountModel } from '@server/models/account/account'
8 import { MVideoWithAllFiles } from '@server/types/models'
9 import { HttpStatusCode } from '@shared/core-utils'
10 import { ServerErrorCode, UserRight, VideoChangeOwnershipAccept, VideoChangeOwnershipStatus, VideoState } from '@shared/models'
13 checkUserCanManageVideo,
14 doesChangeVideoOwnershipExist,
15 doesVideoChannelOfAccountExist,
19 const videosChangeOwnershipValidator = [
20 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking changeOwnership parameters', { parameters: req.params })
25 if (areValidationErrors(req, res)) return
26 if (!await doesVideoExist(req.params.videoId, res)) return
28 // Check if the user who did the request is able to change the ownership of the video
29 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.CHANGE_VIDEO_OWNERSHIP, res)) return
31 const nextOwner = await AccountModel.loadLocalByName(req.body.username)
33 res.fail({ message: 'Changing video ownership to a remote account is not supported yet' })
37 res.locals.nextOwner = nextOwner
42 const videosTerminateChangeOwnershipValidator = [
43 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
45 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 logger.debug('Checking changeOwnership parameters', { parameters: req.params })
48 if (areValidationErrors(req, res)) return
49 if (!await doesChangeVideoOwnershipExist(req.params.id, res)) return
51 // Check if the user who did the request is able to change the ownership of the video
52 if (!checkUserCanTerminateOwnershipChange(res.locals.oauth.token.User, res.locals.videoChangeOwnership, res)) return
54 const videoChangeOwnership = res.locals.videoChangeOwnership
56 if (videoChangeOwnership.status !== VideoChangeOwnershipStatus.WAITING) {
58 status: HttpStatusCode.FORBIDDEN_403,
59 message: 'Ownership already accepted or refused'
68 const videosAcceptChangeOwnershipValidator = [
69 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
70 const body = req.body as VideoChangeOwnershipAccept
71 if (!await doesVideoChannelOfAccountExist(body.channelId, res.locals.oauth.token.User, res)) return
73 const videoChangeOwnership = res.locals.videoChangeOwnership
75 const video = videoChangeOwnership.Video
77 if (!await checkCanAccept(video, res)) return
84 videosChangeOwnershipValidator,
85 videosTerminateChangeOwnershipValidator,
86 videosAcceptChangeOwnershipValidator
89 // ---------------------------------------------------------------------------
91 async function checkCanAccept (video: MVideoWithAllFiles, res: express.Response): Promise<boolean> {
94 if (video.state !== VideoState.WAITING_FOR_LIVE) {
96 status: HttpStatusCode.BAD_REQUEST_400,
97 message: 'You can accept an ownership change of a published live.'
106 const user = res.locals.oauth.token.User
108 if (!await isAbleToUploadVideo(user.id, video.getMaxQualityFile().size)) {
110 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
111 message: 'The user video quota is exceeded with this video.',
112 type: ServerErrorCode.QUOTA_REACHED