1
0
Fork 0
peertube/server/controllers/api/videos/channel.ts

186 lines
6.1 KiB
TypeScript
Raw Normal View History

2017-10-24 13:41:09 -04:00
import * as express from 'express'
import { database as db } from '../../../initializers'
import {
logger,
getFormattedObjects,
2017-10-25 05:55:06 -04:00
retryTransactionWrapper,
resetSequelizeInstance
2017-10-24 13:41:09 -04:00
} from '../../../helpers'
import {
authenticate,
paginationValidator,
videoChannelsSortValidator,
videoChannelsAddValidator,
setVideoChannelsSort,
setPagination,
videoChannelsRemoveValidator,
videoChannelGetValidator,
videoChannelsUpdateValidator,
2017-10-25 05:55:06 -04:00
listVideoAuthorChannelsValidator,
asyncMiddleware
2017-10-24 13:41:09 -04:00
} from '../../../middlewares'
import {
createVideoChannel,
updateVideoChannelToFriends
} from '../../../lib'
import { VideoChannelInstance, AuthorInstance } from '../../../models'
import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared'
const videoChannelRouter = express.Router()
videoChannelRouter.get('/channels',
paginationValidator,
videoChannelsSortValidator,
setVideoChannelsSort,
setPagination,
2017-10-25 05:55:06 -04:00
asyncMiddleware(listVideoChannels)
2017-10-24 13:41:09 -04:00
)
videoChannelRouter.get('/authors/:authorId/channels',
listVideoAuthorChannelsValidator,
2017-10-25 05:55:06 -04:00
asyncMiddleware(listVideoAuthorChannels)
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,
videoChannelsUpdateValidator,
updateVideoChannelRetryWrapper
)
videoChannelRouter.delete('/channels/:id',
authenticate,
videoChannelsRemoveValidator,
2017-10-25 05:55:06 -04:00
asyncMiddleware(removeVideoChannelRetryWrapper)
2017-10-24 13:41:09 -04:00
)
videoChannelRouter.get('/channels/:id',
videoChannelGetValidator,
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) {
const resultList = await db.VideoChannel.listForApi(req.query.start, req.query.count, req.query.sort)
return res.json(getFormattedObjects(resultList.data, resultList.total))
2017-10-24 13:41:09 -04:00
}
2017-10-25 05:55:06 -04:00
async function listVideoAuthorChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
const resultList = await db.VideoChannel.listByAuthor(res.locals.author.id)
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.'
}
2017-10-25 05:55:06 -04:00
await retryTransactionWrapper(addVideoChannel, options)
// TODO : include Location of the new video channel -> 201
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 addVideoChannel (req: express.Request, res: express.Response) {
2017-10-24 13:41:09 -04:00
const videoChannelInfo: VideoChannelCreate = req.body
const author: AuthorInstance = res.locals.oauth.token.User.Author
2017-10-25 05:55:06 -04:00
let videoChannelCreated: VideoChannelInstance
2017-10-24 13:41:09 -04:00
2017-10-25 05:55:06 -04:00
await db.sequelize.transaction(async t => {
videoChannelCreated = await createVideoChannel(videoChannelInfo, author, t)
2017-10-24 13:41:09 -04:00
})
2017-10-25 05:55:06 -04:00
logger.info('Video channel with uuid %s created.', videoChannelCreated.uuid)
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-10-24 13:41:09 -04:00
const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel
const videoChannelFieldsSave = videoChannelInstance.toJSON()
const videoChannelInfoToUpdate: VideoChannelUpdate = req.body
2017-10-25 05:55:06 -04:00
try {
await db.sequelize.transaction(async t => {
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)
2017-10-24 13:41:09 -04:00
2017-10-25 05:55:06 -04:00
await videoChannelInstance.save(sequelizeOptions)
const json = videoChannelInstance.toUpdateRemoteJSON()
// Now we'll update the video channel's meta data to our friends
return updateVideoChannelToFriends(json, t)
2017-10-24 13:41:09 -04:00
})
2017-10-25 05:55:06 -04:00
logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.uuid)
} 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-10-24 13:41:09 -04:00
const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel
2017-10-25 05:55:06 -04:00
await db.sequelize.transaction(async t => {
await videoChannelInstance.destroy({ transaction: t })
2017-10-24 13:41:09 -04:00
})
2017-10-25 05:55:06 -04:00
logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.uuid)
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) {
const videoChannelWithVideos = await db.VideoChannel.loadAndPopulateAuthorAndVideos(res.locals.videoChannel.id)
return res.json(videoChannelWithVideos.toFormattedJSON())
2017-10-24 13:41:09 -04:00
}