]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/ownership.ts
Create a dedicated table to track video thumbnails
[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'
5cf84858 15import { VideoChangeOwnershipStatus, VideoPrivacy, VideoState } from '../../../../shared/models/videos'
74d63469
GR
16import { VideoChannelModel } from '../../../models/video/video-channel'
17import { getFormattedObjects } from '../../../helpers/utils'
5cf84858
C
18import { changeVideoChannelShare } from '../../../lib/activitypub'
19import { sendUpdateVideo } from '../../../lib/activitypub/send'
74d63469
GR
20
21const ownershipVideoRouter = express.Router()
22
23ownershipVideoRouter.post('/:videoId/give-ownership',
24 authenticate,
25 asyncMiddleware(videosChangeOwnershipValidator),
26 asyncRetryTransactionMiddleware(giveVideoOwnership)
27)
28
29ownershipVideoRouter.get('/ownership',
30 authenticate,
31 paginationValidator,
32 setDefaultPagination,
33 asyncRetryTransactionMiddleware(listVideoOwnership)
34)
35
36ownershipVideoRouter.post('/ownership/:id/accept',
37 authenticate,
38 asyncMiddleware(videosTerminateChangeOwnershipValidator),
39 asyncMiddleware(videosAcceptChangeOwnershipValidator),
40 asyncRetryTransactionMiddleware(acceptOwnership)
41)
42
43ownershipVideoRouter.post('/ownership/:id/refuse',
44 authenticate,
45 asyncMiddleware(videosTerminateChangeOwnershipValidator),
46 asyncRetryTransactionMiddleware(refuseOwnership)
47)
48
49// ---------------------------------------------------------------------------
50
51export {
52 ownershipVideoRouter
53}
54
55// ---------------------------------------------------------------------------
56
57async function giveVideoOwnership (req: express.Request, res: express.Response) {
dae86118
C
58 const videoInstance = res.locals.video
59 const initiatorAccountId = res.locals.oauth.token.User.Account.id
60 const nextOwner = res.locals.nextOwner
74d63469 61
5cf84858
C
62 await sequelizeTypescript.transaction(t => {
63 return VideoChangeOwnershipModel.findOrCreate({
74d63469 64 where: {
91411dba 65 initiatorAccountId,
74d63469
GR
66 nextOwnerAccountId: nextOwner.id,
67 videoId: videoInstance.id,
68 status: VideoChangeOwnershipStatus.WAITING
69 },
70 defaults: {
91411dba 71 initiatorAccountId,
74d63469
GR
72 nextOwnerAccountId: nextOwner.id,
73 videoId: videoInstance.id,
74 status: VideoChangeOwnershipStatus.WAITING
5cf84858
C
75 },
76 transaction: t
74d63469 77 })
74d63469 78 })
5cf84858
C
79
80 logger.info('Ownership change for video %s created.', videoInstance.name)
81 return res.type('json').status(204).end()
74d63469
GR
82}
83
84async function listVideoOwnership (req: express.Request, res: express.Response) {
dae86118 85 const currentAccountId = res.locals.oauth.token.User.Account.id
91411dba 86
74d63469 87 const resultList = await VideoChangeOwnershipModel.listForApi(
91411dba 88 currentAccountId,
74d63469
GR
89 req.query.start || 0,
90 req.query.count || 10,
91 req.query.sort || 'createdAt'
92 )
93
94 return res.json(getFormattedObjects(resultList.data, resultList.total))
95}
96
97async function acceptOwnership (req: express.Request, res: express.Response) {
98 return sequelizeTypescript.transaction(async t => {
dae86118 99 const videoChangeOwnership = res.locals.videoChangeOwnership
74d63469 100 const targetVideo = videoChangeOwnership.Video
dae86118 101 const channel = res.locals.videoChannel
74d63469 102
5cf84858
C
103 const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
104
74d63469 105 targetVideo.set('channelId', channel.id)
5cf84858
C
106 const targetVideoUpdated = await targetVideo.save({ transaction: t })
107 targetVideoUpdated.VideoChannel = channel
108
109 if (targetVideoUpdated.privacy !== VideoPrivacy.PRIVATE && targetVideoUpdated.state === VideoState.PUBLISHED) {
110 await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
111 await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
112 }
74d63469 113
74d63469 114 videoChangeOwnership.set('status', VideoChangeOwnershipStatus.ACCEPTED)
5cf84858 115 await videoChangeOwnership.save({ transaction: t })
74d63469
GR
116
117 return res.sendStatus(204)
118 })
119}
120
121async function refuseOwnership (req: express.Request, res: express.Response) {
122 return sequelizeTypescript.transaction(async t => {
dae86118 123 const videoChangeOwnership = res.locals.videoChangeOwnership
5cf84858 124
74d63469 125 videoChangeOwnership.set('status', VideoChangeOwnershipStatus.REFUSED)
5cf84858
C
126 await videoChangeOwnership.save({ transaction: t })
127
74d63469
GR
128 return res.sendStatus(204)
129 })
130}