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 videoPlaylistsSortValidator
17 } from '../../middlewares'
18 import { VideoChannelModel } from '../../models/video/video-channel'
19 import { videoChannelsNameWithHostValidator, videosSortValidator } from '../../middlewares/validators'
20 import { sendUpdateActor } from '../../lib/activitypub/send'
21 import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
22 import { createLocalVideoChannel, federateAllVideosOfChannel } from '../../lib/video-channel'
23 import { buildNSFWFilter, createReqFiles, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
24 import { setAsyncActorKeys } from '../../lib/activitypub'
25 import { AccountModel } from '../../models/account/account'
26 import { MIMETYPES } from '../../initializers/constants'
27 import { logger } from '../../helpers/logger'
28 import { VideoModel } from '../../models/video/video'
29 import { updateAvatarValidator } from '../../middlewares/validators/avatar'
30 import { updateActorAvatarFile } from '../../lib/avatar'
31 import { auditLoggerFactory, getAuditIdFromRes, VideoChannelAuditView } from '../../helpers/audit-logger'
32 import { resetSequelizeInstance } from '../../helpers/database-utils'
33 import { JobQueue } from '../../lib/job-queue'
34 import { VideoPlaylistModel } from '../../models/video/video-playlist'
35 import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
36 import { CONFIG } from '../../initializers/config'
37 import { sequelizeTypescript } from '../../initializers/database'
38 import { MChannelAccountDefault } from '@server/typings/models'
40 const auditLogger = auditLoggerFactory('channels')
41 const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR })
43 const videoChannelRouter = express.Router()
45 videoChannelRouter.get('/',
47 videoChannelsSortValidator,
50 asyncMiddleware(listVideoChannels)
53 videoChannelRouter.post('/',
55 asyncMiddleware(videoChannelsAddValidator),
56 asyncRetryTransactionMiddleware(addVideoChannel)
59 videoChannelRouter.post('/:nameWithHost/avatar/pick',
63 asyncMiddleware(videoChannelsUpdateValidator),
64 updateAvatarValidator,
65 asyncMiddleware(updateVideoChannelAvatar)
68 videoChannelRouter.put('/:nameWithHost',
70 asyncMiddleware(videoChannelsUpdateValidator),
71 asyncRetryTransactionMiddleware(updateVideoChannel)
74 videoChannelRouter.delete('/:nameWithHost',
76 asyncMiddleware(videoChannelsRemoveValidator),
77 asyncRetryTransactionMiddleware(removeVideoChannel)
80 videoChannelRouter.get('/:nameWithHost',
81 asyncMiddleware(videoChannelsNameWithHostValidator),
82 asyncMiddleware(getVideoChannel)
85 videoChannelRouter.get('/:nameWithHost/video-playlists',
86 asyncMiddleware(videoChannelsNameWithHostValidator),
88 videoPlaylistsSortValidator,
91 commonVideoPlaylistFiltersValidator,
92 asyncMiddleware(listVideoChannelPlaylists)
95 videoChannelRouter.get('/:nameWithHost/videos',
96 asyncMiddleware(videoChannelsNameWithHostValidator),
100 setDefaultPagination,
101 optionalAuthenticate,
102 commonVideosFiltersValidator,
103 asyncMiddleware(listVideoChannelVideos)
106 // ---------------------------------------------------------------------------
112 // ---------------------------------------------------------------------------
114 async function listVideoChannels (req: express.Request, res: express.Response) {
115 const serverActor = await getServerActor()
116 const resultList = await VideoChannelModel.listForApi(serverActor.id, req.query.start, req.query.count, req.query.sort)
118 return res.json(getFormattedObjects(resultList.data, resultList.total))
121 async function updateVideoChannelAvatar (req: express.Request, res: express.Response) {
122 const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ]
123 const videoChannel = res.locals.videoChannel
124 const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON())
126 const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel)
128 auditLogger.update(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannel.toFormattedJSON()), oldVideoChannelAuditKeys)
132 avatar: avatar.toFormattedJSON()
137 async function addVideoChannel (req: express.Request, res: express.Response) {
138 const videoChannelInfo: VideoChannelCreate = req.body
140 const videoChannelCreated = await sequelizeTypescript.transaction(async t => {
141 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
143 return createLocalVideoChannel(videoChannelInfo, account, t)
146 setAsyncActorKeys(videoChannelCreated.Actor)
147 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.url, { err }))
149 auditLogger.create(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelCreated.toFormattedJSON()))
150 logger.info('Video channel %s created.', videoChannelCreated.Actor.url)
154 id: videoChannelCreated.id
159 async function updateVideoChannel (req: express.Request, res: express.Response) {
160 const videoChannelInstance = res.locals.videoChannel
161 const videoChannelFieldsSave = videoChannelInstance.toJSON()
162 const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannelInstance.toFormattedJSON())
163 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
164 let doBulkVideoUpdate = false
167 await sequelizeTypescript.transaction(async t => {
168 const sequelizeOptions = {
172 if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.name = videoChannelInfoToUpdate.displayName
173 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.description = videoChannelInfoToUpdate.description
175 if (videoChannelInfoToUpdate.support !== undefined) {
176 const oldSupportField = videoChannelInstance.support
177 videoChannelInstance.support = videoChannelInfoToUpdate.support
179 if (videoChannelInfoToUpdate.bulkVideosSupportUpdate === true && oldSupportField !== videoChannelInfoToUpdate.support) {
180 doBulkVideoUpdate = true
181 await VideoModel.bulkUpdateSupportField(videoChannelInstance, t)
185 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions) as MChannelAccountDefault
186 await sendUpdateActor(videoChannelInstanceUpdated, t)
189 getAuditIdFromRes(res),
190 new VideoChannelAuditView(videoChannelInstanceUpdated.toFormattedJSON()),
191 oldVideoChannelAuditKeys
194 logger.info('Video channel %s updated.', videoChannelInstance.Actor.url)
197 logger.debug('Cannot update the video channel.', { err })
199 // Force fields we want to update
200 // If the transaction is retried, sequelize will think the object has not changed
201 // So it will skip the SQL request, even if the last one was ROLLBACKed!
202 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
207 res.type('json').status(204).end()
209 // Don't process in a transaction, and after the response because it could be long
210 if (doBulkVideoUpdate) {
211 await federateAllVideosOfChannel(videoChannelInstance)
215 async function removeVideoChannel (req: express.Request, res: express.Response) {
216 const videoChannelInstance = res.locals.videoChannel
218 await sequelizeTypescript.transaction(async t => {
219 await VideoPlaylistModel.resetPlaylistsOfChannel(videoChannelInstance.id, t)
221 await videoChannelInstance.destroy({ transaction: t })
223 auditLogger.delete(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelInstance.toFormattedJSON()))
224 logger.info('Video channel %s deleted.', videoChannelInstance.Actor.url)
227 return res.type('json').status(204).end()
230 async function getVideoChannel (req: express.Request, res: express.Response) {
231 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
233 if (videoChannelWithVideos.isOutdated()) {
234 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: videoChannelWithVideos.Actor.url } })
235 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', videoChannelWithVideos.Actor.url, { err }))
238 return res.json(videoChannelWithVideos.toFormattedJSON())
241 async function listVideoChannelPlaylists (req: express.Request, res: express.Response) {
242 const serverActor = await getServerActor()
244 const resultList = await VideoPlaylistModel.listForApi({
245 followerActorId: serverActor.id,
246 start: req.query.start,
247 count: req.query.count,
248 sort: req.query.sort,
249 videoChannelId: res.locals.videoChannel.id,
250 type: req.query.playlistType
253 return res.json(getFormattedObjects(resultList.data, resultList.total))
256 async function listVideoChannelVideos (req: express.Request, res: express.Response) {
257 const videoChannelInstance = res.locals.videoChannel
258 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
260 const resultList = await VideoModel.listForApi({
262 start: req.query.start,
263 count: req.query.count,
264 sort: req.query.sort,
265 includeLocalVideos: true,
266 categoryOneOf: req.query.categoryOneOf,
267 licenceOneOf: req.query.licenceOneOf,
268 languageOneOf: req.query.languageOneOf,
269 tagsOneOf: req.query.tagsOneOf,
270 tagsAllOf: req.query.tagsAllOf,
271 filter: req.query.filter,
272 nsfw: buildNSFWFilter(res, req.query.nsfw),
274 videoChannelId: videoChannelInstance.id,
275 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
278 return res.json(getFormattedObjects(resultList.data, resultList.total))