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