1 import * as express from 'express'
3 import { database as db } from '../../../initializers'
7 retryTransactionWrapper,
9 } from '../../../helpers'
13 videoChannelsSortValidator,
14 videoChannelsAddValidator,
17 videoChannelsRemoveValidator,
18 videoChannelGetValidator,
19 videoChannelsUpdateValidator,
20 listVideoAuthorChannelsValidator,
22 } from '../../../middlewares'
25 updateVideoChannelToFriends
27 import { VideoChannelInstance, AuthorInstance } from '../../../models'
28 import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared'
30 const videoChannelRouter = express.Router()
32 videoChannelRouter.get('/channels',
34 videoChannelsSortValidator,
37 asyncMiddleware(listVideoChannels)
40 videoChannelRouter.get('/authors/:authorId/channels',
41 listVideoAuthorChannelsValidator,
42 asyncMiddleware(listVideoAuthorChannels)
45 videoChannelRouter.post('/channels',
47 videoChannelsAddValidator,
48 asyncMiddleware(addVideoChannelRetryWrapper)
51 videoChannelRouter.put('/channels/:id',
53 videoChannelsUpdateValidator,
54 updateVideoChannelRetryWrapper
57 videoChannelRouter.delete('/channels/:id',
59 videoChannelsRemoveValidator,
60 asyncMiddleware(removeVideoChannelRetryWrapper)
63 videoChannelRouter.get('/channels/:id',
64 videoChannelGetValidator,
65 asyncMiddleware(getVideoChannel)
68 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
76 async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
77 const resultList = await db.VideoChannel.listForApi(req.query.start, req.query.count, req.query.sort)
79 return res.json(getFormattedObjects(resultList.data, resultList.total))
82 async function listVideoAuthorChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
83 const resultList = await db.VideoChannel.listByAuthor(res.locals.author.id)
85 return res.json(getFormattedObjects(resultList.data, resultList.total))
88 // Wrapper to video channel add that retry the async function if there is a database error
89 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
90 async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
92 arguments: [ req, res ],
93 errorMessage: 'Cannot insert the video video channel with many retries.'
96 await retryTransactionWrapper(addVideoChannel, options)
98 // TODO : include Location of the new video channel -> 201
99 return res.type('json').status(204).end()
102 async function addVideoChannel (req: express.Request, res: express.Response) {
103 const videoChannelInfo: VideoChannelCreate = req.body
104 const author: AuthorInstance = res.locals.oauth.token.User.Author
105 let videoChannelCreated: VideoChannelInstance
107 await db.sequelize.transaction(async t => {
108 videoChannelCreated = await createVideoChannel(videoChannelInfo, author, t)
111 logger.info('Video channel with uuid %s created.', videoChannelCreated.uuid)
114 async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
116 arguments: [ req, res ],
117 errorMessage: 'Cannot update the video with many retries.'
120 await retryTransactionWrapper(updateVideoChannel, options)
122 return res.type('json').status(204).end()
125 async function updateVideoChannel (req: express.Request, res: express.Response) {
126 const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel
127 const videoChannelFieldsSave = videoChannelInstance.toJSON()
128 const videoChannelInfoToUpdate: VideoChannelUpdate = req.body
131 await db.sequelize.transaction(async t => {
132 const sequelizeOptions = {
136 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
137 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
139 await videoChannelInstance.save(sequelizeOptions)
140 const json = videoChannelInstance.toUpdateRemoteJSON()
142 // Now we'll update the video channel's meta data to our friends
143 return updateVideoChannelToFriends(json, t)
147 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.uuid)
149 logger.debug('Cannot update the video channel.', err)
151 // Force fields we want to update
152 // If the transaction is retried, sequelize will think the object has not changed
153 // So it will skip the SQL request, even if the last one was ROLLBACKed!
154 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
160 async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
162 arguments: [ req, res ],
163 errorMessage: 'Cannot remove the video channel with many retries.'
166 await retryTransactionWrapper(removeVideoChannel, options)
168 return res.type('json').status(204).end()
171 async function removeVideoChannel (req: express.Request, res: express.Response) {
172 const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel
174 await db.sequelize.transaction(async t => {
175 await videoChannelInstance.destroy({ transaction: t })
178 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.uuid)
181 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
182 const videoChannelWithVideos = await db.VideoChannel.loadAndPopulateAuthorAndVideos(res.locals.videoChannel.id)
184 return res.json(videoChannelWithVideos.toFormattedJSON())