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