1 import * as express from 'express'
2 import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared'
3 import { getFormattedObjects, logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers'
4 import { database as db } from '../../../initializers'
5 import { createVideoChannel } from '../../../lib'
6 import { sendUpdateVideoChannel } from '../../../lib/activitypub/send/send-update'
10 listVideoAccountChannelsValidator,
14 videoChannelsAddValidator,
15 videoChannelsGetValidator,
16 videoChannelsRemoveValidator,
17 videoChannelsSortValidator,
18 videoChannelsUpdateValidator
19 } from '../../../middlewares'
20 import { AccountInstance, VideoChannelInstance } from '../../../models'
22 const videoChannelRouter = express.Router()
24 videoChannelRouter.get('/channels',
26 videoChannelsSortValidator,
29 asyncMiddleware(listVideoChannels)
32 videoChannelRouter.get('/accounts/:accountId/channels',
33 asyncMiddleware(listVideoAccountChannelsValidator),
34 asyncMiddleware(listVideoAccountChannels)
37 videoChannelRouter.post('/channels',
39 videoChannelsAddValidator,
40 asyncMiddleware(addVideoChannelRetryWrapper)
43 videoChannelRouter.put('/channels/:id',
45 asyncMiddleware(videoChannelsUpdateValidator),
46 updateVideoChannelRetryWrapper
49 videoChannelRouter.delete('/channels/:id',
51 asyncMiddleware(videoChannelsRemoveValidator),
52 asyncMiddleware(removeVideoChannelRetryWrapper)
55 videoChannelRouter.get('/channels/:id',
56 asyncMiddleware(videoChannelsGetValidator),
57 asyncMiddleware(getVideoChannel)
60 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
69 const resultList = await db.VideoChannel.listForApi(req.query.start, req.query.count, req.query.sort)
71 return res.json(getFormattedObjects(resultList.data, resultList.total))
74 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
75 const resultList = await db.VideoChannel.listByAccount(res.locals.account.id)
77 return res.json(getFormattedObjects(resultList.data, resultList.total))
80 // Wrapper to video channel add that retry the async function if there is a database error
81 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
82 async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
84 arguments: [ req, res ],
85 errorMessage: 'Cannot insert the video video channel with many retries.'
88 await retryTransactionWrapper(addVideoChannel, options)
90 // TODO : include Location of the new video channel -> 201
91 return res.type('json').status(204).end()
94 async function addVideoChannel (req: express.Request, res: express.Response) {
95 const videoChannelInfo: VideoChannelCreate = req.body
96 const account: AccountInstance = res.locals.oauth.token.User.Account
97 let videoChannelCreated: VideoChannelInstance
99 await db.sequelize.transaction(async t => {
100 videoChannelCreated = await createVideoChannel(videoChannelInfo, account, t)
103 logger.info('Video channel with uuid %s created.', videoChannelCreated.uuid)
106 async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
108 arguments: [ req, res ],
109 errorMessage: 'Cannot update the video with many retries.'
112 await retryTransactionWrapper(updateVideoChannel, options)
114 return res.type('json').status(204).end()
117 async function updateVideoChannel (req: express.Request, res: express.Response) {
118 const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel
119 const videoChannelFieldsSave = videoChannelInstance.toJSON()
120 const videoChannelInfoToUpdate: VideoChannelUpdate = req.body
123 await db.sequelize.transaction(async t => {
124 const sequelizeOptions = {
128 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
129 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
131 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
133 await sendUpdateVideoChannel(videoChannelInstanceUpdated, t)
136 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.uuid)
138 logger.debug('Cannot update the video channel.', err)
140 // Force fields we want to update
141 // If the transaction is retried, sequelize will think the object has not changed
142 // So it will skip the SQL request, even if the last one was ROLLBACKed!
143 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
149 async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
151 arguments: [ req, res ],
152 errorMessage: 'Cannot remove the video channel with many retries.'
155 await retryTransactionWrapper(removeVideoChannel, options)
157 return res.type('json').status(204).end()
160 async function removeVideoChannel (req: express.Request, res: express.Response) {
161 const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel
163 await db.sequelize.transaction(async t => {
164 await videoChannelInstance.destroy({ transaction: t })
167 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.uuid)
170 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
171 const videoChannelWithVideos = await db.VideoChannel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
173 return res.json(videoChannelWithVideos.toFormattedJSON())