1 import * as express from 'express'
2 import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
5 asyncRetryTransactionMiddleware,
6 authenticate, commonVideosFiltersValidator,
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 { createReqFiles, buildNSFWFilter } from '../../helpers/express-utils'
23 import { setAsyncActorKeys } from '../../lib/activitypub'
24 import { AccountModel } from '../../models/account/account'
25 import { CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../initializers'
26 import { logger } from '../../helpers/logger'
27 import { VideoModel } from '../../models/video/video'
28 import { updateAvatarValidator } from '../../middlewares/validators/avatar'
29 import { updateActorAvatarFile } from '../../lib/avatar'
31 const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR })
33 const videoChannelRouter = express.Router()
35 videoChannelRouter.get('/',
37 videoChannelsSortValidator,
40 asyncMiddleware(listVideoChannels)
43 videoChannelRouter.post('/',
45 videoChannelsAddValidator,
46 asyncRetryTransactionMiddleware(addVideoChannel)
49 videoChannelRouter.post('/:id/avatar/pick',
53 asyncMiddleware(videoChannelsUpdateValidator),
54 updateAvatarValidator,
55 asyncMiddleware(updateVideoChannelAvatar)
58 videoChannelRouter.put('/:id',
60 asyncMiddleware(videoChannelsUpdateValidator),
61 asyncRetryTransactionMiddleware(updateVideoChannel)
64 videoChannelRouter.delete('/:id',
66 asyncMiddleware(videoChannelsRemoveValidator),
67 asyncRetryTransactionMiddleware(removeVideoChannel)
70 videoChannelRouter.get('/:id',
71 asyncMiddleware(videoChannelsGetValidator),
72 asyncMiddleware(getVideoChannel)
75 videoChannelRouter.get('/:id/videos',
76 asyncMiddleware(videoChannelsGetValidator),
82 commonVideosFiltersValidator,
83 asyncMiddleware(listVideoChannelVideos)
86 // ---------------------------------------------------------------------------
92 // ---------------------------------------------------------------------------
94 async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
95 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
97 return res.json(getFormattedObjects(resultList.data, resultList.total))
100 async function updateVideoChannelAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
101 const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ]
102 const videoChannel = res.locals.videoChannel
104 const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel.Actor, videoChannel)
108 avatar: avatar.toFormattedJSON()
113 async function addVideoChannel (req: express.Request, res: express.Response) {
114 const videoChannelInfo: VideoChannelCreate = req.body
115 const account: AccountModel = res.locals.oauth.token.User.Account
117 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
118 return createVideoChannel(videoChannelInfo, account, t)
121 setAsyncActorKeys(videoChannelCreated.Actor)
122 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
124 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
128 id: videoChannelCreated.id,
129 uuid: videoChannelCreated.Actor.uuid
134 async function updateVideoChannel (req: express.Request, res: express.Response) {
135 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
136 const videoChannelFieldsSave = videoChannelInstance.toJSON()
137 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
140 await sequelizeTypescript.transaction(async t => {
141 const sequelizeOptions = {
145 if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.displayName)
146 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
147 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
149 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
150 await sendUpdateActor(videoChannelInstanceUpdated, t)
153 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
155 logger.debug('Cannot update the video channel.', { err })
157 // Force fields we want to update
158 // If the transaction is retried, sequelize will think the object has not changed
159 // So it will skip the SQL request, even if the last one was ROLLBACKed!
160 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
165 return res.type('json').status(204).end()
168 async function removeVideoChannel (req: express.Request, res: express.Response) {
169 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
171 await sequelizeTypescript.transaction(async t => {
172 await videoChannelInstance.destroy({ transaction: t })
174 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
177 return res.type('json').status(204).end()
180 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
181 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
183 return res.json(videoChannelWithVideos.toFormattedJSON())
186 async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
187 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
189 const resultList = await VideoModel.listForApi({
190 start: req.query.start,
191 count: req.query.count,
192 sort: req.query.sort,
193 categoryOneOf: req.query.categoryOneOf,
194 licenceOneOf: req.query.licenceOneOf,
195 languageOneOf: req.query.languageOneOf,
196 tagsOneOf: req.query.tagsOneOf,
197 tagsAllOf: req.query.tagsAllOf,
198 nsfw: buildNSFWFilter(res, req.query.nsfw),
200 videoChannelId: videoChannelInstance.id
203 return res.json(getFormattedObjects(resultList.data, resultList.total))