1 import * as express from 'express'
2 import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
5 asyncRetryTransactionMiddleware,
11 videoChannelsAddValidator,
12 videoChannelsGetValidator,
13 videoChannelsRemoveValidator,
14 videoChannelsSortValidator,
15 videoChannelsUpdateValidator
16 } from '../../middlewares'
17 import { VideoChannelModel } from '../../models/video/video-channel'
18 import { videosSortValidator } from '../../middlewares/validators'
19 import { sendUpdateActor } from '../../lib/activitypub/send'
20 import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
21 import { createVideoChannel } from '../../lib/video-channel'
22 import { isNSFWHidden } from '../../helpers/express-utils'
23 import { setAsyncActorKeys } from '../../lib/activitypub'
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 asyncRetryTransactionMiddleware(addVideoChannel)
45 videoChannelRouter.put('/:id',
47 asyncMiddleware(videoChannelsUpdateValidator),
48 asyncRetryTransactionMiddleware(updateVideoChannel)
51 videoChannelRouter.delete('/:id',
53 asyncMiddleware(videoChannelsRemoveValidator),
54 asyncRetryTransactionMiddleware(removeVideoChannel)
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 async function addVideoChannel (req: express.Request, res: express.Response) {
87 const videoChannelInfo: VideoChannelCreate = req.body
88 const account: AccountModel = res.locals.oauth.token.User.Account
90 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
91 return createVideoChannel(videoChannelInfo, account, t)
94 setAsyncActorKeys(videoChannelCreated.Actor)
95 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
97 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
101 id: videoChannelCreated.id,
102 uuid: videoChannelCreated.Actor.uuid
107 async function updateVideoChannel (req: express.Request, res: express.Response) {
108 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
109 const videoChannelFieldsSave = videoChannelInstance.toJSON()
110 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
113 await sequelizeTypescript.transaction(async t => {
114 const sequelizeOptions = {
118 if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.displayName)
119 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
120 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
122 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
123 await sendUpdateActor(videoChannelInstanceUpdated, t)
126 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
128 logger.debug('Cannot update the video channel.', { err })
130 // Force fields we want to update
131 // If the transaction is retried, sequelize will think the object has not changed
132 // So it will skip the SQL request, even if the last one was ROLLBACKed!
133 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
138 return res.type('json').status(204).end()
141 async function removeVideoChannel (req: express.Request, res: express.Response) {
142 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
144 await sequelizeTypescript.transaction(async t => {
145 await videoChannelInstance.destroy({ transaction: t })
147 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
150 return res.type('json').status(204).end()
153 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
154 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
156 return res.json(videoChannelWithVideos.toFormattedJSON())
159 async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
160 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
162 const resultList = await VideoModel.listForApi({
163 start: req.query.start,
164 count: req.query.count,
165 sort: req.query.sort,
166 hideNSFW: isNSFWHidden(res),
168 videoChannelId: videoChannelInstance.id
171 return res.json(getFormattedObjects(resultList.data, resultList.total))