1 import * as express from 'express'
2 import { logger } from '../../../helpers/logger'
3 import { sequelizeTypescript } from '../../../initializers'
6 asyncRetryTransactionMiddleware,
10 videosAcceptChangeOwnershipValidator,
11 videosChangeOwnershipValidator,
12 videosTerminateChangeOwnershipValidator
13 } from '../../../middlewares'
14 import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership'
15 import { VideoChangeOwnershipStatus, VideoPrivacy, VideoState } from '../../../../shared/models/videos'
16 import { VideoChannelModel } from '../../../models/video/video-channel'
17 import { getFormattedObjects } from '../../../helpers/utils'
18 import { changeVideoChannelShare } from '../../../lib/activitypub'
19 import { sendUpdateVideo } from '../../../lib/activitypub/send'
20 import { UserModel } from '../../../models/account/user'
22 const ownershipVideoRouter = express.Router()
24 ownershipVideoRouter.post('/:videoId/give-ownership',
26 asyncMiddleware(videosChangeOwnershipValidator),
27 asyncRetryTransactionMiddleware(giveVideoOwnership)
30 ownershipVideoRouter.get('/ownership',
34 asyncRetryTransactionMiddleware(listVideoOwnership)
37 ownershipVideoRouter.post('/ownership/:id/accept',
39 asyncMiddleware(videosTerminateChangeOwnershipValidator),
40 asyncMiddleware(videosAcceptChangeOwnershipValidator),
41 asyncRetryTransactionMiddleware(acceptOwnership)
44 ownershipVideoRouter.post('/ownership/:id/refuse',
46 asyncMiddleware(videosTerminateChangeOwnershipValidator),
47 asyncRetryTransactionMiddleware(refuseOwnership)
50 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
58 async function giveVideoOwnership (req: express.Request, res: express.Response) {
59 const videoInstance = res.locals.video
60 const initiatorAccountId = res.locals.oauth.token.User.Account.id
61 const nextOwner = res.locals.nextOwner
63 await sequelizeTypescript.transaction(t => {
64 return VideoChangeOwnershipModel.findOrCreate({
67 nextOwnerAccountId: nextOwner.id,
68 videoId: videoInstance.id,
69 status: VideoChangeOwnershipStatus.WAITING
73 nextOwnerAccountId: nextOwner.id,
74 videoId: videoInstance.id,
75 status: VideoChangeOwnershipStatus.WAITING
81 logger.info('Ownership change for video %s created.', videoInstance.name)
82 return res.type('json').status(204).end()
85 async function listVideoOwnership (req: express.Request, res: express.Response) {
86 const currentAccountId = res.locals.oauth.token.User.Account.id
88 const resultList = await VideoChangeOwnershipModel.listForApi(
91 req.query.count || 10,
92 req.query.sort || 'createdAt'
95 return res.json(getFormattedObjects(resultList.data, resultList.total))
98 async function acceptOwnership (req: express.Request, res: express.Response) {
99 return sequelizeTypescript.transaction(async t => {
100 const videoChangeOwnership = res.locals.videoChangeOwnership
101 const targetVideo = videoChangeOwnership.Video
102 const channel = res.locals.videoChannel
104 const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
106 targetVideo.set('channelId', channel.id)
107 const targetVideoUpdated = await targetVideo.save({ transaction: t })
108 targetVideoUpdated.VideoChannel = channel
110 if (targetVideoUpdated.privacy !== VideoPrivacy.PRIVATE && targetVideoUpdated.state === VideoState.PUBLISHED) {
111 await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
112 await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
115 videoChangeOwnership.set('status', VideoChangeOwnershipStatus.ACCEPTED)
116 await videoChangeOwnership.save({ transaction: t })
118 return res.sendStatus(204)
122 async function refuseOwnership (req: express.Request, res: express.Response) {
123 return sequelizeTypescript.transaction(async t => {
124 const videoChangeOwnership = res.locals.videoChangeOwnership
126 videoChangeOwnership.set('status', VideoChangeOwnershipStatus.REFUSED)
127 await videoChangeOwnership.save({ transaction: t })
129 return res.sendStatus(204)