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