Split types and typings
[oweals/peertube.git] / server / controllers / api / videos / ownership.ts
1 import * as express from 'express'
2 import { logger } from '../../../helpers/logger'
3 import { sequelizeTypescript } from '../../../initializers/database'
4 import {
5   asyncMiddleware,
6   asyncRetryTransactionMiddleware,
7   authenticate,
8   paginationValidator,
9   setDefaultPagination,
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
23 const ownershipVideoRouter = express.Router()
24
25 ownershipVideoRouter.post('/:videoId/give-ownership',
26   authenticate,
27   asyncMiddleware(videosChangeOwnershipValidator),
28   asyncRetryTransactionMiddleware(giveVideoOwnership)
29 )
30
31 ownershipVideoRouter.get('/ownership',
32   authenticate,
33   paginationValidator,
34   setDefaultPagination,
35   asyncRetryTransactionMiddleware(listVideoOwnership)
36 )
37
38 ownershipVideoRouter.post('/ownership/:id/accept',
39   authenticate,
40   asyncMiddleware(videosTerminateChangeOwnershipValidator),
41   asyncMiddleware(videosAcceptChangeOwnershipValidator),
42   asyncRetryTransactionMiddleware(acceptOwnership)
43 )
44
45 ownershipVideoRouter.post('/ownership/:id/refuse',
46   authenticate,
47   asyncMiddleware(videosTerminateChangeOwnershipValidator),
48   asyncRetryTransactionMiddleware(refuseOwnership)
49 )
50
51 // ---------------------------------------------------------------------------
52
53 export {
54   ownershipVideoRouter
55 }
56
57 // ---------------------------------------------------------------------------
58
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
63
64   await sequelizeTypescript.transaction(t => {
65     return VideoChangeOwnershipModel.findOrCreate({
66       where: {
67         initiatorAccountId,
68         nextOwnerAccountId: nextOwner.id,
69         videoId: videoInstance.id,
70         status: VideoChangeOwnershipStatus.WAITING
71       },
72       defaults: {
73         initiatorAccountId,
74         nextOwnerAccountId: nextOwner.id,
75         videoId: videoInstance.id,
76         status: VideoChangeOwnershipStatus.WAITING
77       },
78       transaction: t
79     })
80   })
81
82   logger.info('Ownership change for video %s created.', videoInstance.name)
83   return res.type('json').status(204).end()
84 }
85
86 async function listVideoOwnership (req: express.Request, res: express.Response) {
87   const currentAccountId = res.locals.oauth.token.User.Account.id
88
89   const resultList = await VideoChangeOwnershipModel.listForApi(
90     currentAccountId,
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
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
103
104     // We need more attributes for federation
105     const targetVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoChangeOwnership.Video.id)
106
107     const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
108
109     targetVideo.channelId = channel.id
110
111     const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFullLight
112     targetVideoUpdated.VideoChannel = channel
113
114     if (targetVideoUpdated.hasPrivacyForFederation() && targetVideoUpdated.state === VideoState.PUBLISHED) {
115       await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
116       await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
117     }
118
119     videoChangeOwnership.status = VideoChangeOwnershipStatus.ACCEPTED
120     await videoChangeOwnership.save({ transaction: t })
121
122     return res.sendStatus(204)
123   })
124 }
125
126 async function refuseOwnership (req: express.Request, res: express.Response) {
127   return sequelizeTypescript.transaction(async t => {
128     const videoChangeOwnership = res.locals.videoChangeOwnership
129
130     videoChangeOwnership.status = VideoChangeOwnershipStatus.REFUSED
131     await videoChangeOwnership.save({ transaction: t })
132
133     return res.sendStatus(204)
134   })
135 }