]>
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 | videosRemoveValidator, | |
30 | videosSortValidator | |
31 | } from '../../../middlewares' | |
32 | import { VideoModel } from '../../../models/video/video' | |
33 | import { blacklistRouter } from './blacklist' | |
34 | import { videoCaptionsRouter } from './captions' | |
35 | import { videoCommentRouter } from './comment' | |
36 | import { filesRouter } from './files' | |
37 | import { videoImportsRouter } from './import' | |
38 | import { liveRouter } from './live' | |
39 | import { ownershipVideoRouter } from './ownership' | |
40 | import { rateVideoRouter } from './rate' | |
41 | import { statsRouter } from './stats' | |
42 | import { studioRouter } from './studio' | |
43 | import { transcodingRouter } from './transcoding' | |
44 | import { updateRouter } from './update' | |
45 | import { uploadRouter } from './upload' | |
46 | import { viewRouter } from './view' | |
47 | ||
48 | const auditLogger = auditLoggerFactory('videos') | |
49 | const videosRouter = express.Router() | |
50 | ||
51 | videosRouter.use('/', blacklistRouter) | |
52 | videosRouter.use('/', statsRouter) | |
53 | videosRouter.use('/', rateVideoRouter) | |
54 | videosRouter.use('/', videoCommentRouter) | |
55 | videosRouter.use('/', studioRouter) | |
56 | videosRouter.use('/', videoCaptionsRouter) | |
57 | videosRouter.use('/', videoImportsRouter) | |
58 | videosRouter.use('/', ownershipVideoRouter) | |
59 | videosRouter.use('/', viewRouter) | |
60 | videosRouter.use('/', liveRouter) | |
61 | videosRouter.use('/', uploadRouter) | |
62 | videosRouter.use('/', updateRouter) | |
63 | videosRouter.use('/', filesRouter) | |
64 | videosRouter.use('/', transcodingRouter) | |
65 | ||
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 | ) | |
82 | ||
83 | videosRouter.get('/', | |
84 | openapiOperationDoc({ operationId: 'getVideos' }), | |
85 | paginationValidator, | |
86 | videosSortValidator, | |
87 | setDefaultVideosSort, | |
88 | setDefaultPagination, | |
89 | optionalAuthenticate, | |
90 | commonVideosFiltersValidator, | |
91 | asyncMiddleware(listVideos) | |
92 | ) | |
93 | ||
94 | videosRouter.get('/:id/description', | |
95 | openapiOperationDoc({ operationId: 'getVideoDesc' }), | |
96 | asyncMiddleware(videosGetValidator), | |
97 | asyncMiddleware(getVideoDescription) | |
98 | ) | |
99 | videosRouter.get('/:id', | |
100 | openapiOperationDoc({ operationId: 'getVideo' }), | |
101 | optionalAuthenticate, | |
102 | asyncMiddleware(videosCustomGetValidator('for-api')), | |
103 | asyncMiddleware(checkVideoFollowConstraints), | |
104 | getVideo | |
105 | ) | |
106 | ||
107 | videosRouter.delete('/:id', | |
108 | openapiOperationDoc({ operationId: 'delVideo' }), | |
109 | authenticate, | |
110 | asyncMiddleware(videosRemoveValidator), | |
111 | asyncRetryTransactionMiddleware(removeVideo) | |
112 | ) | |
113 | ||
114 | // --------------------------------------------------------------------------- | |
115 | ||
116 | export { | |
117 | videosRouter | |
118 | } | |
119 | ||
120 | // --------------------------------------------------------------------------- | |
121 | ||
122 | function listVideoCategories (_req: express.Request, res: express.Response) { | |
123 | res.json(VIDEO_CATEGORIES) | |
124 | } | |
125 | ||
126 | function listVideoLicences (_req: express.Request, res: express.Response) { | |
127 | res.json(VIDEO_LICENCES) | |
128 | } | |
129 | ||
130 | function listVideoLanguages (_req: express.Request, res: express.Response) { | |
131 | res.json(VIDEO_LANGUAGES) | |
132 | } | |
133 | ||
134 | function listVideoPrivacies (_req: express.Request, res: express.Response) { | |
135 | res.json(VIDEO_PRIVACIES) | |
136 | } | |
137 | ||
138 | function getVideo (_req: express.Request, res: express.Response) { | |
139 | const video = res.locals.videoAPI | |
140 | ||
141 | if (video.isOutdated()) { | |
142 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) | |
143 | } | |
144 | ||
145 | return res.json(video.toFormattedDetailsJSON()) | |
146 | } | |
147 | ||
148 | async function getVideoDescription (req: express.Request, res: express.Response) { | |
149 | const videoInstance = res.locals.videoAll | |
150 | ||
151 | const description = videoInstance.isOwned() | |
152 | ? videoInstance.description | |
153 | : await fetchRemoteVideoDescription(videoInstance) | |
154 | ||
155 | return res.json({ description }) | |
156 | } | |
157 | ||
158 | async function listVideos (req: express.Request, res: express.Response) { | |
159 | const serverActor = await getServerActor() | |
160 | ||
161 | const query = pickCommonVideoQuery(req.query) | |
162 | const countVideos = getCountVideos(req) | |
163 | ||
164 | const apiOptions = await Hooks.wrapObject({ | |
165 | ...query, | |
166 | ||
167 | displayOnlyForFollower: { | |
168 | actorId: serverActor.id, | |
169 | orLocalVideos: true | |
170 | }, | |
171 | nsfw: buildNSFWFilter(res, query.nsfw), | |
172 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, | |
173 | countVideos | |
174 | }, 'filter:api.videos.list.params') | |
175 | ||
176 | const resultList = await Hooks.wrapPromiseFun( | |
177 | VideoModel.listForApi, | |
178 | apiOptions, | |
179 | 'filter:api.videos.list.result' | |
180 | ) | |
181 | ||
182 | return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) | |
183 | } | |
184 | ||
185 | async function removeVideo (req: express.Request, res: express.Response) { | |
186 | const videoInstance = res.locals.videoAll | |
187 | ||
188 | await sequelizeTypescript.transaction(async t => { | |
189 | await videoInstance.destroy({ transaction: t }) | |
190 | }) | |
191 | ||
192 | auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) | |
193 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) | |
194 | ||
195 | Hooks.runAction('action:api.video.deleted', { video: videoInstance, req, res }) | |
196 | ||
197 | return res.type('json') | |
198 | .status(HttpStatusCode.NO_CONTENT_204) | |
199 | .end() | |
200 | } | |
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 | } |