1 import * as express from 'express'
2 import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
10 videoChannelsAddValidator,
11 videoChannelsGetValidator,
12 videoChannelsRemoveValidator,
13 videoChannelsSortValidator,
14 videoChannelsUpdateValidator
15 } from '../../middlewares'
16 import { VideoChannelModel } from '../../models/video/video-channel'
17 import { videosSortValidator } from '../../middlewares/validators'
18 import { sendUpdateActor } from '../../lib/activitypub/send'
19 import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
20 import { createVideoChannel } from '../../lib/video-channel'
21 import { isNSFWHidden } from '../../helpers/express-utils'
22 import { setAsyncActorKeys } from '../../lib/activitypub'
23 import { retryTransactionWrapper } from '../../helpers/database-utils'
24 import { AccountModel } from '../../models/account/account'
25 import { sequelizeTypescript } from '../../initializers'
26 import { logger } from '../../helpers/logger'
27 import { VideoModel } from '../../models/video/video'
29 const videoChannelRouter = express.Router()
31 videoChannelRouter.get('/',
33 videoChannelsSortValidator,
36 asyncMiddleware(listVideoChannels)
39 videoChannelRouter.post('/',
41 videoChannelsAddValidator,
42 asyncMiddleware(addVideoChannelRetryWrapper)
45 videoChannelRouter.put('/:id',
47 asyncMiddleware(videoChannelsUpdateValidator),
48 updateVideoChannelRetryWrapper
51 videoChannelRouter.delete('/:id',
53 asyncMiddleware(videoChannelsRemoveValidator),
54 asyncMiddleware(removeVideoChannelRetryWrapper)
57 videoChannelRouter.get('/:id',
58 asyncMiddleware(videoChannelsGetValidator),
59 asyncMiddleware(getVideoChannel)
62 videoChannelRouter.get('/:id/videos',
63 asyncMiddleware(videoChannelsGetValidator),
69 asyncMiddleware(listVideoChannelVideos)
72 // ---------------------------------------------------------------------------
78 // ---------------------------------------------------------------------------
80 async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
81 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
83 return res.json(getFormattedObjects(resultList.data, resultList.total))
86 // Wrapper to video channel add that retry the async function if there is a database error
87 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
88 async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
90 arguments: [ req, res ],
91 errorMessage: 'Cannot insert the video video channel with many retries.'
94 const videoChannel = await retryTransactionWrapper(addVideoChannel, options)
98 uuid: videoChannel.Actor.uuid
103 async function addVideoChannel (req: express.Request, res: express.Response) {
104 const videoChannelInfo: VideoChannelCreate = req.body
105 const account: AccountModel = res.locals.oauth.token.User.Account
107 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
108 return createVideoChannel(videoChannelInfo, account, t)
111 setAsyncActorKeys(videoChannelCreated.Actor)
112 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
114 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
116 return videoChannelCreated
119 async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
121 arguments: [ req, res ],
122 errorMessage: 'Cannot update the video with many retries.'
125 await retryTransactionWrapper(updateVideoChannel, options)
127 return res.type('json').status(204).end()
130 async function updateVideoChannel (req: express.Request, res: express.Response) {
131 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
132 const videoChannelFieldsSave = videoChannelInstance.toJSON()
133 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
136 await sequelizeTypescript.transaction(async t => {
137 const sequelizeOptions = {
141 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
142 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
143 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
145 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
146 await sendUpdateActor(videoChannelInstanceUpdated, t)
149 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
151 logger.debug('Cannot update the video channel.', { err })
153 // Force fields we want to update
154 // If the transaction is retried, sequelize will think the object has not changed
155 // So it will skip the SQL request, even if the last one was ROLLBACKed!
156 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
162 async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
164 arguments: [ req, res ],
165 errorMessage: 'Cannot remove the video channel with many retries.'
168 await retryTransactionWrapper(removeVideoChannel, options)
170 return res.type('json').status(204).end()
173 async function removeVideoChannel (req: express.Request, res: express.Response) {
174 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
176 return sequelizeTypescript.transaction(async t => {
177 await videoChannelInstance.destroy({ transaction: t })
179 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
184 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
185 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
187 return res.json(videoChannelWithVideos.toFormattedJSON())
190 async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
191 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
193 const resultList = await VideoModel.listForApi({
194 start: req.query.start,
195 count: req.query.count,
196 sort: req.query.sort,
197 hideNSFW: isNSFWHidden(res),
199 videoChannelId: videoChannelInstance.id
202 return res.json(getFormattedObjects(resultList.data, resultList.total))