]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
21b5c298 | 2 | import { param } from 'express-validator' |
d4a8e7a6 | 3 | import { isIdValid } from '@server/helpers/custom-validators/misc' |
21b5c298 | 4 | import { checkUserCanTerminateOwnershipChange } from '@server/helpers/custom-validators/video-ownership' |
21b5c298 C |
5 | import { AccountModel } from '@server/models/account/account' |
6 | import { MVideoWithAllFiles } from '@server/types/models' | |
c729caf6 | 7 | import { HttpStatusCode, UserRight, VideoChangeOwnershipAccept, VideoChangeOwnershipStatus, VideoState } from '@shared/models' |
21b5c298 C |
8 | import { |
9 | areValidationErrors, | |
10 | checkUserCanManageVideo, | |
c729caf6 | 11 | checkUserQuota, |
21b5c298 C |
12 | doesChangeVideoOwnershipExist, |
13 | doesVideoChannelOfAccountExist, | |
d4a8e7a6 C |
14 | doesVideoExist, |
15 | isValidVideoIdParam | |
21b5c298 C |
16 | } from '../shared' |
17 | ||
18 | const videosChangeOwnershipValidator = [ | |
d4a8e7a6 | 19 | isValidVideoIdParam('videoId'), |
21b5c298 C |
20 | |
21 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
21b5c298 C |
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 = [ | |
d4a8e7a6 | 40 | param('id') |
396f6f01 | 41 | .custom(isIdValid), |
21b5c298 C |
42 | |
43 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
21b5c298 C |
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 | ||
c729caf6 | 104 | if (!await checkUserQuota(user, video.getMaxQualityFile().size, res)) return false |
21b5c298 C |
105 | |
106 | return true | |
107 | } |