]>
Commit | Line | Data |
---|---|---|
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 { 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 { | |
10 | HttpStatusCode, | |
11 | ServerErrorCode, | |
12 | UserRight, | |
13 | VideoChangeOwnershipAccept, | |
14 | VideoChangeOwnershipStatus, | |
15 | VideoState | |
16 | } from '@shared/models' | |
17 | import { | |
18 | areValidationErrors, | |
19 | checkUserCanManageVideo, | |
20 | doesChangeVideoOwnershipExist, | |
21 | doesVideoChannelOfAccountExist, | |
22 | doesVideoExist, | |
23 | isValidVideoIdParam | |
24 | } from '../shared' | |
25 | ||
26 | const videosChangeOwnershipValidator = [ | |
27 | isValidVideoIdParam('videoId'), | |
28 | ||
29 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
30 | logger.debug('Checking changeOwnership parameters', { parameters: req.params }) | |
31 | ||
32 | if (areValidationErrors(req, res)) return | |
33 | if (!await doesVideoExist(req.params.videoId, res)) return | |
34 | ||
35 | // Check if the user who did the request is able to change the ownership of the video | |
36 | if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.CHANGE_VIDEO_OWNERSHIP, res)) return | |
37 | ||
38 | const nextOwner = await AccountModel.loadLocalByName(req.body.username) | |
39 | if (!nextOwner) { | |
40 | res.fail({ message: 'Changing video ownership to a remote account is not supported yet' }) | |
41 | return | |
42 | } | |
43 | ||
44 | res.locals.nextOwner = nextOwner | |
45 | return next() | |
46 | } | |
47 | ] | |
48 | ||
49 | const videosTerminateChangeOwnershipValidator = [ | |
50 | param('id') | |
51 | .custom(isIdValid).withMessage('Should have a valid id'), | |
52 | ||
53 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
54 | logger.debug('Checking changeOwnership parameters', { parameters: req.params }) | |
55 | ||
56 | if (areValidationErrors(req, res)) return | |
57 | if (!await doesChangeVideoOwnershipExist(req.params.id, res)) return | |
58 | ||
59 | // Check if the user who did the request is able to change the ownership of the video | |
60 | if (!checkUserCanTerminateOwnershipChange(res.locals.oauth.token.User, res.locals.videoChangeOwnership, res)) return | |
61 | ||
62 | const videoChangeOwnership = res.locals.videoChangeOwnership | |
63 | ||
64 | if (videoChangeOwnership.status !== VideoChangeOwnershipStatus.WAITING) { | |
65 | res.fail({ | |
66 | status: HttpStatusCode.FORBIDDEN_403, | |
67 | message: 'Ownership already accepted or refused' | |
68 | }) | |
69 | return | |
70 | } | |
71 | ||
72 | return next() | |
73 | } | |
74 | ] | |
75 | ||
76 | const videosAcceptChangeOwnershipValidator = [ | |
77 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
78 | const body = req.body as VideoChangeOwnershipAccept | |
79 | if (!await doesVideoChannelOfAccountExist(body.channelId, res.locals.oauth.token.User, res)) return | |
80 | ||
81 | const videoChangeOwnership = res.locals.videoChangeOwnership | |
82 | ||
83 | const video = videoChangeOwnership.Video | |
84 | ||
85 | if (!await checkCanAccept(video, res)) return | |
86 | ||
87 | return next() | |
88 | } | |
89 | ] | |
90 | ||
91 | export { | |
92 | videosChangeOwnershipValidator, | |
93 | videosTerminateChangeOwnershipValidator, | |
94 | videosAcceptChangeOwnershipValidator | |
95 | } | |
96 | ||
97 | // --------------------------------------------------------------------------- | |
98 | ||
99 | async function checkCanAccept (video: MVideoWithAllFiles, res: express.Response): Promise<boolean> { | |
100 | if (video.isLive) { | |
101 | ||
102 | if (video.state !== VideoState.WAITING_FOR_LIVE) { | |
103 | res.fail({ | |
104 | status: HttpStatusCode.BAD_REQUEST_400, | |
105 | message: 'You can accept an ownership change of a published live.' | |
106 | }) | |
107 | ||
108 | return false | |
109 | } | |
110 | ||
111 | return true | |
112 | } | |
113 | ||
114 | const user = res.locals.oauth.token.User | |
115 | ||
116 | if (!await isAbleToUploadVideo(user.id, video.getMaxQualityFile().size)) { | |
117 | res.fail({ | |
118 | status: HttpStatusCode.PAYLOAD_TOO_LARGE_413, | |
119 | message: 'The user video quota is exceeded with this video.', | |
120 | type: ServerErrorCode.QUOTA_REACHED | |
121 | }) | |
122 | ||
123 | return false | |
124 | } | |
125 | ||
126 | return true | |
127 | } |