1 import * as express from 'express'
2 import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared'
3 import { getFormattedObjects, logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { setAsyncActorKeys } from '../../../lib/activitypub'
6 import { createVideoChannel } from '../../../lib/video-channel'
10 listVideoAccountChannelsValidator,
14 videoChannelsAddValidator,
15 videoChannelsGetValidator,
16 videoChannelsRemoveValidator,
17 videoChannelsSortValidator,
18 videoChannelsUpdateValidator
19 } from '../../../middlewares'
20 import { AccountModel } from '../../../models/account/account'
21 import { VideoChannelModel } from '../../../models/video/video-channel'
23 const videoChannelRouter = express.Router()
25 videoChannelRouter.get('/channels',
27 videoChannelsSortValidator,
30 asyncMiddleware(listVideoChannels)
33 videoChannelRouter.get('/accounts/:accountId/channels',
34 asyncMiddleware(listVideoAccountChannelsValidator),
35 asyncMiddleware(listVideoAccountChannels)
38 videoChannelRouter.post('/channels',
40 videoChannelsAddValidator,
41 asyncMiddleware(addVideoChannelRetryWrapper)
44 videoChannelRouter.put('/channels/:id',
46 asyncMiddleware(videoChannelsUpdateValidator),
47 updateVideoChannelRetryWrapper
50 videoChannelRouter.delete('/channels/:id',
52 asyncMiddleware(videoChannelsRemoveValidator),
53 asyncMiddleware(removeVideoChannelRetryWrapper)
56 videoChannelRouter.get('/channels/:id',
57 asyncMiddleware(videoChannelsGetValidator),
58 asyncMiddleware(getVideoChannel)
61 // ---------------------------------------------------------------------------
67 // ---------------------------------------------------------------------------
69 async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
70 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
72 return res.json(getFormattedObjects(resultList.data, resultList.total))
75 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
76 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
78 return res.json(getFormattedObjects(resultList.data, resultList.total))
81 // Wrapper to video channel add that retry the async function if there is a database error
82 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
83 async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
85 arguments: [ req, res ],
86 errorMessage: 'Cannot insert the video video channel with many retries.'
89 await retryTransactionWrapper(addVideoChannel, options)
91 // TODO : include Location of the new video channel -> 201
92 return res.type('json').status(204).end()
95 async function addVideoChannel (req: express.Request, res: express.Response) {
96 const videoChannelInfo: VideoChannelCreate = req.body
97 const account: AccountModel = res.locals.oauth.token.User.Account
99 const videoChannelCreated = await sequelizeTypescript.transaction(async t => {
100 return createVideoChannel(videoChannelInfo, account, t)
103 setAsyncActorKeys(videoChannelCreated.Actor)
105 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
108 async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
110 arguments: [ req, res ],
111 errorMessage: 'Cannot update the video with many retries.'
114 await retryTransactionWrapper(updateVideoChannel, options)
116 return res.type('json').status(204).end()
119 async function updateVideoChannel (req: express.Request, res: express.Response) {
120 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
121 const videoChannelFieldsSave = videoChannelInstance.toJSON()
122 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
125 await sequelizeTypescript.transaction(async t => {
126 const sequelizeOptions = {
130 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
131 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
133 await videoChannelInstance.save(sequelizeOptions)
136 // await sendUpdateVideoChannel(videoChannelInstanceUpdated, t)
139 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
141 logger.debug('Cannot update the video channel.', err)
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)
152 async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
154 arguments: [ req, res ],
155 errorMessage: 'Cannot remove the video channel with many retries.'
158 await retryTransactionWrapper(removeVideoChannel, options)
160 return res.type('json').status(204).end()
163 async function removeVideoChannel (req: express.Request, res: express.Response) {
164 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
166 return sequelizeTypescript.transaction(async t => {
167 await videoChannelInstance.destroy({ transaction: t })
169 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
174 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
175 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
177 return res.json(videoChannelWithVideos.toFormattedJSON())