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