]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/ownership.ts
Merge branch 'develop' into cli-wrapper
[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 import { UserModel } from '../../../models/account/user'
23
24 const ownershipVideoRouter = express.Router()
25
26 ownershipVideoRouter.post('/:videoId/give-ownership',
27 authenticate,
28 asyncMiddleware(videosChangeOwnershipValidator),
29 asyncRetryTransactionMiddleware(giveVideoOwnership)
30 )
31
32 ownershipVideoRouter.get('/ownership',
33 authenticate,
34 paginationValidator,
35 setDefaultPagination,
36 asyncRetryTransactionMiddleware(listVideoOwnership)
37 )
38
39 ownershipVideoRouter.post('/ownership/:id/accept',
40 authenticate,
41 asyncMiddleware(videosTerminateChangeOwnershipValidator),
42 asyncMiddleware(videosAcceptChangeOwnershipValidator),
43 asyncRetryTransactionMiddleware(acceptOwnership)
44 )
45
46 ownershipVideoRouter.post('/ownership/:id/refuse',
47 authenticate,
48 asyncMiddleware(videosTerminateChangeOwnershipValidator),
49 asyncRetryTransactionMiddleware(refuseOwnership)
50 )
51
52 // ---------------------------------------------------------------------------
53
54 export {
55 ownershipVideoRouter
56 }
57
58 // ---------------------------------------------------------------------------
59
60 async function giveVideoOwnership (req: express.Request, res: express.Response) {
61 const videoInstance = res.locals.video as VideoModel
62 const initiatorAccountId = (res.locals.oauth.token.User as UserModel).Account.id
63 const nextOwner = res.locals.nextOwner as AccountModel
64
65 await sequelizeTypescript.transaction(t => {
66 return VideoChangeOwnershipModel.findOrCreate({
67 where: {
68 initiatorAccountId,
69 nextOwnerAccountId: nextOwner.id,
70 videoId: videoInstance.id,
71 status: VideoChangeOwnershipStatus.WAITING
72 },
73 defaults: {
74 initiatorAccountId,
75 nextOwnerAccountId: nextOwner.id,
76 videoId: videoInstance.id,
77 status: VideoChangeOwnershipStatus.WAITING
78 },
79 transaction: t
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 currentAccountId = (res.locals.oauth.token.User as UserModel).Account.id
89
90 const resultList = await VideoChangeOwnershipModel.listForApi(
91 currentAccountId,
92 req.query.start || 0,
93 req.query.count || 10,
94 req.query.sort || 'createdAt'
95 )
96
97 return res.json(getFormattedObjects(resultList.data, resultList.total))
98 }
99
100 async function acceptOwnership (req: express.Request, res: express.Response) {
101 return sequelizeTypescript.transaction(async t => {
102 const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel
103 const targetVideo = videoChangeOwnership.Video
104 const channel = res.locals.videoChannel as VideoChannelModel
105
106 const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
107
108 targetVideo.set('channelId', channel.id)
109 const targetVideoUpdated = await targetVideo.save({ transaction: t })
110 targetVideoUpdated.VideoChannel = channel
111
112 if (targetVideoUpdated.privacy !== VideoPrivacy.PRIVATE && targetVideoUpdated.state === VideoState.PUBLISHED) {
113 await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
114 await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
115 }
116
117 videoChangeOwnership.set('status', VideoChangeOwnershipStatus.ACCEPTED)
118 await videoChangeOwnership.save({ transaction: t })
119
120 return res.sendStatus(204)
121 })
122 }
123
124 async function refuseOwnership (req: express.Request, res: express.Response) {
125 return sequelizeTypescript.transaction(async t => {
126 const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel
127
128 videoChangeOwnership.set('status', VideoChangeOwnershipStatus.REFUSED)
129 await videoChangeOwnership.save({ transaction: t })
130
131 return res.sendStatus(204)
132 })
133 }