aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-28 11:54:40 +0200
committerChocobozzz <me@florianbigard.com>2021-06-28 11:54:40 +0200
commit21b5c2982ff93bc67d5dd4c04db352a3f96608b0 (patch)
tree7d07f1dc83782b8eb06ff960456a154a1a2d92d0 /server
parent50cb778ee6c55e318331ff80a91e891751ab5c73 (diff)
downloadPeerTube-21b5c2982ff93bc67d5dd4c04db352a3f96608b0.tar.gz
PeerTube-21b5c2982ff93bc67d5dd4c04db352a3f96608b0.tar.zst
PeerTube-21b5c2982ff93bc67d5dd4c04db352a3f96608b0.zip
Fix ownership change with a live video
Diffstat (limited to 'server')
-rw-r--r--server/middlewares/validators/videos/index.ts2
-rw-r--r--server/middlewares/validators/videos/video-ownership-changes.ts119
-rw-r--r--server/middlewares/validators/videos/videos.ts80
-rw-r--r--server/tests/api/videos/video-change-ownership.ts108
4 files changed, 208 insertions, 101 deletions
diff --git a/server/middlewares/validators/videos/index.ts b/server/middlewares/validators/videos/index.ts
index 1eabada0a..369c2c9b6 100644
--- a/server/middlewares/validators/videos/index.ts
+++ b/server/middlewares/validators/videos/index.ts
@@ -3,6 +3,8 @@ export * from './video-captions'
3export * from './video-channels' 3export * from './video-channels'
4export * from './video-comments' 4export * from './video-comments'
5export * from './video-imports' 5export * from './video-imports'
6export * from './video-live'
7export * from './video-ownership-changes'
6export * from './video-watch' 8export * from './video-watch'
7export * from './video-rates' 9export * from './video-rates'
8export * from './video-shares' 10export * from './video-shares'
diff --git a/server/middlewares/validators/videos/video-ownership-changes.ts b/server/middlewares/validators/videos/video-ownership-changes.ts
new file mode 100644
index 000000000..120b0469c
--- /dev/null
+++ b/server/middlewares/validators/videos/video-ownership-changes.ts
@@ -0,0 +1,119 @@
1import * as express from 'express'
2import { param } from 'express-validator'
3import { isIdOrUUIDValid } from '@server/helpers/custom-validators/misc'
4import { checkUserCanTerminateOwnershipChange } from '@server/helpers/custom-validators/video-ownership'
5import { logger } from '@server/helpers/logger'
6import { isAbleToUploadVideo } from '@server/lib/user'
7import { AccountModel } from '@server/models/account/account'
8import { MVideoWithAllFiles } from '@server/types/models'
9import { HttpStatusCode } from '@shared/core-utils'
10import { ServerErrorCode, UserRight, VideoChangeOwnershipAccept, VideoChangeOwnershipStatus, VideoState } from '@shared/models'
11import {
12 areValidationErrors,
13 checkUserCanManageVideo,
14 doesChangeVideoOwnershipExist,
15 doesVideoChannelOfAccountExist,
16 doesVideoExist
17} from '../shared'
18
19const videosChangeOwnershipValidator = [
20 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
21
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking changeOwnership parameters', { parameters: req.params })
24
25 if (areValidationErrors(req, res)) return
26 if (!await doesVideoExist(req.params.videoId, res)) return
27
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
30
31 const nextOwner = await AccountModel.loadLocalByName(req.body.username)
32 if (!nextOwner) {
33 res.fail({ message: 'Changing video ownership to a remote account is not supported yet' })
34 return
35 }
36
37 res.locals.nextOwner = nextOwner
38 return next()
39 }
40]
41
42const videosTerminateChangeOwnershipValidator = [
43 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
44
45 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 logger.debug('Checking changeOwnership parameters', { parameters: req.params })
47
48 if (areValidationErrors(req, res)) return
49 if (!await doesChangeVideoOwnershipExist(req.params.id, res)) return
50
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
53
54 const videoChangeOwnership = res.locals.videoChangeOwnership
55
56 if (videoChangeOwnership.status !== VideoChangeOwnershipStatus.WAITING) {
57 res.fail({
58 status: HttpStatusCode.FORBIDDEN_403,
59 message: 'Ownership already accepted or refused'
60 })
61 return
62 }
63
64 return next()
65 }
66]
67
68const 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
72
73 const videoChangeOwnership = res.locals.videoChangeOwnership
74
75 const video = videoChangeOwnership.Video
76
77 if (!await checkCanAccept(video, res)) return
78
79 return next()
80 }
81]
82
83export {
84 videosChangeOwnershipValidator,
85 videosTerminateChangeOwnershipValidator,
86 videosAcceptChangeOwnershipValidator
87}
88
89// ---------------------------------------------------------------------------
90
91async function checkCanAccept (video: MVideoWithAllFiles, res: express.Response): Promise<boolean> {
92 if (video.isLive) {
93
94 if (video.state !== VideoState.WAITING_FOR_LIVE) {
95 res.fail({
96 status: HttpStatusCode.BAD_REQUEST_400,
97 message: 'You can accept an ownership change of a published live.'
98 })
99
100 return false
101 }
102
103 return true
104 }
105
106 const user = res.locals.oauth.token.User
107
108 if (!await isAbleToUploadVideo(user.id, video.getMaxQualityFile().size)) {
109 res.fail({
110 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
111 message: 'The user video quota is exceeded with this video.',
112 type: ServerErrorCode.QUOTA_REACHED
113 })
114
115 return false
116 }
117
118 return true
119}
diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts
index 2bed5f181..8201e80c3 100644
--- a/server/middlewares/validators/videos/videos.ts
+++ b/server/middlewares/validators/videos/videos.ts
@@ -5,9 +5,8 @@ import { isAbleToUploadVideo } from '@server/lib/user'
5import { getServerActor } from '@server/models/application/application' 5import { getServerActor } from '@server/models/application/application'
6import { ExpressPromiseHandler } from '@server/types/express' 6import { ExpressPromiseHandler } from '@server/types/express'
7import { MUserAccountId, MVideoFullLight } from '@server/types/models' 7import { MUserAccountId, MVideoFullLight } from '@server/types/models'
8import { ServerErrorCode, UserRight, VideoChangeOwnershipStatus, VideoPrivacy } from '../../../../shared' 8import { ServerErrorCode, UserRight, VideoPrivacy } from '../../../../shared'
9import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 9import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
10import { VideoChangeOwnershipAccept } from '../../../../shared/models/videos/change-ownership/video-change-ownership-accept.model'
11import { 10import {
12 exists, 11 exists,
13 isBooleanValid, 12 isBooleanValid,
@@ -22,7 +21,6 @@ import {
22 toValueOrNull 21 toValueOrNull
23} from '../../../helpers/custom-validators/misc' 22} from '../../../helpers/custom-validators/misc'
24import { isBooleanBothQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search' 23import { isBooleanBothQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search'
25import { checkUserCanTerminateOwnershipChange } from '../../../helpers/custom-validators/video-ownership'
26import { 24import {
27 isScheduleVideoUpdatePrivacyValid, 25 isScheduleVideoUpdatePrivacyValid,
28 isVideoCategoryValid, 26 isVideoCategoryValid,
@@ -48,13 +46,11 @@ import { CONFIG } from '../../../initializers/config'
48import { CONSTRAINTS_FIELDS, OVERVIEWS } from '../../../initializers/constants' 46import { CONSTRAINTS_FIELDS, OVERVIEWS } from '../../../initializers/constants'
49import { isLocalVideoAccepted } from '../../../lib/moderation' 47import { isLocalVideoAccepted } from '../../../lib/moderation'
50import { Hooks } from '../../../lib/plugins/hooks' 48import { Hooks } from '../../../lib/plugins/hooks'
51import { AccountModel } from '../../../models/account/account'
52import { VideoModel } from '../../../models/video/video' 49import { VideoModel } from '../../../models/video/video'
53import { authenticatePromiseIfNeeded } from '../../auth' 50import { authenticatePromiseIfNeeded } from '../../auth'
54import { 51import {
55 areValidationErrors, 52 areValidationErrors,
56 checkUserCanManageVideo, 53 checkUserCanManageVideo,
57 doesChangeVideoOwnershipExist,
58 doesVideoChannelOfAccountExist, 54 doesVideoChannelOfAccountExist,
59 doesVideoExist, 55 doesVideoExist,
60 doesVideoFileOfVideoExist 56 doesVideoFileOfVideoExist
@@ -342,76 +338,6 @@ const videosRemoveValidator = [
342 } 338 }
343] 339]
344 340
345const videosChangeOwnershipValidator = [
346 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
347
348 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
349 logger.debug('Checking changeOwnership parameters', { parameters: req.params })
350
351 if (areValidationErrors(req, res)) return
352 if (!await doesVideoExist(req.params.videoId, res)) return
353
354 // Check if the user who did the request is able to change the ownership of the video
355 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.CHANGE_VIDEO_OWNERSHIP, res)) return
356
357 const nextOwner = await AccountModel.loadLocalByName(req.body.username)
358 if (!nextOwner) {
359 res.fail({ message: 'Changing video ownership to a remote account is not supported yet' })
360 return
361 }
362
363 res.locals.nextOwner = nextOwner
364 return next()
365 }
366]
367
368const videosTerminateChangeOwnershipValidator = [
369 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
370
371 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
372 logger.debug('Checking changeOwnership parameters', { parameters: req.params })
373
374 if (areValidationErrors(req, res)) return
375 if (!await doesChangeVideoOwnershipExist(req.params.id, res)) return
376
377 // Check if the user who did the request is able to change the ownership of the video
378 if (!checkUserCanTerminateOwnershipChange(res.locals.oauth.token.User, res.locals.videoChangeOwnership, res)) return
379
380 const videoChangeOwnership = res.locals.videoChangeOwnership
381
382 if (videoChangeOwnership.status !== VideoChangeOwnershipStatus.WAITING) {
383 res.fail({
384 status: HttpStatusCode.FORBIDDEN_403,
385 message: 'Ownership already accepted or refused'
386 })
387 return
388 }
389
390 return next()
391 }
392]
393
394const videosAcceptChangeOwnershipValidator = [
395 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
396 const body = req.body as VideoChangeOwnershipAccept
397 if (!await doesVideoChannelOfAccountExist(body.channelId, res.locals.oauth.token.User, res)) return
398
399 const user = res.locals.oauth.token.User
400 const videoChangeOwnership = res.locals.videoChangeOwnership
401 const isAble = await isAbleToUploadVideo(user.id, videoChangeOwnership.Video.getMaxQualityFile().size)
402 if (isAble === false) {
403 res.fail({
404 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
405 message: 'The user video quota is exceeded with this video.',
406 type: ServerErrorCode.QUOTA_REACHED
407 })
408 return
409 }
410
411 return next()
412 }
413]
414
415const videosOverviewValidator = [ 341const videosOverviewValidator = [
416 query('page') 342 query('page')
417 .optional() 343 .optional()
@@ -578,10 +504,6 @@ export {
578 videosCustomGetValidator, 504 videosCustomGetValidator,
579 videosRemoveValidator, 505 videosRemoveValidator,
580 506
581 videosChangeOwnershipValidator,
582 videosTerminateChangeOwnershipValidator,
583 videosAcceptChangeOwnershipValidator,
584
585 getCommonVideoEditAttributes, 507 getCommonVideoEditAttributes,
586 508
587 commonVideosFiltersValidator, 509 commonVideosFiltersValidator,
diff --git a/server/tests/api/videos/video-change-ownership.ts b/server/tests/api/videos/video-change-ownership.ts
index fad4c8b1f..a3384851b 100644
--- a/server/tests/api/videos/video-change-ownership.ts
+++ b/server/tests/api/videos/video-change-ownership.ts
@@ -1,11 +1,13 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
4import * as chai from 'chai'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
5import { 6import {
6 acceptChangeOwnership, 7 acceptChangeOwnership,
7 changeVideoOwnership, 8 changeVideoOwnership,
8 cleanupTests, 9 cleanupTests,
10 createLive,
9 createUser, 11 createUser,
10 doubleFollow, 12 doubleFollow,
11 flushAndRunMultipleServers, 13 flushAndRunMultipleServers,
@@ -17,13 +19,14 @@ import {
17 refuseChangeOwnership, 19 refuseChangeOwnership,
18 ServerInfo, 20 ServerInfo,
19 setAccessTokensToServers, 21 setAccessTokensToServers,
22 setDefaultVideoChannel,
23 updateCustomSubConfig,
20 uploadVideo, 24 uploadVideo,
21 userLogin 25 userLogin
22} from '../../../../shared/extra-utils' 26} from '../../../../shared/extra-utils'
23import { waitJobs } from '../../../../shared/extra-utils/server/jobs' 27import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
24import { User } from '../../../../shared/models/users' 28import { User } from '../../../../shared/models/users'
25import { VideoDetails } from '../../../../shared/models/videos' 29import { VideoDetails, VideoPrivacy } from '../../../../shared/models/videos'
26import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
27 30
28const expect = chai.expect 31const expect = chai.expect
29 32
@@ -37,15 +40,32 @@ describe('Test video change ownership - nominal', function () {
37 username: 'second', 40 username: 'second',
38 password: 'My other password' 41 password: 'My other password'
39 } 42 }
43
40 let firstUserAccessToken = '' 44 let firstUserAccessToken = ''
45 let firstUserChannelId: number
46
41 let secondUserAccessToken = '' 47 let secondUserAccessToken = ''
48 let secondUserChannelId: number
49
42 let lastRequestChangeOwnershipId = '' 50 let lastRequestChangeOwnershipId = ''
43 51
52 let liveId: number
53
44 before(async function () { 54 before(async function () {
45 this.timeout(50000) 55 this.timeout(50000)
46 56
47 servers = await flushAndRunMultipleServers(2) 57 servers = await flushAndRunMultipleServers(2)
48 await setAccessTokensToServers(servers) 58 await setAccessTokensToServers(servers)
59 await setDefaultVideoChannel(servers)
60
61 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
62 transcoding: {
63 enabled: false
64 },
65 live: {
66 enabled: true
67 }
68 })
49 69
50 const videoQuota = 42000000 70 const videoQuota = 42000000
51 await createUser({ 71 await createUser({
@@ -66,22 +86,35 @@ describe('Test video change ownership - nominal', function () {
66 firstUserAccessToken = await userLogin(servers[0], firstUser) 86 firstUserAccessToken = await userLogin(servers[0], firstUser)
67 secondUserAccessToken = await userLogin(servers[0], secondUser) 87 secondUserAccessToken = await userLogin(servers[0], secondUser)
68 88
69 const videoAttributes = { 89 {
70 name: 'my super name', 90 const res = await getMyUserInformation(servers[0].url, firstUserAccessToken)
71 description: 'my super description' 91 const firstUserInformation: User = res.body
92 firstUserChannelId = firstUserInformation.videoChannels[0].id
72 } 93 }
73 await uploadVideo(servers[0].url, firstUserAccessToken, videoAttributes)
74 94
75 await waitJobs(servers) 95 {
96 const res = await getMyUserInformation(servers[0].url, secondUserAccessToken)
97 const secondUserInformation: User = res.body
98 secondUserChannelId = secondUserInformation.videoChannels[0].id
99 }
76 100
77 const res = await getVideosList(servers[0].url) 101 {
78 const videos = res.body.data 102 const videoAttributes = {
103 name: 'my super name',
104 description: 'my super description'
105 }
106 const res = await uploadVideo(servers[0].url, firstUserAccessToken, videoAttributes)
79 107
80 expect(videos.length).to.equal(1) 108 const resVideo = await getVideo(servers[0].url, res.body.video.id)
109 servers[0].video = resVideo.body
110 }
81 111
82 const video = videos.find(video => video.name === 'my super name') 112 {
83 expect(video.channel.name).to.equal('first_channel') 113 const attributes = { name: 'live', channelId: firstUserChannelId, privacy: VideoPrivacy.PUBLIC }
84 servers[0].video = video 114 const res = await createLive(servers[0].url, firstUserAccessToken, attributes)
115
116 liveId = res.body.video.id
117 }
85 118
86 await doubleFollow(servers[0], servers[1]) 119 await doubleFollow(servers[0], servers[1])
87 }) 120 })
@@ -175,19 +208,19 @@ describe('Test video change ownership - nominal', function () {
175 it('Should not be possible to accept the change of ownership from first user', async function () { 208 it('Should not be possible to accept the change of ownership from first user', async function () {
176 this.timeout(10000) 209 this.timeout(10000)
177 210
178 const secondUserInformationResponse = await getMyUserInformation(servers[0].url, secondUserAccessToken) 211 await acceptChangeOwnership(
179 const secondUserInformation: User = secondUserInformationResponse.body 212 servers[0].url,
180 const channelId = secondUserInformation.videoChannels[0].id 213 firstUserAccessToken,
181 await acceptChangeOwnership(servers[0].url, firstUserAccessToken, lastRequestChangeOwnershipId, channelId, HttpStatusCode.FORBIDDEN_403) 214 lastRequestChangeOwnershipId,
215 secondUserChannelId,
216 HttpStatusCode.FORBIDDEN_403
217 )
182 }) 218 })
183 219
184 it('Should be possible to accept the change of ownership from second user', async function () { 220 it('Should be possible to accept the change of ownership from second user', async function () {
185 this.timeout(10000) 221 this.timeout(10000)
186 222
187 const secondUserInformationResponse = await getMyUserInformation(servers[0].url, secondUserAccessToken) 223 await acceptChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId, secondUserChannelId)
188 const secondUserInformation: User = secondUserInformationResponse.body
189 const channelId = secondUserInformation.videoChannels[0].id
190 await acceptChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId, channelId)
191 224
192 await waitJobs(servers) 225 await waitJobs(servers)
193 }) 226 })
@@ -204,6 +237,37 @@ describe('Test video change ownership - nominal', function () {
204 } 237 }
205 }) 238 })
206 239
240 it('Should send a request to change ownership of a live', async function () {
241 this.timeout(15000)
242
243 await changeVideoOwnership(servers[0].url, firstUserAccessToken, liveId, secondUser.username)
244
245 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
246
247 expect(resSecondUser.body.total).to.equal(3)
248 expect(resSecondUser.body.data.length).to.equal(3)
249
250 lastRequestChangeOwnershipId = resSecondUser.body.data[0].id
251 })
252
253 it('Should accept a live ownership change', async function () {
254 this.timeout(20000)
255
256 await acceptChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId, secondUserChannelId)
257
258 await waitJobs(servers)
259
260 for (const server of servers) {
261 const res = await getVideo(server.url, servers[0].video.uuid)
262
263 const video: VideoDetails = res.body
264
265 expect(video.name).to.equal('my super name')
266 expect(video.channel.displayName).to.equal('Main second channel')
267 expect(video.channel.name).to.equal('second_channel')
268 }
269 })
270
207 after(async function () { 271 after(async function () {
208 await cleanupTests(servers) 272 await cleanupTests(servers)
209 }) 273 })