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