]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/ownership.ts
Cleaner warning of IP address leaking on embedded videos (#2034)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / ownership.ts
CommitLineData
74d63469
GR
1import * as express from 'express'
2import { logger } from '../../../helpers/logger'
3import { sequelizeTypescript } from '../../../initializers'
4import {
5 asyncMiddleware,
6 asyncRetryTransactionMiddleware,
7 authenticate,
8 paginationValidator,
9 setDefaultPagination,
10 videosAcceptChangeOwnershipValidator,
11 videosChangeOwnershipValidator,
12 videosTerminateChangeOwnershipValidator
13} from '../../../middlewares'
74d63469 14import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership'
5cf84858 15import { VideoChangeOwnershipStatus, VideoPrivacy, VideoState } from '../../../../shared/models/videos'
74d63469
GR
16import { VideoChannelModel } from '../../../models/video/video-channel'
17import { getFormattedObjects } from '../../../helpers/utils'
5cf84858
C
18import { changeVideoChannelShare } from '../../../lib/activitypub'
19import { sendUpdateVideo } from '../../../lib/activitypub/send'
b876eaf1 20import { VideoModel } from '../../../models/video/video'
74d63469
GR
21
22const ownershipVideoRouter = express.Router()
23
24ownershipVideoRouter.post('/:videoId/give-ownership',
25 authenticate,
26 asyncMiddleware(videosChangeOwnershipValidator),
27 asyncRetryTransactionMiddleware(giveVideoOwnership)
28)
29
30ownershipVideoRouter.get('/ownership',
31 authenticate,
32 paginationValidator,
33 setDefaultPagination,
34 asyncRetryTransactionMiddleware(listVideoOwnership)
35)
36
37ownershipVideoRouter.post('/ownership/:id/accept',
38 authenticate,
39 asyncMiddleware(videosTerminateChangeOwnershipValidator),
40 asyncMiddleware(videosAcceptChangeOwnershipValidator),
41 asyncRetryTransactionMiddleware(acceptOwnership)
42)
43
44ownershipVideoRouter.post('/ownership/:id/refuse',
45 authenticate,
46 asyncMiddleware(videosTerminateChangeOwnershipValidator),
47 asyncRetryTransactionMiddleware(refuseOwnership)
48)
49
50// ---------------------------------------------------------------------------
51
52export {
53 ownershipVideoRouter
54}
55
56// ---------------------------------------------------------------------------
57
58async function giveVideoOwnership (req: express.Request, res: express.Response) {
dae86118
C
59 const videoInstance = res.locals.video
60 const initiatorAccountId = res.locals.oauth.token.User.Account.id
61 const nextOwner = res.locals.nextOwner
74d63469 62
5cf84858
C
63 await sequelizeTypescript.transaction(t => {
64 return VideoChangeOwnershipModel.findOrCreate({
74d63469 65 where: {
91411dba 66 initiatorAccountId,
74d63469
GR
67 nextOwnerAccountId: nextOwner.id,
68 videoId: videoInstance.id,
69 status: VideoChangeOwnershipStatus.WAITING
70 },
71 defaults: {
91411dba 72 initiatorAccountId,
74d63469
GR
73 nextOwnerAccountId: nextOwner.id,
74 videoId: videoInstance.id,
75 status: VideoChangeOwnershipStatus.WAITING
5cf84858
C
76 },
77 transaction: t
74d63469 78 })
74d63469 79 })
5cf84858
C
80
81 logger.info('Ownership change for video %s created.', videoInstance.name)
82 return res.type('json').status(204).end()
74d63469
GR
83}
84
85async function listVideoOwnership (req: express.Request, res: express.Response) {
dae86118 86 const currentAccountId = res.locals.oauth.token.User.Account.id
91411dba 87
74d63469 88 const resultList = await VideoChangeOwnershipModel.listForApi(
91411dba 89 currentAccountId,
74d63469
GR
90 req.query.start || 0,
91 req.query.count || 10,
92 req.query.sort || 'createdAt'
93 )
94
95 return res.json(getFormattedObjects(resultList.data, resultList.total))
96}
97
98async function acceptOwnership (req: express.Request, res: express.Response) {
99 return sequelizeTypescript.transaction(async t => {
dae86118 100 const videoChangeOwnership = res.locals.videoChangeOwnership
dae86118 101 const channel = res.locals.videoChannel
74d63469 102
b876eaf1
C
103 // We need more attributes for federation
104 const targetVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoChangeOwnership.Video.id)
105
5cf84858
C
106 const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
107
b876eaf1
C
108 targetVideo.channelId = channel.id
109
5cf84858
C
110 const targetVideoUpdated = await targetVideo.save({ transaction: t })
111 targetVideoUpdated.VideoChannel = channel
112
113 if (targetVideoUpdated.privacy !== VideoPrivacy.PRIVATE && targetVideoUpdated.state === VideoState.PUBLISHED) {
114 await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
115 await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
116 }
74d63469 117
b876eaf1 118 videoChangeOwnership.status = VideoChangeOwnershipStatus.ACCEPTED
5cf84858 119 await videoChangeOwnership.save({ transaction: t })
74d63469
GR
120
121 return res.sendStatus(204)
122 })
123}
124
125async function refuseOwnership (req: express.Request, res: express.Response) {
126 return sequelizeTypescript.transaction(async t => {
dae86118 127 const videoChangeOwnership = res.locals.videoChangeOwnership
5cf84858 128
b876eaf1 129 videoChangeOwnership.status = VideoChangeOwnershipStatus.REFUSED
5cf84858
C
130 await videoChangeOwnership.save({ transaction: t })
131
74d63469
GR
132 return res.sendStatus(204)
133 })
134}