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