1 import * as express from 'express'
2 import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { getFormattedObjects, resetSequelizeInstance } from '../../../helpers/utils'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { setAsyncActorKeys } from '../../../lib/activitypub'
8 import { createVideoChannel } from '../../../lib/video-channel'
10 asyncMiddleware, authenticate, listVideoAccountChannelsValidator, paginationValidator, setDefaultSort, setDefaultPagination,
11 videoChannelsAddValidator, videoChannelsGetValidator, videoChannelsRemoveValidator, videoChannelsSortValidator,
12 videoChannelsUpdateValidator
13 } from '../../../middlewares'
14 import { AccountModel } from '../../../models/account/account'
15 import { VideoChannelModel } from '../../../models/video/video-channel'
17 const videoChannelRouter = express.Router()
19 videoChannelRouter.get('/channels',
21 videoChannelsSortValidator,
24 asyncMiddleware(listVideoChannels)
27 videoChannelRouter.get('/accounts/:accountId/channels',
28 asyncMiddleware(listVideoAccountChannelsValidator),
29 asyncMiddleware(listVideoAccountChannels)
32 videoChannelRouter.post('/channels',
34 videoChannelsAddValidator,
35 asyncMiddleware(addVideoChannelRetryWrapper)
38 videoChannelRouter.put('/channels/:id',
40 asyncMiddleware(videoChannelsUpdateValidator),
41 updateVideoChannelRetryWrapper
44 videoChannelRouter.delete('/channels/:id',
46 asyncMiddleware(videoChannelsRemoveValidator),
47 asyncMiddleware(removeVideoChannelRetryWrapper)
50 videoChannelRouter.get('/channels/:id',
51 asyncMiddleware(videoChannelsGetValidator),
52 asyncMiddleware(getVideoChannel)
55 // ---------------------------------------------------------------------------
61 // ---------------------------------------------------------------------------
63 async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
64 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
66 return res.json(getFormattedObjects(resultList.data, resultList.total))
69 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
70 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
72 return res.json(getFormattedObjects(resultList.data, resultList.total))
75 // Wrapper to video channel add that retry the async function if there is a database error
76 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
77 async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
79 arguments: [ req, res ],
80 errorMessage: 'Cannot insert the video video channel with many retries.'
83 await retryTransactionWrapper(addVideoChannel, options)
85 // TODO : include Location of the new video channel -> 201
86 return res.type('json').status(204).end()
89 async function addVideoChannel (req: express.Request, res: express.Response) {
90 const videoChannelInfo: VideoChannelCreate = req.body
91 const account: AccountModel = res.locals.oauth.token.User.Account
93 const videoChannelCreated = await sequelizeTypescript.transaction(async t => {
94 return createVideoChannel(videoChannelInfo, account, t)
97 setAsyncActorKeys(videoChannelCreated.Actor)
99 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
102 async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
104 arguments: [ req, res ],
105 errorMessage: 'Cannot update the video with many retries.'
108 await retryTransactionWrapper(updateVideoChannel, options)
110 return res.type('json').status(204).end()
113 async function updateVideoChannel (req: express.Request, res: express.Response) {
114 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
115 const videoChannelFieldsSave = videoChannelInstance.toJSON()
116 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
119 await sequelizeTypescript.transaction(async t => {
120 const sequelizeOptions = {
124 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
125 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
127 await videoChannelInstance.save(sequelizeOptions)
130 // await sendUpdateVideoChannel(videoChannelInstanceUpdated, t)
133 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
135 logger.debug('Cannot update the video channel.', err)
137 // Force fields we want to update
138 // If the transaction is retried, sequelize will think the object has not changed
139 // So it will skip the SQL request, even if the last one was ROLLBACKed!
140 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
146 async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
148 arguments: [ req, res ],
149 errorMessage: 'Cannot remove the video channel with many retries.'
152 await retryTransactionWrapper(removeVideoChannel, options)
154 return res.type('json').status(204).end()
157 async function removeVideoChannel (req: express.Request, res: express.Response) {
158 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
160 return sequelizeTypescript.transaction(async t => {
161 await videoChannelInstance.destroy({ transaction: t })
163 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
168 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
169 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
171 return res.json(videoChannelWithVideos.toFormattedJSON())