1 import * as express from 'express'
2 import { getFormattedObjects, getServerActor } from '../../helpers/utils'
5 asyncRetryTransactionMiddleware,
7 commonVideosFiltersValidator,
12 videoChannelsAddValidator,
13 videoChannelsRemoveValidator,
14 videoChannelsSortValidator,
15 videoChannelsUpdateValidator
16 } from '../../middlewares'
17 import { VideoChannelModel } from '../../models/video/video-channel'
18 import { videoChannelsNameWithHostValidator, 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 { buildNSFWFilter, createReqFiles, isUserAbleToSearchRemoteURI } 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'
30 import { auditLoggerFactory, VideoChannelAuditView } from '../../helpers/audit-logger'
31 import { resetSequelizeInstance } from '../../helpers/database-utils'
33 const auditLogger = auditLoggerFactory('channels')
34 const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR })
36 const videoChannelRouter = express.Router()
38 videoChannelRouter.get('/',
40 videoChannelsSortValidator,
43 asyncMiddleware(listVideoChannels)
46 videoChannelRouter.post('/',
48 videoChannelsAddValidator,
49 asyncRetryTransactionMiddleware(addVideoChannel)
52 videoChannelRouter.post('/:nameWithHost/avatar/pick',
56 asyncMiddleware(videoChannelsUpdateValidator),
57 updateAvatarValidator,
58 asyncMiddleware(updateVideoChannelAvatar)
61 videoChannelRouter.put('/:nameWithHost',
63 asyncMiddleware(videoChannelsUpdateValidator),
64 asyncRetryTransactionMiddleware(updateVideoChannel)
67 videoChannelRouter.delete('/:nameWithHost',
69 asyncMiddleware(videoChannelsRemoveValidator),
70 asyncRetryTransactionMiddleware(removeVideoChannel)
73 videoChannelRouter.get('/:nameWithHost',
74 asyncMiddleware(videoChannelsNameWithHostValidator),
75 asyncMiddleware(getVideoChannel)
78 videoChannelRouter.get('/:nameWithHost/videos',
79 asyncMiddleware(videoChannelsNameWithHostValidator),
85 commonVideosFiltersValidator,
86 asyncMiddleware(listVideoChannelVideos)
89 // ---------------------------------------------------------------------------
95 // ---------------------------------------------------------------------------
97 async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
98 const serverActor = await getServerActor()
99 const resultList = await VideoChannelModel.listForApi(serverActor.id, req.query.start, req.query.count, req.query.sort)
101 return res.json(getFormattedObjects(resultList.data, resultList.total))
104 async function updateVideoChannelAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
105 const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ]
106 const videoChannel = res.locals.videoChannel as VideoChannelModel
107 const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON())
109 const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel.Actor, videoChannel)
112 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
113 new VideoChannelAuditView(videoChannel.toFormattedJSON()),
114 oldVideoChannelAuditKeys
119 avatar: avatar.toFormattedJSON()
124 async function addVideoChannel (req: express.Request, res: express.Response) {
125 const videoChannelInfo: VideoChannelCreate = req.body
126 const account: AccountModel = res.locals.oauth.token.User.Account
128 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
129 return createVideoChannel(videoChannelInfo, account, t)
132 setAsyncActorKeys(videoChannelCreated.Actor)
133 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
136 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
137 new VideoChannelAuditView(videoChannelCreated.toFormattedJSON())
139 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
143 id: videoChannelCreated.id,
144 uuid: videoChannelCreated.Actor.uuid
149 async function updateVideoChannel (req: express.Request, res: express.Response) {
150 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
151 const videoChannelFieldsSave = videoChannelInstance.toJSON()
152 const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannelInstance.toFormattedJSON())
153 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
156 await sequelizeTypescript.transaction(async t => {
157 const sequelizeOptions = {
161 if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.displayName)
162 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
163 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
165 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
166 await sendUpdateActor(videoChannelInstanceUpdated, t)
169 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
170 new VideoChannelAuditView(videoChannelInstanceUpdated.toFormattedJSON()),
171 oldVideoChannelAuditKeys
173 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
176 logger.debug('Cannot update the video channel.', { err })
178 // Force fields we want to update
179 // If the transaction is retried, sequelize will think the object has not changed
180 // So it will skip the SQL request, even if the last one was ROLLBACKed!
181 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
186 return res.type('json').status(204).end()
189 async function removeVideoChannel (req: express.Request, res: express.Response) {
190 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
192 await sequelizeTypescript.transaction(async t => {
193 await videoChannelInstance.destroy({ transaction: t })
196 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
197 new VideoChannelAuditView(videoChannelInstance.toFormattedJSON())
199 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
202 return res.type('json').status(204).end()
205 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
206 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
208 return res.json(videoChannelWithVideos.toFormattedJSON())
211 async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
212 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
213 const actorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
215 const resultList = await VideoModel.listForApi({
217 start: req.query.start,
218 count: req.query.count,
219 sort: req.query.sort,
220 includeLocalVideos: true,
221 categoryOneOf: req.query.categoryOneOf,
222 licenceOneOf: req.query.licenceOneOf,
223 languageOneOf: req.query.languageOneOf,
224 tagsOneOf: req.query.tagsOneOf,
225 tagsAllOf: req.query.tagsAllOf,
226 nsfw: buildNSFWFilter(res, req.query.nsfw),
228 videoChannelId: videoChannelInstance.id
231 return res.json(getFormattedObjects(resultList.data, resultList.total))