]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { pickCommonVideoQuery } from '@server/helpers/query' | |
3 | import { Hooks } from '@server/lib/plugins/hooks' | |
4 | import { ActorFollowModel } from '@server/models/actor/actor-follow' | |
5 | import { getServerActor } from '@server/models/application/application' | |
6 | import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils' | |
7 | import { MChannelBannerAccountDefault } from '@server/types/models' | |
8 | import { ActorImageType, HttpStatusCode, VideoChannelCreate, VideoChannelUpdate } from '@shared/models' | |
9 | import { auditLoggerFactory, getAuditIdFromRes, VideoChannelAuditView } from '../../helpers/audit-logger' | |
10 | import { resetSequelizeInstance } from '../../helpers/database-utils' | |
11 | import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' | |
12 | import { logger } from '../../helpers/logger' | |
13 | import { getFormattedObjects } from '../../helpers/utils' | |
14 | import { CONFIG } from '../../initializers/config' | |
15 | import { MIMETYPES } from '../../initializers/constants' | |
16 | import { sequelizeTypescript } from '../../initializers/database' | |
17 | import { sendUpdateActor } from '../../lib/activitypub/send' | |
18 | import { JobQueue } from '../../lib/job-queue' | |
19 | import { deleteLocalActorImageFile, updateLocalActorImageFile } from '../../lib/local-actor' | |
20 | import { createLocalVideoChannel, federateAllVideosOfChannel } from '../../lib/video-channel' | |
21 | import { | |
22 | asyncMiddleware, | |
23 | asyncRetryTransactionMiddleware, | |
24 | authenticate, | |
25 | commonVideosFiltersValidator, | |
26 | ensureCanManageChannel, | |
27 | optionalAuthenticate, | |
28 | paginationValidator, | |
29 | setDefaultPagination, | |
30 | setDefaultSort, | |
31 | setDefaultVideosSort, | |
32 | videoChannelsAddValidator, | |
33 | videoChannelsRemoveValidator, | |
34 | videoChannelsSortValidator, | |
35 | videoChannelsUpdateValidator, | |
36 | videoPlaylistsSortValidator | |
37 | } from '../../middlewares' | |
38 | import { | |
39 | ensureIsLocalChannel, | |
40 | videoChannelsFollowersSortValidator, | |
41 | videoChannelsListValidator, | |
42 | videoChannelsNameWithHostValidator, | |
43 | videosSortValidator | |
44 | } from '../../middlewares/validators' | |
45 | import { updateAvatarValidator, updateBannerValidator } from '../../middlewares/validators/actor-image' | |
46 | import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists' | |
47 | import { AccountModel } from '../../models/account/account' | |
48 | import { VideoModel } from '../../models/video/video' | |
49 | import { VideoChannelModel } from '../../models/video/video-channel' | |
50 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | |
51 | ||
52 | const auditLogger = auditLoggerFactory('channels') | |
53 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) | |
54 | const reqBannerFile = createReqFiles([ 'bannerfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { bannerfile: CONFIG.STORAGE.TMP_DIR }) | |
55 | ||
56 | const videoChannelRouter = express.Router() | |
57 | ||
58 | videoChannelRouter.get('/', | |
59 | paginationValidator, | |
60 | videoChannelsSortValidator, | |
61 | setDefaultSort, | |
62 | setDefaultPagination, | |
63 | videoChannelsListValidator, | |
64 | asyncMiddleware(listVideoChannels) | |
65 | ) | |
66 | ||
67 | videoChannelRouter.post('/', | |
68 | authenticate, | |
69 | asyncMiddleware(videoChannelsAddValidator), | |
70 | asyncRetryTransactionMiddleware(addVideoChannel) | |
71 | ) | |
72 | ||
73 | videoChannelRouter.post('/:nameWithHost/avatar/pick', | |
74 | authenticate, | |
75 | reqAvatarFile, | |
76 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
77 | ensureIsLocalChannel, | |
78 | ensureCanManageChannel, | |
79 | updateAvatarValidator, | |
80 | asyncMiddleware(updateVideoChannelAvatar) | |
81 | ) | |
82 | ||
83 | videoChannelRouter.post('/:nameWithHost/banner/pick', | |
84 | authenticate, | |
85 | reqBannerFile, | |
86 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
87 | ensureIsLocalChannel, | |
88 | ensureCanManageChannel, | |
89 | updateBannerValidator, | |
90 | asyncMiddleware(updateVideoChannelBanner) | |
91 | ) | |
92 | ||
93 | videoChannelRouter.delete('/:nameWithHost/avatar', | |
94 | authenticate, | |
95 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
96 | ensureIsLocalChannel, | |
97 | ensureCanManageChannel, | |
98 | asyncMiddleware(deleteVideoChannelAvatar) | |
99 | ) | |
100 | ||
101 | videoChannelRouter.delete('/:nameWithHost/banner', | |
102 | authenticate, | |
103 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
104 | ensureIsLocalChannel, | |
105 | ensureCanManageChannel, | |
106 | asyncMiddleware(deleteVideoChannelBanner) | |
107 | ) | |
108 | ||
109 | videoChannelRouter.put('/:nameWithHost', | |
110 | authenticate, | |
111 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
112 | ensureIsLocalChannel, | |
113 | ensureCanManageChannel, | |
114 | videoChannelsUpdateValidator, | |
115 | asyncRetryTransactionMiddleware(updateVideoChannel) | |
116 | ) | |
117 | ||
118 | videoChannelRouter.delete('/:nameWithHost', | |
119 | authenticate, | |
120 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
121 | ensureIsLocalChannel, | |
122 | ensureCanManageChannel, | |
123 | asyncMiddleware(videoChannelsRemoveValidator), | |
124 | asyncRetryTransactionMiddleware(removeVideoChannel) | |
125 | ) | |
126 | ||
127 | videoChannelRouter.get('/:nameWithHost', | |
128 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
129 | getVideoChannel | |
130 | ) | |
131 | ||
132 | videoChannelRouter.get('/:nameWithHost/video-playlists', | |
133 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
134 | paginationValidator, | |
135 | videoPlaylistsSortValidator, | |
136 | setDefaultSort, | |
137 | setDefaultPagination, | |
138 | commonVideoPlaylistFiltersValidator, | |
139 | asyncMiddleware(listVideoChannelPlaylists) | |
140 | ) | |
141 | ||
142 | videoChannelRouter.get('/:nameWithHost/videos', | |
143 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
144 | paginationValidator, | |
145 | videosSortValidator, | |
146 | setDefaultVideosSort, | |
147 | setDefaultPagination, | |
148 | optionalAuthenticate, | |
149 | commonVideosFiltersValidator, | |
150 | asyncMiddleware(listVideoChannelVideos) | |
151 | ) | |
152 | ||
153 | videoChannelRouter.get('/:nameWithHost/followers', | |
154 | authenticate, | |
155 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
156 | ensureCanManageChannel, | |
157 | paginationValidator, | |
158 | videoChannelsFollowersSortValidator, | |
159 | setDefaultSort, | |
160 | setDefaultPagination, | |
161 | asyncMiddleware(listVideoChannelFollowers) | |
162 | ) | |
163 | ||
164 | // --------------------------------------------------------------------------- | |
165 | ||
166 | export { | |
167 | videoChannelRouter | |
168 | } | |
169 | ||
170 | // --------------------------------------------------------------------------- | |
171 | ||
172 | async function listVideoChannels (req: express.Request, res: express.Response) { | |
173 | const serverActor = await getServerActor() | |
174 | const resultList = await VideoChannelModel.listForApi({ | |
175 | actorId: serverActor.id, | |
176 | start: req.query.start, | |
177 | count: req.query.count, | |
178 | sort: req.query.sort | |
179 | }) | |
180 | ||
181 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
182 | } | |
183 | ||
184 | async function updateVideoChannelBanner (req: express.Request, res: express.Response) { | |
185 | const bannerPhysicalFile = req.files['bannerfile'][0] | |
186 | const videoChannel = res.locals.videoChannel | |
187 | const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON()) | |
188 | ||
189 | const banner = await updateLocalActorImageFile(videoChannel, bannerPhysicalFile, ActorImageType.BANNER) | |
190 | ||
191 | auditLogger.update(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannel.toFormattedJSON()), oldVideoChannelAuditKeys) | |
192 | ||
193 | return res.json({ banner: banner.toFormattedJSON() }) | |
194 | } | |
195 | ||
196 | async function updateVideoChannelAvatar (req: express.Request, res: express.Response) { | |
197 | const avatarPhysicalFile = req.files['avatarfile'][0] | |
198 | const videoChannel = res.locals.videoChannel | |
199 | const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON()) | |
200 | ||
201 | const avatar = await updateLocalActorImageFile(videoChannel, avatarPhysicalFile, ActorImageType.AVATAR) | |
202 | ||
203 | auditLogger.update(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannel.toFormattedJSON()), oldVideoChannelAuditKeys) | |
204 | ||
205 | return res.json({ avatar: avatar.toFormattedJSON() }) | |
206 | } | |
207 | ||
208 | async function deleteVideoChannelAvatar (req: express.Request, res: express.Response) { | |
209 | const videoChannel = res.locals.videoChannel | |
210 | ||
211 | await deleteLocalActorImageFile(videoChannel, ActorImageType.AVATAR) | |
212 | ||
213 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
214 | } | |
215 | ||
216 | async function deleteVideoChannelBanner (req: express.Request, res: express.Response) { | |
217 | const videoChannel = res.locals.videoChannel | |
218 | ||
219 | await deleteLocalActorImageFile(videoChannel, ActorImageType.BANNER) | |
220 | ||
221 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
222 | } | |
223 | ||
224 | async function addVideoChannel (req: express.Request, res: express.Response) { | |
225 | const videoChannelInfo: VideoChannelCreate = req.body | |
226 | ||
227 | const videoChannelCreated = await sequelizeTypescript.transaction(async t => { | |
228 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) | |
229 | ||
230 | return createLocalVideoChannel(videoChannelInfo, account, t) | |
231 | }) | |
232 | ||
233 | const payload = { actorId: videoChannelCreated.actorId } | |
234 | await JobQueue.Instance.createJobWithPromise({ type: 'actor-keys', payload }) | |
235 | ||
236 | auditLogger.create(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelCreated.toFormattedJSON())) | |
237 | logger.info('Video channel %s created.', videoChannelCreated.Actor.url) | |
238 | ||
239 | return res.json({ | |
240 | videoChannel: { | |
241 | id: videoChannelCreated.id | |
242 | } | |
243 | }) | |
244 | } | |
245 | ||
246 | async function updateVideoChannel (req: express.Request, res: express.Response) { | |
247 | const videoChannelInstance = res.locals.videoChannel | |
248 | const videoChannelFieldsSave = videoChannelInstance.toJSON() | |
249 | const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannelInstance.toFormattedJSON()) | |
250 | const videoChannelInfoToUpdate = req.body as VideoChannelUpdate | |
251 | let doBulkVideoUpdate = false | |
252 | ||
253 | try { | |
254 | await sequelizeTypescript.transaction(async t => { | |
255 | if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.name = videoChannelInfoToUpdate.displayName | |
256 | if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.description = videoChannelInfoToUpdate.description | |
257 | ||
258 | if (videoChannelInfoToUpdate.support !== undefined) { | |
259 | const oldSupportField = videoChannelInstance.support | |
260 | videoChannelInstance.support = videoChannelInfoToUpdate.support | |
261 | ||
262 | if (videoChannelInfoToUpdate.bulkVideosSupportUpdate === true && oldSupportField !== videoChannelInfoToUpdate.support) { | |
263 | doBulkVideoUpdate = true | |
264 | await VideoModel.bulkUpdateSupportField(videoChannelInstance, t) | |
265 | } | |
266 | } | |
267 | ||
268 | const videoChannelInstanceUpdated = await videoChannelInstance.save({ transaction: t }) as MChannelBannerAccountDefault | |
269 | await sendUpdateActor(videoChannelInstanceUpdated, t) | |
270 | ||
271 | auditLogger.update( | |
272 | getAuditIdFromRes(res), | |
273 | new VideoChannelAuditView(videoChannelInstanceUpdated.toFormattedJSON()), | |
274 | oldVideoChannelAuditKeys | |
275 | ) | |
276 | ||
277 | logger.info('Video channel %s updated.', videoChannelInstance.Actor.url) | |
278 | }) | |
279 | } catch (err) { | |
280 | logger.debug('Cannot update the video channel.', { err }) | |
281 | ||
282 | // Force fields we want to update | |
283 | // If the transaction is retried, sequelize will think the object has not changed | |
284 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
285 | resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave) | |
286 | ||
287 | throw err | |
288 | } | |
289 | ||
290 | res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
291 | ||
292 | // Don't process in a transaction, and after the response because it could be long | |
293 | if (doBulkVideoUpdate) { | |
294 | await federateAllVideosOfChannel(videoChannelInstance) | |
295 | } | |
296 | } | |
297 | ||
298 | async function removeVideoChannel (req: express.Request, res: express.Response) { | |
299 | const videoChannelInstance = res.locals.videoChannel | |
300 | ||
301 | await sequelizeTypescript.transaction(async t => { | |
302 | await VideoPlaylistModel.resetPlaylistsOfChannel(videoChannelInstance.id, t) | |
303 | ||
304 | await videoChannelInstance.destroy({ transaction: t }) | |
305 | ||
306 | auditLogger.delete(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelInstance.toFormattedJSON())) | |
307 | logger.info('Video channel %s deleted.', videoChannelInstance.Actor.url) | |
308 | }) | |
309 | ||
310 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
311 | } | |
312 | ||
313 | function getVideoChannel (req: express.Request, res: express.Response) { | |
314 | const videoChannel = res.locals.videoChannel | |
315 | ||
316 | if (videoChannel.isOutdated()) { | |
317 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: videoChannel.Actor.url } }) | |
318 | } | |
319 | ||
320 | return res.json(videoChannel.toFormattedJSON()) | |
321 | } | |
322 | ||
323 | async function listVideoChannelPlaylists (req: express.Request, res: express.Response) { | |
324 | const serverActor = await getServerActor() | |
325 | ||
326 | const resultList = await VideoPlaylistModel.listForApi({ | |
327 | followerActorId: serverActor.id, | |
328 | start: req.query.start, | |
329 | count: req.query.count, | |
330 | sort: req.query.sort, | |
331 | videoChannelId: res.locals.videoChannel.id, | |
332 | type: req.query.playlistType | |
333 | }) | |
334 | ||
335 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
336 | } | |
337 | ||
338 | async function listVideoChannelVideos (req: express.Request, res: express.Response) { | |
339 | const serverActor = await getServerActor() | |
340 | ||
341 | const videoChannelInstance = res.locals.videoChannel | |
342 | ||
343 | const displayOnlyForFollower = isUserAbleToSearchRemoteURI(res) | |
344 | ? null | |
345 | : { | |
346 | actorId: serverActor.id, | |
347 | orLocalVideos: true | |
348 | } | |
349 | ||
350 | const countVideos = getCountVideos(req) | |
351 | const query = pickCommonVideoQuery(req.query) | |
352 | ||
353 | const apiOptions = await Hooks.wrapObject({ | |
354 | ...query, | |
355 | ||
356 | displayOnlyForFollower, | |
357 | nsfw: buildNSFWFilter(res, query.nsfw), | |
358 | videoChannelId: videoChannelInstance.id, | |
359 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, | |
360 | countVideos | |
361 | }, 'filter:api.video-channels.videos.list.params') | |
362 | ||
363 | const resultList = await Hooks.wrapPromiseFun( | |
364 | VideoModel.listForApi, | |
365 | apiOptions, | |
366 | 'filter:api.video-channels.videos.list.result' | |
367 | ) | |
368 | ||
369 | return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) | |
370 | } | |
371 | ||
372 | async function listVideoChannelFollowers (req: express.Request, res: express.Response) { | |
373 | const channel = res.locals.videoChannel | |
374 | ||
375 | const resultList = await ActorFollowModel.listFollowersForApi({ | |
376 | actorIds: [ channel.actorId ], | |
377 | start: req.query.start, | |
378 | count: req.query.count, | |
379 | sort: req.query.sort, | |
380 | search: req.query.search, | |
381 | state: 'accepted' | |
382 | }) | |
383 | ||
384 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
385 | } |