]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/index.ts
Add `req` and `res` as controllers hooks parameters
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
CommitLineData
41fb13c3 1import express from 'express'
d6886027 2import { pickCommonVideoQuery } from '@server/helpers/query'
304a84d5 3import { doJSONRequest } from '@server/helpers/requests'
51353d9a 4import { VideoViews } from '@server/lib/video-views'
1c627fd8 5import { openapiOperationDoc } from '@server/middlewares/doc'
8054669f 6import { getServerActor } from '@server/models/application/application'
2760b454 7import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
304a84d5 8import { MVideoAccountLight } from '@server/types/models'
c0e8b12e 9import { HttpStatusCode } from '../../../../shared/models'
8054669f 10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
c158a5fa
C
11import { buildNSFWFilter, getCountVideos } from '../../../helpers/express-utils'
12import { logger } from '../../../helpers/logger'
8dc8a34e 13import { getFormattedObjects } from '../../../helpers/utils'
304a84d5 14import { REMOTE_SCHEME, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers/constants'
8054669f
C
15import { sequelizeTypescript } from '../../../initializers/database'
16import { sendView } from '../../../lib/activitypub/send/send-view'
94a5ff8a 17import { JobQueue } from '../../../lib/job-queue'
8054669f 18import { Hooks } from '../../../lib/plugins/hooks'
65fcc311 19import {
ac81d1a0 20 asyncMiddleware,
90d4bb81 21 asyncRetryTransactionMiddleware,
ac81d1a0 22 authenticate,
8d427346 23 checkVideoFollowConstraints,
d525fc39 24 commonVideosFiltersValidator,
0883b324 25 optionalAuthenticate,
ac81d1a0
C
26 paginationValidator,
27 setDefaultPagination,
8054669f 28 setDefaultVideosSort,
09209296 29 videosCustomGetValidator,
ac81d1a0
C
30 videosGetValidator,
31 videosRemoveValidator,
c158a5fa 32 videosSortValidator
65fcc311 33} from '../../../middlewares'
3fd3ab2d 34import { VideoModel } from '../../../models/video/video'
65fcc311 35import { blacklistRouter } from './blacklist'
40e87e9e 36import { videoCaptionsRouter } from './captions'
8054669f 37import { videoCommentRouter } from './comment'
b46cf4b9 38import { filesRouter } from './files'
fbad87b0 39import { videoImportsRouter } from './import'
c6c0fa6c 40import { liveRouter } from './live'
8054669f
C
41import { ownershipVideoRouter } from './ownership'
42import { rateVideoRouter } from './rate'
ad5db104 43import { transcodingRouter } from './transcoding'
c158a5fa
C
44import { updateRouter } from './update'
45import { uploadRouter } from './upload'
6e46de09 46import { watchingRouter } from './watching'
65fcc311 47
80e36cd9 48const auditLogger = auditLoggerFactory('videos')
65fcc311 49const videosRouter = express.Router()
8c308c2b 50
65fcc311
C
51videosRouter.use('/', blacklistRouter)
52videosRouter.use('/', rateVideoRouter)
bf1f6508 53videosRouter.use('/', videoCommentRouter)
40e87e9e 54videosRouter.use('/', videoCaptionsRouter)
fbad87b0 55videosRouter.use('/', videoImportsRouter)
74d63469 56videosRouter.use('/', ownershipVideoRouter)
6e46de09 57videosRouter.use('/', watchingRouter)
c6c0fa6c 58videosRouter.use('/', liveRouter)
c158a5fa
C
59videosRouter.use('/', uploadRouter)
60videosRouter.use('/', updateRouter)
b46cf4b9 61videosRouter.use('/', filesRouter)
ad5db104 62videosRouter.use('/', transcodingRouter)
d33242b0 63
c756bae0
RK
64videosRouter.get('/categories',
65 openapiOperationDoc({ operationId: 'getCategories' }),
66 listVideoCategories
67)
68videosRouter.get('/licences',
69 openapiOperationDoc({ operationId: 'getLicences' }),
70 listVideoLicences
71)
72videosRouter.get('/languages',
73 openapiOperationDoc({ operationId: 'getLanguages' }),
74 listVideoLanguages
75)
76videosRouter.get('/privacies',
77 openapiOperationDoc({ operationId: 'getPrivacies' }),
78 listVideoPrivacies
79)
6e07c3de 80
65fcc311 81videosRouter.get('/',
c756bae0 82 openapiOperationDoc({ operationId: 'getVideos' }),
65fcc311
C
83 paginationValidator,
84 videosSortValidator,
8054669f 85 setDefaultVideosSort,
f05a1c30 86 setDefaultPagination,
0883b324 87 optionalAuthenticate,
d525fc39 88 commonVideosFiltersValidator,
eb080476 89 asyncMiddleware(listVideos)
fbf1134e 90)
f6d6e7f8 91
9567011b 92videosRouter.get('/:id/description',
c756bae0 93 openapiOperationDoc({ operationId: 'getVideoDesc' }),
a2431b7d 94 asyncMiddleware(videosGetValidator),
9567011b
C
95 asyncMiddleware(getVideoDescription)
96)
65fcc311 97videosRouter.get('/:id',
1c627fd8 98 openapiOperationDoc({ operationId: 'getVideo' }),
6e46de09 99 optionalAuthenticate,
ca4b4b2e 100 asyncMiddleware(videosCustomGetValidator('for-api')),
8d427346 101 asyncMiddleware(checkVideoFollowConstraints),
98ab5dc8 102 getVideo
fbf1134e 103)
1f3e9fec 104videosRouter.post('/:id/views',
c756bae0 105 openapiOperationDoc({ operationId: 'addView' }),
51353d9a 106 asyncMiddleware(videosCustomGetValidator('only-video')),
1f3e9fec
C
107 asyncMiddleware(viewVideo)
108)
198b205c 109
65fcc311 110videosRouter.delete('/:id',
1c627fd8 111 openapiOperationDoc({ operationId: 'delVideo' }),
65fcc311 112 authenticate,
a2431b7d 113 asyncMiddleware(videosRemoveValidator),
90d4bb81 114 asyncRetryTransactionMiddleware(removeVideo)
fbf1134e 115)
198b205c 116
9f10b292 117// ---------------------------------------------------------------------------
c45f7f84 118
65fcc311
C
119export {
120 videosRouter
121}
c45f7f84 122
9f10b292 123// ---------------------------------------------------------------------------
c45f7f84 124
f6d6e7f8 125function listVideoCategories (_req: express.Request, res: express.Response) {
65fcc311 126 res.json(VIDEO_CATEGORIES)
6e07c3de
C
127}
128
f6d6e7f8 129function listVideoLicences (_req: express.Request, res: express.Response) {
65fcc311 130 res.json(VIDEO_LICENCES)
6f0c39e2
C
131}
132
f6d6e7f8 133function listVideoLanguages (_req: express.Request, res: express.Response) {
65fcc311 134 res.json(VIDEO_LANGUAGES)
3092476e
C
135}
136
f6d6e7f8 137function listVideoPrivacies (_req: express.Request, res: express.Response) {
fd45e8f4
C
138 res.json(VIDEO_PRIVACIES)
139}
140
98ab5dc8 141function getVideo (_req: express.Request, res: express.Response) {
ca4b4b2e 142 const video = res.locals.videoAPI
1f3e9fec 143
09209296
C
144 if (video.isOutdated()) {
145 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } })
04b8c3fb
C
146 }
147
09209296 148 return res.json(video.toFormattedDetailsJSON())
1f3e9fec
C
149}
150
151async function viewVideo (req: express.Request, res: express.Response) {
51353d9a 152 const video = res.locals.onlyVideo
9e167724 153
490b595a 154 const ip = req.ip
51353d9a 155 const success = await VideoViews.Instance.processView({ video, ip })
e4bf7856 156
51353d9a 157 if (success) {
e4bf7856 158 const serverActor = await getServerActor()
51353d9a 159 await sendView(serverActor, video, undefined)
e4bf7856 160
7226e90f 161 Hooks.runAction('action:api.video.viewed', { video: video, ip, req, res })
51353d9a 162 }
e4bf7856 163
76148b27 164 return res.status(HttpStatusCode.NO_CONTENT_204).end()
9f10b292 165}
8c308c2b 166
9567011b 167async function getVideoDescription (req: express.Request, res: express.Response) {
453e83ea 168 const videoInstance = res.locals.videoAll
9567011b 169
c158a5fa
C
170 const description = videoInstance.isOwned()
171 ? videoInstance.description
172 : await fetchRemoteVideoDescription(videoInstance)
9567011b
C
173
174 return res.json({ description })
175}
176
04b8c3fb 177async function listVideos (req: express.Request, res: express.Response) {
2760b454
C
178 const serverActor = await getServerActor()
179
d6886027 180 const query = pickCommonVideoQuery(req.query)
fe987656
C
181 const countVideos = getCountVideos(req)
182
b4055e1c 183 const apiOptions = await Hooks.wrapObject({
d6886027
C
184 ...query,
185
2760b454
C
186 displayOnlyForFollower: {
187 actorId: serverActor.id,
188 orLocalVideos: true
189 },
1fd61899 190 nsfw: buildNSFWFilter(res, query.nsfw),
fe987656
C
191 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
192 countVideos
b4055e1c
C
193 }, 'filter:api.videos.list.params')
194
89cd1275
C
195 const resultList = await Hooks.wrapPromiseFun(
196 VideoModel.listForApi,
197 apiOptions,
b4055e1c
C
198 'filter:api.videos.list.result'
199 )
eb080476 200
2760b454 201 return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
9f10b292 202}
c45f7f84 203
7226e90f 204async function removeVideo (req: express.Request, res: express.Response) {
453e83ea 205 const videoInstance = res.locals.videoAll
91f6f169 206
3fd3ab2d 207 await sequelizeTypescript.transaction(async t => {
eb080476 208 await videoInstance.destroy({ transaction: t })
91f6f169 209 })
eb080476 210
993cef4b 211 auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON()))
eb080476 212 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
90d4bb81 213
7226e90f 214 Hooks.runAction('action:api.video.deleted', { video: videoInstance, req, res })
b4055e1c 215
2d53be02
RK
216 return res.type('json')
217 .status(HttpStatusCode.NO_CONTENT_204)
218 .end()
9f10b292 219}
304a84d5
C
220
221// ---------------------------------------------------------------------------
222
223// FIXME: Should not exist, we rely on specific API
224async function fetchRemoteVideoDescription (video: MVideoAccountLight) {
225 const host = video.VideoChannel.Account.Actor.Server.host
226 const path = video.getDescriptionAPIPath()
227 const url = REMOTE_SCHEME.HTTP + '://' + host + path
228
229 const { body } = await doJSONRequest<any>(url)
230 return body.description || ''
231}