]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/channel.ts
Add dirty migration :/
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / channel.ts
CommitLineData
72c7248b 1import * as express from 'express'
571389d4
C
2import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared'
3import { getFormattedObjects, logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers'
3fd3ab2d 4import { sequelizeTypescript } from '../../../initializers'
50d6de9c
C
5import { setAsyncActorKeys } from '../../../lib/activitypub'
6import { createVideoChannel } from '../../../lib/video-channel'
72c7248b 7import {
571389d4 8 asyncMiddleware,
72c7248b 9 authenticate,
571389d4 10 listVideoAccountChannelsValidator,
72c7248b 11 paginationValidator,
72c7248b 12 setPagination,
571389d4 13 setVideoChannelsSort,
571389d4 14 videoChannelsAddValidator,
a2431b7d 15 videoChannelsGetValidator,
571389d4
C
16 videoChannelsRemoveValidator,
17 videoChannelsSortValidator,
18 videoChannelsUpdateValidator
72c7248b 19} from '../../../middlewares'
3fd3ab2d
C
20import { AccountModel } from '../../../models/account/account'
21import { VideoChannelModel } from '../../../models/video/video-channel'
72c7248b
C
22
23const videoChannelRouter = express.Router()
24
25videoChannelRouter.get('/channels',
26 paginationValidator,
27 videoChannelsSortValidator,
28 setVideoChannelsSort,
29 setPagination,
eb080476 30 asyncMiddleware(listVideoChannels)
72c7248b
C
31)
32
38fa2065 33videoChannelRouter.get('/accounts/:accountId/channels',
a2431b7d 34 asyncMiddleware(listVideoAccountChannelsValidator),
38fa2065 35 asyncMiddleware(listVideoAccountChannels)
72c7248b
C
36)
37
38videoChannelRouter.post('/channels',
39 authenticate,
40 videoChannelsAddValidator,
eb080476 41 asyncMiddleware(addVideoChannelRetryWrapper)
72c7248b
C
42)
43
44videoChannelRouter.put('/channels/:id',
45 authenticate,
a2431b7d 46 asyncMiddleware(videoChannelsUpdateValidator),
72c7248b
C
47 updateVideoChannelRetryWrapper
48)
49
50videoChannelRouter.delete('/channels/:id',
51 authenticate,
a2431b7d 52 asyncMiddleware(videoChannelsRemoveValidator),
eb080476 53 asyncMiddleware(removeVideoChannelRetryWrapper)
72c7248b
C
54)
55
56videoChannelRouter.get('/channels/:id',
a2431b7d 57 asyncMiddleware(videoChannelsGetValidator),
eb080476 58 asyncMiddleware(getVideoChannel)
72c7248b
C
59)
60
61// ---------------------------------------------------------------------------
62
63export {
64 videoChannelRouter
65}
66
67// ---------------------------------------------------------------------------
68
eb080476 69async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 70 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
eb080476
C
71
72 return res.json(getFormattedObjects(resultList.data, resultList.total))
72c7248b
C
73}
74
38fa2065 75async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 76 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
eb080476
C
77
78 return res.json(getFormattedObjects(resultList.data, resultList.total))
72c7248b
C
79}
80
eb080476 81// Wrapper to video channel add that retry the async function if there is a database error
72c7248b 82// We need this because we run the transaction in SERIALIZABLE isolation that can fail
eb080476 83async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
72c7248b
C
84 const options = {
85 arguments: [ req, res ],
86 errorMessage: 'Cannot insert the video video channel with many retries.'
87 }
88
eb080476
C
89 await retryTransactionWrapper(addVideoChannel, options)
90
91 // TODO : include Location of the new video channel -> 201
92 return res.type('json').status(204).end()
72c7248b
C
93}
94
50d6de9c 95async function addVideoChannel (req: express.Request, res: express.Response) {
72c7248b 96 const videoChannelInfo: VideoChannelCreate = req.body
3fd3ab2d 97 const account: AccountModel = res.locals.oauth.token.User.Account
72c7248b 98
50d6de9c
C
99 const videoChannelCreated = await sequelizeTypescript.transaction(async t => {
100 return createVideoChannel(videoChannelInfo, account, t)
fadf619a 101 })
50d6de9c
C
102
103 setAsyncActorKeys(videoChannelCreated.Actor)
104
105 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
72c7248b
C
106}
107
eb080476 108async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
72c7248b
C
109 const options = {
110 arguments: [ req, res ],
111 errorMessage: 'Cannot update the video with many retries.'
112 }
113
eb080476
C
114 await retryTransactionWrapper(updateVideoChannel, options)
115
116 return res.type('json').status(204).end()
72c7248b
C
117}
118
eb080476 119async function updateVideoChannel (req: express.Request, res: express.Response) {
3fd3ab2d 120 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
72c7248b 121 const videoChannelFieldsSave = videoChannelInstance.toJSON()
3fd3ab2d 122 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
72c7248b 123
eb080476 124 try {
3fd3ab2d 125 await sequelizeTypescript.transaction(async t => {
eb080476
C
126 const sequelizeOptions = {
127 transaction: t
128 }
72c7248b 129
eb080476
C
130 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
131 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
72c7248b 132
50d6de9c 133 await videoChannelInstance.save(sequelizeOptions)
72c7248b 134
50d6de9c
C
135 // TODO
136 // await sendUpdateVideoChannel(videoChannelInstanceUpdated, t)
72c7248b 137 })
eb080476 138
50d6de9c 139 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
eb080476
C
140 } catch (err) {
141 logger.debug('Cannot update the video channel.', err)
142
143 // Force fields we want to update
144 // If the transaction is retried, sequelize will think the object has not changed
145 // So it will skip the SQL request, even if the last one was ROLLBACKed!
146 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
147
148 throw err
149 }
72c7248b
C
150}
151
eb080476 152async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
72c7248b
C
153 const options = {
154 arguments: [ req, res ],
155 errorMessage: 'Cannot remove the video channel with many retries.'
156 }
157
eb080476
C
158 await retryTransactionWrapper(removeVideoChannel, options)
159
160 return res.type('json').status(204).end()
72c7248b
C
161}
162
eb080476 163async function removeVideoChannel (req: express.Request, res: express.Response) {
3fd3ab2d 164 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
72c7248b 165
50d6de9c 166 return sequelizeTypescript.transaction(async t => {
eb080476 167 await videoChannelInstance.destroy({ transaction: t })
50d6de9c
C
168
169 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
72c7248b 170 })
eb080476 171
72c7248b
C
172}
173
eb080476 174async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 175 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
eb080476
C
176
177 return res.json(videoChannelWithVideos.toFormattedJSON())
72c7248b 178}