2017-10-24 13:41:09 -04:00
|
|
|
import * as express from 'express'
|
2017-11-10 11:27:49 -05:00
|
|
|
import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { getFormattedObjects, resetSequelizeInstance } from '../../../helpers/utils'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
2017-12-14 11:38:41 -05:00
|
|
|
import { setAsyncActorKeys } from '../../../lib/activitypub'
|
2018-02-15 08:46:26 -05:00
|
|
|
import { sendUpdateActor } from '../../../lib/activitypub/send'
|
2017-12-14 11:38:41 -05:00
|
|
|
import { createVideoChannel } from '../../../lib/video-channel'
|
2017-10-24 13:41:09 -04:00
|
|
|
import {
|
2018-01-18 04:53:54 -05:00
|
|
|
asyncMiddleware, authenticate, listVideoAccountChannelsValidator, paginationValidator, setDefaultSort, setDefaultPagination,
|
2017-12-28 05:16:08 -05:00
|
|
|
videoChannelsAddValidator, videoChannelsGetValidator, videoChannelsRemoveValidator, videoChannelsSortValidator,
|
2017-11-10 11:27:49 -05:00
|
|
|
videoChannelsUpdateValidator
|
2017-10-24 13:41:09 -04:00
|
|
|
} from '../../../middlewares'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { AccountModel } from '../../../models/account/account'
|
|
|
|
import { VideoChannelModel } from '../../../models/video/video-channel'
|
2017-10-24 13:41:09 -04:00
|
|
|
|
|
|
|
const videoChannelRouter = express.Router()
|
|
|
|
|
|
|
|
videoChannelRouter.get('/channels',
|
|
|
|
paginationValidator,
|
|
|
|
videoChannelsSortValidator,
|
2018-01-17 04:50:33 -05:00
|
|
|
setDefaultSort,
|
2018-01-18 04:53:54 -05:00
|
|
|
setDefaultPagination,
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(listVideoChannels)
|
2017-10-24 13:41:09 -04:00
|
|
|
)
|
|
|
|
|
2017-11-10 08:48:08 -05:00
|
|
|
videoChannelRouter.get('/accounts/:accountId/channels',
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(listVideoAccountChannelsValidator),
|
2017-11-10 08:48:08 -05:00
|
|
|
asyncMiddleware(listVideoAccountChannels)
|
2017-10-24 13:41:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
videoChannelRouter.post('/channels',
|
|
|
|
authenticate,
|
|
|
|
videoChannelsAddValidator,
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(addVideoChannelRetryWrapper)
|
2017-10-24 13:41:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
videoChannelRouter.put('/channels/:id',
|
|
|
|
authenticate,
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(videoChannelsUpdateValidator),
|
2017-10-24 13:41:09 -04:00
|
|
|
updateVideoChannelRetryWrapper
|
|
|
|
)
|
|
|
|
|
|
|
|
videoChannelRouter.delete('/channels/:id',
|
|
|
|
authenticate,
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(videoChannelsRemoveValidator),
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(removeVideoChannelRetryWrapper)
|
2017-10-24 13:41:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
videoChannelRouter.get('/channels/:id',
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(videoChannelsGetValidator),
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(getVideoChannel)
|
2017-10-24 13:41:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoChannelRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-10 08:48:08 -05:00
|
|
|
async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
// Wrapper to video channel add that retry the async function if there is a database error
|
2017-10-24 13:41:09 -04:00
|
|
|
// We need this because we run the transaction in SERIALIZABLE isolation that can fail
|
2017-10-25 05:55:06 -04:00
|
|
|
async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-24 13:41:09 -04:00
|
|
|
const options = {
|
|
|
|
arguments: [ req, res ],
|
|
|
|
errorMessage: 'Cannot insert the video video channel with many retries.'
|
|
|
|
}
|
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
const videoChannel = await retryTransactionWrapper(addVideoChannel, options)
|
|
|
|
return res.json({
|
|
|
|
videoChannel: {
|
|
|
|
id: videoChannel.id
|
|
|
|
}
|
|
|
|
}).end()
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
async function addVideoChannel (req: express.Request, res: express.Response) {
|
2017-10-24 13:41:09 -04:00
|
|
|
const videoChannelInfo: VideoChannelCreate = req.body
|
2017-12-12 11:53:50 -05:00
|
|
|
const account: AccountModel = res.locals.oauth.token.User.Account
|
2017-10-24 13:41:09 -04:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
|
2017-12-14 11:38:41 -05:00
|
|
|
return createVideoChannel(videoChannelInfo, account, t)
|
2017-12-14 05:18:49 -05:00
|
|
|
})
|
2017-12-14 11:38:41 -05:00
|
|
|
|
|
|
|
setAsyncActorKeys(videoChannelCreated.Actor)
|
2018-02-15 08:46:26 -05:00
|
|
|
.catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, err))
|
2017-12-14 11:38:41 -05:00
|
|
|
|
|
|
|
logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
|
2018-02-15 08:46:26 -05:00
|
|
|
|
|
|
|
return videoChannelCreated
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-24 13:41:09 -04:00
|
|
|
const options = {
|
|
|
|
arguments: [ req, res ],
|
|
|
|
errorMessage: 'Cannot update the video with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
await retryTransactionWrapper(updateVideoChannel, options)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function updateVideoChannel (req: express.Request, res: express.Response) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
|
2017-10-24 13:41:09 -04:00
|
|
|
const videoChannelFieldsSave = videoChannelInstance.toJSON()
|
2017-12-12 11:53:50 -05:00
|
|
|
const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
|
2017-10-24 13:41:09 -04:00
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
try {
|
2017-12-12 11:53:50 -05:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2017-10-25 05:55:06 -04:00
|
|
|
const sequelizeOptions = {
|
|
|
|
transaction: t
|
|
|
|
}
|
2017-10-24 13:41:09 -04:00
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
|
|
|
|
if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
|
2018-02-15 08:46:26 -05:00
|
|
|
if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
|
2017-10-24 13:41:09 -04:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
|
|
|
|
await sendUpdateActor(videoChannelInstanceUpdated, t)
|
2017-10-24 13:41:09 -04:00
|
|
|
})
|
2017-10-25 05:55:06 -04:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
|
2017-10-25 05:55:06 -04:00
|
|
|
} catch (err) {
|
|
|
|
logger.debug('Cannot update the video channel.', err)
|
|
|
|
|
|
|
|
// Force fields we want to update
|
|
|
|
// If the transaction is retried, sequelize will think the object has not changed
|
|
|
|
// So it will skip the SQL request, even if the last one was ROLLBACKed!
|
|
|
|
resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
|
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-24 13:41:09 -04:00
|
|
|
const options = {
|
|
|
|
arguments: [ req, res ],
|
|
|
|
errorMessage: 'Cannot remove the video channel with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
await retryTransactionWrapper(removeVideoChannel, options)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function removeVideoChannel (req: express.Request, res: express.Response) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
|
2017-10-24 13:41:09 -04:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
2017-10-25 05:55:06 -04:00
|
|
|
await videoChannelInstance.destroy({ transaction: t })
|
2017-12-14 11:38:41 -05:00
|
|
|
|
|
|
|
logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
|
2017-10-24 13:41:09 -04:00
|
|
|
})
|
2017-10-25 05:55:06 -04:00
|
|
|
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
|
|
|
return res.json(videoChannelWithVideos.toFormattedJSON())
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|