]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
8054669f | 2 | import toInt from 'validator/lib/toInt' |
d6886027 | 3 | import { pickCommonVideoQuery } from '@server/helpers/query' |
304a84d5 | 4 | import { doJSONRequest } from '@server/helpers/requests' |
8ebf2a5d | 5 | import { LiveManager } from '@server/lib/live' |
1c627fd8 | 6 | import { openapiOperationDoc } from '@server/middlewares/doc' |
8054669f | 7 | import { getServerActor } from '@server/models/application/application' |
2760b454 | 8 | import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils' |
304a84d5 | 9 | import { MVideoAccountLight } from '@server/types/models' |
c0e8b12e | 10 | import { HttpStatusCode } from '../../../../shared/models' |
8054669f | 11 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
c158a5fa C |
12 | import { buildNSFWFilter, getCountVideos } from '../../../helpers/express-utils' |
13 | import { logger } from '../../../helpers/logger' | |
8dc8a34e | 14 | import { getFormattedObjects } from '../../../helpers/utils' |
304a84d5 | 15 | import { REMOTE_SCHEME, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers/constants' |
8054669f C |
16 | import { sequelizeTypescript } from '../../../initializers/database' |
17 | import { sendView } from '../../../lib/activitypub/send/send-view' | |
94a5ff8a | 18 | import { JobQueue } from '../../../lib/job-queue' |
8054669f | 19 | import { Hooks } from '../../../lib/plugins/hooks' |
b5c0e955 | 20 | import { Redis } from '../../../lib/redis' |
65fcc311 | 21 | import { |
ac81d1a0 | 22 | asyncMiddleware, |
90d4bb81 | 23 | asyncRetryTransactionMiddleware, |
ac81d1a0 | 24 | authenticate, |
8d427346 | 25 | checkVideoFollowConstraints, |
d525fc39 | 26 | commonVideosFiltersValidator, |
0883b324 | 27 | optionalAuthenticate, |
ac81d1a0 C |
28 | paginationValidator, |
29 | setDefaultPagination, | |
8054669f | 30 | setDefaultVideosSort, |
d57d1d83 | 31 | videoFileMetadataGetValidator, |
09209296 | 32 | videosCustomGetValidator, |
ac81d1a0 C |
33 | videosGetValidator, |
34 | videosRemoveValidator, | |
c158a5fa | 35 | videosSortValidator |
65fcc311 | 36 | } from '../../../middlewares' |
3fd3ab2d C |
37 | import { VideoModel } from '../../../models/video/video' |
38 | import { VideoFileModel } from '../../../models/video/video-file' | |
65fcc311 | 39 | import { blacklistRouter } from './blacklist' |
40e87e9e | 40 | import { videoCaptionsRouter } from './captions' |
8054669f | 41 | import { videoCommentRouter } from './comment' |
fbad87b0 | 42 | import { videoImportsRouter } from './import' |
c6c0fa6c | 43 | import { liveRouter } from './live' |
8054669f C |
44 | import { ownershipVideoRouter } from './ownership' |
45 | import { rateVideoRouter } from './rate' | |
c158a5fa C |
46 | import { updateRouter } from './update' |
47 | import { uploadRouter } from './upload' | |
6e46de09 | 48 | import { watchingRouter } from './watching' |
65fcc311 | 49 | |
80e36cd9 | 50 | const auditLogger = auditLoggerFactory('videos') |
65fcc311 | 51 | const videosRouter = express.Router() |
8c308c2b | 52 | |
65fcc311 C |
53 | videosRouter.use('/', blacklistRouter) |
54 | videosRouter.use('/', rateVideoRouter) | |
bf1f6508 | 55 | videosRouter.use('/', videoCommentRouter) |
40e87e9e | 56 | videosRouter.use('/', videoCaptionsRouter) |
fbad87b0 | 57 | videosRouter.use('/', videoImportsRouter) |
74d63469 | 58 | videosRouter.use('/', ownershipVideoRouter) |
6e46de09 | 59 | videosRouter.use('/', watchingRouter) |
c6c0fa6c | 60 | videosRouter.use('/', liveRouter) |
c158a5fa C |
61 | videosRouter.use('/', uploadRouter) |
62 | videosRouter.use('/', updateRouter) | |
d33242b0 | 63 | |
c756bae0 RK |
64 | videosRouter.get('/categories', |
65 | openapiOperationDoc({ operationId: 'getCategories' }), | |
66 | listVideoCategories | |
67 | ) | |
68 | videosRouter.get('/licences', | |
69 | openapiOperationDoc({ operationId: 'getLicences' }), | |
70 | listVideoLicences | |
71 | ) | |
72 | videosRouter.get('/languages', | |
73 | openapiOperationDoc({ operationId: 'getLanguages' }), | |
74 | listVideoLanguages | |
75 | ) | |
76 | videosRouter.get('/privacies', | |
77 | openapiOperationDoc({ operationId: 'getPrivacies' }), | |
78 | listVideoPrivacies | |
79 | ) | |
6e07c3de | 80 | |
65fcc311 | 81 | videosRouter.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 | 92 | videosRouter.get('/:id/description', |
c756bae0 | 93 | openapiOperationDoc({ operationId: 'getVideoDesc' }), |
a2431b7d | 94 | asyncMiddleware(videosGetValidator), |
9567011b C |
95 | asyncMiddleware(getVideoDescription) |
96 | ) | |
8319d6ae RK |
97 | videosRouter.get('/:id/metadata/:videoFileId', |
98 | asyncMiddleware(videoFileMetadataGetValidator), | |
99 | asyncMiddleware(getVideoFileMetadata) | |
100 | ) | |
65fcc311 | 101 | videosRouter.get('/:id', |
1c627fd8 | 102 | openapiOperationDoc({ operationId: 'getVideo' }), |
6e46de09 | 103 | optionalAuthenticate, |
ca4b4b2e | 104 | asyncMiddleware(videosCustomGetValidator('for-api')), |
8d427346 | 105 | asyncMiddleware(checkVideoFollowConstraints), |
98ab5dc8 | 106 | getVideo |
fbf1134e | 107 | ) |
1f3e9fec | 108 | videosRouter.post('/:id/views', |
c756bae0 | 109 | openapiOperationDoc({ operationId: 'addView' }), |
2c8776fc | 110 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), |
1f3e9fec C |
111 | asyncMiddleware(viewVideo) |
112 | ) | |
198b205c | 113 | |
65fcc311 | 114 | videosRouter.delete('/:id', |
1c627fd8 | 115 | openapiOperationDoc({ operationId: 'delVideo' }), |
65fcc311 | 116 | authenticate, |
a2431b7d | 117 | asyncMiddleware(videosRemoveValidator), |
90d4bb81 | 118 | asyncRetryTransactionMiddleware(removeVideo) |
fbf1134e | 119 | ) |
198b205c | 120 | |
9f10b292 | 121 | // --------------------------------------------------------------------------- |
c45f7f84 | 122 | |
65fcc311 C |
123 | export { |
124 | videosRouter | |
125 | } | |
c45f7f84 | 126 | |
9f10b292 | 127 | // --------------------------------------------------------------------------- |
c45f7f84 | 128 | |
f6d6e7f8 | 129 | function listVideoCategories (_req: express.Request, res: express.Response) { |
65fcc311 | 130 | res.json(VIDEO_CATEGORIES) |
6e07c3de C |
131 | } |
132 | ||
f6d6e7f8 | 133 | function listVideoLicences (_req: express.Request, res: express.Response) { |
65fcc311 | 134 | res.json(VIDEO_LICENCES) |
6f0c39e2 C |
135 | } |
136 | ||
f6d6e7f8 | 137 | function listVideoLanguages (_req: express.Request, res: express.Response) { |
65fcc311 | 138 | res.json(VIDEO_LANGUAGES) |
3092476e C |
139 | } |
140 | ||
f6d6e7f8 | 141 | function listVideoPrivacies (_req: express.Request, res: express.Response) { |
fd45e8f4 C |
142 | res.json(VIDEO_PRIVACIES) |
143 | } | |
144 | ||
98ab5dc8 | 145 | function getVideo (_req: express.Request, res: express.Response) { |
ca4b4b2e | 146 | const video = res.locals.videoAPI |
1f3e9fec | 147 | |
09209296 C |
148 | if (video.isOutdated()) { |
149 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) | |
04b8c3fb C |
150 | } |
151 | ||
09209296 | 152 | return res.json(video.toFormattedDetailsJSON()) |
1f3e9fec C |
153 | } |
154 | ||
155 | async function viewVideo (req: express.Request, res: express.Response) { | |
e4bf7856 | 156 | const immutableVideoAttrs = res.locals.onlyImmutableVideo |
9e167724 | 157 | |
490b595a | 158 | const ip = req.ip |
e4bf7856 | 159 | const exists = await Redis.Instance.doesVideoIPViewExist(ip, immutableVideoAttrs.uuid) |
b5c0e955 | 160 | if (exists) { |
e4bf7856 | 161 | logger.debug('View for ip %s and video %s already exists.', ip, immutableVideoAttrs.uuid) |
76148b27 | 162 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
b5c0e955 C |
163 | } |
164 | ||
e4bf7856 | 165 | const video = await VideoModel.load(immutableVideoAttrs.id) |
b5c0e955 | 166 | |
e4bf7856 C |
167 | const promises: Promise<any>[] = [ |
168 | Redis.Instance.setIPVideoView(ip, video.uuid, video.isLive) | |
169 | ] | |
9e167724 | 170 | |
e4bf7856 | 171 | let federateView = true |
b4055e1c | 172 | |
e4bf7856 C |
173 | // Increment our live manager |
174 | if (video.isLive && video.isOwned()) { | |
175 | LiveManager.Instance.addViewTo(video.id) | |
176 | ||
177 | // Views of our local live will be sent by our live manager | |
178 | federateView = false | |
179 | } | |
180 | ||
181 | // Increment our video views cache counter | |
182 | if (!video.isLive) { | |
183 | promises.push(Redis.Instance.addVideoView(video.id)) | |
184 | } | |
185 | ||
186 | if (federateView) { | |
187 | const serverActor = await getServerActor() | |
188 | promises.push(sendView(serverActor, video, undefined)) | |
189 | } | |
190 | ||
191 | await Promise.all(promises) | |
192 | ||
193 | Hooks.runAction('action:api.video.viewed', { video, ip }) | |
194 | ||
76148b27 | 195 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
9f10b292 | 196 | } |
8c308c2b | 197 | |
9567011b | 198 | async function getVideoDescription (req: express.Request, res: express.Response) { |
453e83ea | 199 | const videoInstance = res.locals.videoAll |
9567011b | 200 | |
c158a5fa C |
201 | const description = videoInstance.isOwned() |
202 | ? videoInstance.description | |
203 | : await fetchRemoteVideoDescription(videoInstance) | |
9567011b C |
204 | |
205 | return res.json({ description }) | |
206 | } | |
207 | ||
8319d6ae RK |
208 | async function getVideoFileMetadata (req: express.Request, res: express.Response) { |
209 | const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId)) | |
583eb04b | 210 | |
8319d6ae RK |
211 | return res.json(videoFile.metadata) |
212 | } | |
213 | ||
04b8c3fb | 214 | async function listVideos (req: express.Request, res: express.Response) { |
2760b454 C |
215 | const serverActor = await getServerActor() |
216 | ||
d6886027 | 217 | const query = pickCommonVideoQuery(req.query) |
fe987656 C |
218 | const countVideos = getCountVideos(req) |
219 | ||
b4055e1c | 220 | const apiOptions = await Hooks.wrapObject({ |
d6886027 C |
221 | ...query, |
222 | ||
2760b454 C |
223 | displayOnlyForFollower: { |
224 | actorId: serverActor.id, | |
225 | orLocalVideos: true | |
226 | }, | |
1fd61899 | 227 | nsfw: buildNSFWFilter(res, query.nsfw), |
fe987656 C |
228 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, |
229 | countVideos | |
b4055e1c C |
230 | }, 'filter:api.videos.list.params') |
231 | ||
89cd1275 C |
232 | const resultList = await Hooks.wrapPromiseFun( |
233 | VideoModel.listForApi, | |
234 | apiOptions, | |
b4055e1c C |
235 | 'filter:api.videos.list.result' |
236 | ) | |
eb080476 | 237 | |
2760b454 | 238 | return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) |
9f10b292 | 239 | } |
c45f7f84 | 240 | |
c158a5fa | 241 | async function removeVideo (_req: express.Request, res: express.Response) { |
453e83ea | 242 | const videoInstance = res.locals.videoAll |
91f6f169 | 243 | |
3fd3ab2d | 244 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 245 | await videoInstance.destroy({ transaction: t }) |
91f6f169 | 246 | }) |
eb080476 | 247 | |
993cef4b | 248 | auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) |
eb080476 | 249 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) |
90d4bb81 | 250 | |
b4055e1c C |
251 | Hooks.runAction('action:api.video.deleted', { video: videoInstance }) |
252 | ||
2d53be02 RK |
253 | return res.type('json') |
254 | .status(HttpStatusCode.NO_CONTENT_204) | |
255 | .end() | |
9f10b292 | 256 | } |
304a84d5 C |
257 | |
258 | // --------------------------------------------------------------------------- | |
259 | ||
260 | // FIXME: Should not exist, we rely on specific API | |
261 | async function fetchRemoteVideoDescription (video: MVideoAccountLight) { | |
262 | const host = video.VideoChannel.Account.Actor.Server.host | |
263 | const path = video.getDescriptionAPIPath() | |
264 | const url = REMOTE_SCHEME.HTTP + '://' + host + path | |
265 | ||
266 | const { body } = await doJSONRequest<any>(url) | |
267 | return body.description || '' | |
268 | } |