]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/ownership.ts
Avoir some circular dependencies
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / ownership.ts
CommitLineData
74d63469
GR
1import * as express from 'express'
2import { logger } from '../../../helpers/logger'
3import { sequelizeTypescript } from '../../../initializers'
4import {
5 asyncMiddleware,
6 asyncRetryTransactionMiddleware,
7 authenticate,
8 paginationValidator,
9 setDefaultPagination,
10 videosAcceptChangeOwnershipValidator,
11 videosChangeOwnershipValidator,
12 videosTerminateChangeOwnershipValidator
13} from '../../../middlewares'
74d63469 14import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership'
22a73cb8 15import { VideoChangeOwnershipStatus, VideoState } from '../../../../shared/models/videos'
74d63469
GR
16import { VideoChannelModel } from '../../../models/video/video-channel'
17import { getFormattedObjects } from '../../../helpers/utils'
8dc8a34e 18import { changeVideoChannelShare } from '../../../lib/activitypub/share'
5cf84858 19import { sendUpdateVideo } from '../../../lib/activitypub/send'
b876eaf1 20import { VideoModel } from '../../../models/video/video'
453e83ea 21import { MVideoFullLight } from '@server/typings/models'
74d63469
GR
22
23const ownershipVideoRouter = express.Router()
24
25ownershipVideoRouter.post('/:videoId/give-ownership',
26 authenticate,
27 asyncMiddleware(videosChangeOwnershipValidator),
28 asyncRetryTransactionMiddleware(giveVideoOwnership)
29)
30
31ownershipVideoRouter.get('/ownership',
32 authenticate,
33 paginationValidator,
34 setDefaultPagination,
35 asyncRetryTransactionMiddleware(listVideoOwnership)
36)
37
38ownershipVideoRouter.post('/ownership/:id/accept',
39 authenticate,
40 asyncMiddleware(videosTerminateChangeOwnershipValidator),
41 asyncMiddleware(videosAcceptChangeOwnershipValidator),
42 asyncRetryTransactionMiddleware(acceptOwnership)
43)
44
45ownershipVideoRouter.post('/ownership/:id/refuse',
46 authenticate,
47 asyncMiddleware(videosTerminateChangeOwnershipValidator),
48 asyncRetryTransactionMiddleware(refuseOwnership)
49)
50
51// ---------------------------------------------------------------------------
52
53export {
54 ownershipVideoRouter
55}
56
57// ---------------------------------------------------------------------------
58
59async function giveVideoOwnership (req: express.Request, res: express.Response) {
453e83ea 60 const videoInstance = res.locals.videoAll
dae86118
C
61 const initiatorAccountId = res.locals.oauth.token.User.Account.id
62 const nextOwner = res.locals.nextOwner
74d63469 63
5cf84858
C
64 await sequelizeTypescript.transaction(t => {
65 return VideoChangeOwnershipModel.findOrCreate({
74d63469 66 where: {
91411dba 67 initiatorAccountId,
74d63469
GR
68 nextOwnerAccountId: nextOwner.id,
69 videoId: videoInstance.id,
70 status: VideoChangeOwnershipStatus.WAITING
71 },
72 defaults: {
91411dba 73 initiatorAccountId,
74d63469
GR
74 nextOwnerAccountId: nextOwner.id,
75 videoId: videoInstance.id,
76 status: VideoChangeOwnershipStatus.WAITING
5cf84858
C
77 },
78 transaction: t
74d63469 79 })
74d63469 80 })
5cf84858
C
81
82 logger.info('Ownership change for video %s created.', videoInstance.name)
83 return res.type('json').status(204).end()
74d63469
GR
84}
85
86async function listVideoOwnership (req: express.Request, res: express.Response) {
dae86118 87 const currentAccountId = res.locals.oauth.token.User.Account.id
91411dba 88
74d63469 89 const resultList = await VideoChangeOwnershipModel.listForApi(
91411dba 90 currentAccountId,
74d63469
GR
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
99async function acceptOwnership (req: express.Request, res: express.Response) {
100 return sequelizeTypescript.transaction(async t => {
dae86118 101 const videoChangeOwnership = res.locals.videoChangeOwnership
dae86118 102 const channel = res.locals.videoChannel
74d63469 103
b876eaf1
C
104 // We need more attributes for federation
105 const targetVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoChangeOwnership.Video.id)
106
5cf84858
C
107 const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
108
b876eaf1
C
109 targetVideo.channelId = channel.id
110
453e83ea 111 const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFullLight
5cf84858
C
112 targetVideoUpdated.VideoChannel = channel
113
22a73cb8 114 if (targetVideoUpdated.hasPrivacyForFederation() && targetVideoUpdated.state === VideoState.PUBLISHED) {
5cf84858
C
115 await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
116 await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
117 }
74d63469 118
b876eaf1 119 videoChangeOwnership.status = VideoChangeOwnershipStatus.ACCEPTED
5cf84858 120 await videoChangeOwnership.save({ transaction: t })
74d63469
GR
121
122 return res.sendStatus(204)
123 })
124}
125
126async function refuseOwnership (req: express.Request, res: express.Response) {
127 return sequelizeTypescript.transaction(async t => {
dae86118 128 const videoChangeOwnership = res.locals.videoChangeOwnership
5cf84858 129
b876eaf1 130 videoChangeOwnership.status = VideoChangeOwnershipStatus.REFUSED
5cf84858
C
131 await videoChangeOwnership.save({ transaction: t })
132
74d63469
GR
133 return res.sendStatus(204)
134 })
135}