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'
22 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
24 const ownershipVideoRouter = express.Router()
26 ownershipVideoRouter.post('/:videoId/give-ownership',
28 asyncMiddleware(videosChangeOwnershipValidator),
29 asyncRetryTransactionMiddleware(giveVideoOwnership)
32 ownershipVideoRouter.get('/ownership',
36 asyncRetryTransactionMiddleware(listVideoOwnership)
39 ownershipVideoRouter.post('/ownership/:id/accept',
41 asyncMiddleware(videosTerminateChangeOwnershipValidator),
42 asyncMiddleware(videosAcceptChangeOwnershipValidator),
43 asyncRetryTransactionMiddleware(acceptOwnership)
46 ownershipVideoRouter.post('/ownership/:id/refuse',
48 asyncMiddleware(videosTerminateChangeOwnershipValidator),
49 asyncRetryTransactionMiddleware(refuseOwnership)
52 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 async function giveVideoOwnership (req: express.Request, res: express.Response) {
61 const videoInstance = res.locals.videoAll
62 const initiatorAccountId = res.locals.oauth.token.User.Account.id
63 const nextOwner = res.locals.nextOwner
65 await sequelizeTypescript.transaction(t => {
66 return VideoChangeOwnershipModel.findOrCreate({
69 nextOwnerAccountId: nextOwner.id,
70 videoId: videoInstance.id,
71 status: VideoChangeOwnershipStatus.WAITING
75 nextOwnerAccountId: nextOwner.id,
76 videoId: videoInstance.id,
77 status: VideoChangeOwnershipStatus.WAITING
83 logger.info('Ownership change for video %s created.', videoInstance.name)
84 return res.type('json')
85 .status(HttpStatusCode.NO_CONTENT_204)
89 async function listVideoOwnership (req: express.Request, res: express.Response) {
90 const currentAccountId = res.locals.oauth.token.User.Account.id
92 const resultList = await VideoChangeOwnershipModel.listForApi(
95 req.query.count || 10,
96 req.query.sort || 'createdAt'
99 return res.json(getFormattedObjects(resultList.data, resultList.total))
102 async function acceptOwnership (req: express.Request, res: express.Response) {
103 return sequelizeTypescript.transaction(async t => {
104 const videoChangeOwnership = res.locals.videoChangeOwnership
105 const channel = res.locals.videoChannel
107 // We need more attributes for federation
108 const targetVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoChangeOwnership.Video.id)
110 const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
112 targetVideo.channelId = channel.id
114 const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFullLight
115 targetVideoUpdated.VideoChannel = channel
117 if (targetVideoUpdated.hasPrivacyForFederation() && targetVideoUpdated.state === VideoState.PUBLISHED) {
118 await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
119 await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
122 videoChangeOwnership.status = VideoChangeOwnershipStatus.ACCEPTED
123 await videoChangeOwnership.save({ transaction: t })
125 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
129 async function refuseOwnership (req: express.Request, res: express.Response) {
130 return sequelizeTypescript.transaction(async t => {
131 const videoChangeOwnership = res.locals.videoChangeOwnership
133 videoChangeOwnership.status = VideoChangeOwnershipStatus.REFUSED
134 await videoChangeOwnership.save({ transaction: t })
136 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)