1 import * as express from 'express'
2 import { logger } from '../../../helpers/logger'
3 import { sequelizeTypescript } from '../../../initializers/database'
6 asyncRetryTransactionMiddleware,
10 videosAcceptChangeOwnershipValidator,
11 videosChangeOwnershipValidator,
12 videosTerminateChangeOwnershipValidator
13 } from '../../../middlewares'
14 import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership'
15 import { VideoChangeOwnershipStatus, 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/share'
19 import { sendUpdateVideo } from '../../../lib/activitypub/send'
20 import { VideoModel } from '../../../models/video/video'
21 import { MVideoFullLight } from '@server/types/models'
23 const ownershipVideoRouter = express.Router()
25 ownershipVideoRouter.post('/:videoId/give-ownership',
27 asyncMiddleware(videosChangeOwnershipValidator),
28 asyncRetryTransactionMiddleware(giveVideoOwnership)
31 ownershipVideoRouter.get('/ownership',
35 asyncRetryTransactionMiddleware(listVideoOwnership)
38 ownershipVideoRouter.post('/ownership/:id/accept',
40 asyncMiddleware(videosTerminateChangeOwnershipValidator),
41 asyncMiddleware(videosAcceptChangeOwnershipValidator),
42 asyncRetryTransactionMiddleware(acceptOwnership)
45 ownershipVideoRouter.post('/ownership/:id/refuse',
47 asyncMiddleware(videosTerminateChangeOwnershipValidator),
48 asyncRetryTransactionMiddleware(refuseOwnership)
51 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 async function giveVideoOwnership (req: express.Request, res: express.Response) {
60 const videoInstance = res.locals.videoAll
61 const initiatorAccountId = res.locals.oauth.token.User.Account.id
62 const nextOwner = res.locals.nextOwner
64 await sequelizeTypescript.transaction(t => {
65 return VideoChangeOwnershipModel.findOrCreate({
68 nextOwnerAccountId: nextOwner.id,
69 videoId: videoInstance.id,
70 status: VideoChangeOwnershipStatus.WAITING
74 nextOwnerAccountId: nextOwner.id,
75 videoId: videoInstance.id,
76 status: VideoChangeOwnershipStatus.WAITING
82 logger.info('Ownership change for video %s created.', videoInstance.name)
83 return res.type('json').status(204).end()
86 async function listVideoOwnership (req: express.Request, res: express.Response) {
87 const currentAccountId = res.locals.oauth.token.User.Account.id
89 const resultList = await VideoChangeOwnershipModel.listForApi(
92 req.query.count || 10,
93 req.query.sort || 'createdAt'
96 return res.json(getFormattedObjects(resultList.data, resultList.total))
99 async function acceptOwnership (req: express.Request, res: express.Response) {
100 return sequelizeTypescript.transaction(async t => {
101 const videoChangeOwnership = res.locals.videoChangeOwnership
102 const channel = res.locals.videoChannel
104 // We need more attributes for federation
105 const targetVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoChangeOwnership.Video.id)
107 const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
109 targetVideo.channelId = channel.id
111 const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFullLight
112 targetVideoUpdated.VideoChannel = channel
114 if (targetVideoUpdated.hasPrivacyForFederation() && targetVideoUpdated.state === VideoState.PUBLISHED) {
115 await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
116 await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
119 videoChangeOwnership.status = VideoChangeOwnershipStatus.ACCEPTED
120 await videoChangeOwnership.save({ transaction: t })
122 return res.sendStatus(204)
126 async function refuseOwnership (req: express.Request, res: express.Response) {
127 return sequelizeTypescript.transaction(async t => {
128 const videoChangeOwnership = res.locals.videoChangeOwnership
130 videoChangeOwnership.status = VideoChangeOwnershipStatus.REFUSED
131 await videoChangeOwnership.save({ transaction: t })
133 return res.sendStatus(204)