]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/video-channel.ts
Increase redundancy check params timeout
[github/Chocobozzz/PeerTube.git] / server / controllers / api / video-channel.ts
CommitLineData
48dce1c9 1import * as express from 'express'
e1c55031 2import { getFormattedObjects } from '../../helpers/utils'
48dce1c9
C
3import {
4 asyncMiddleware,
90d4bb81 5 asyncRetryTransactionMiddleware,
06215f15
C
6 authenticate,
7 commonVideosFiltersValidator,
cc918ac3 8 optionalAuthenticate,
48dce1c9
C
9 paginationValidator,
10 setDefaultPagination,
11 setDefaultSort,
cc918ac3 12 videoChannelsAddValidator,
cc918ac3
C
13 videoChannelsRemoveValidator,
14 videoChannelsSortValidator,
418d092a
C
15 videoChannelsUpdateValidator,
16 videoPlaylistsSortValidator
48dce1c9
C
17} from '../../middlewares'
18import { VideoChannelModel } from '../../models/video/video-channel'
bc99dfe5 19import { videoChannelsNameWithHostValidator, videosSortValidator, videoChannelsOwnSearchValidator } from '../../middlewares/validators'
cc918ac3
C
20import { sendUpdateActor } from '../../lib/activitypub/send'
21import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
1ca9f7c3 22import { createLocalVideoChannel, federateAllVideosOfChannel } from '../../lib/video-channel'
fe987656 23import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
8dc8a34e 24import { setAsyncActorKeys } from '../../lib/activitypub/actor'
cc918ac3 25import { AccountModel } from '../../models/account/account'
74dc3bca 26import { MIMETYPES } from '../../initializers/constants'
cc918ac3
C
27import { logger } from '../../helpers/logger'
28import { VideoModel } from '../../models/video/video'
4bbfc6c6
C
29import { updateAvatarValidator } from '../../middlewares/validators/avatar'
30import { updateActorAvatarFile } from '../../lib/avatar'
993cef4b 31import { auditLoggerFactory, getAuditIdFromRes, VideoChannelAuditView } from '../../helpers/audit-logger'
06215f15 32import { resetSequelizeInstance } from '../../helpers/database-utils'
744d0eca 33import { JobQueue } from '../../lib/job-queue'
418d092a 34import { VideoPlaylistModel } from '../../models/video/video-playlist'
df0b219d 35import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
6dd9de95 36import { CONFIG } from '../../initializers/config'
74dc3bca 37import { sequelizeTypescript } from '../../initializers/database'
26d6bf65 38import { MChannelAccountDefault } from '@server/types/models'
8dc8a34e 39import { getServerActor } from '@server/models/application/application'
4bbfc6c6 40
80e36cd9 41const auditLogger = auditLoggerFactory('channels')
14e2014a 42const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR })
48dce1c9
C
43
44const videoChannelRouter = express.Router()
45
46videoChannelRouter.get('/',
47 paginationValidator,
48 videoChannelsSortValidator,
49 setDefaultSort,
50 setDefaultPagination,
bc99dfe5 51 videoChannelsOwnSearchValidator,
48dce1c9
C
52 asyncMiddleware(listVideoChannels)
53)
54
cc918ac3
C
55videoChannelRouter.post('/',
56 authenticate,
601527d7 57 asyncMiddleware(videoChannelsAddValidator),
90d4bb81 58 asyncRetryTransactionMiddleware(addVideoChannel)
cc918ac3
C
59)
60
8a19bee1 61videoChannelRouter.post('/:nameWithHost/avatar/pick',
4bbfc6c6
C
62 authenticate,
63 reqAvatarFile,
64 // Check the rights
65 asyncMiddleware(videoChannelsUpdateValidator),
66 updateAvatarValidator,
4a534352 67 asyncMiddleware(updateVideoChannelAvatar)
4bbfc6c6
C
68)
69
8a19bee1 70videoChannelRouter.put('/:nameWithHost',
cc918ac3
C
71 authenticate,
72 asyncMiddleware(videoChannelsUpdateValidator),
90d4bb81 73 asyncRetryTransactionMiddleware(updateVideoChannel)
cc918ac3
C
74)
75
8a19bee1 76videoChannelRouter.delete('/:nameWithHost',
cc918ac3
C
77 authenticate,
78 asyncMiddleware(videoChannelsRemoveValidator),
90d4bb81 79 asyncRetryTransactionMiddleware(removeVideoChannel)
cc918ac3
C
80)
81
8a19bee1
C
82videoChannelRouter.get('/:nameWithHost',
83 asyncMiddleware(videoChannelsNameWithHostValidator),
cc918ac3
C
84 asyncMiddleware(getVideoChannel)
85)
86
418d092a
C
87videoChannelRouter.get('/:nameWithHost/video-playlists',
88 asyncMiddleware(videoChannelsNameWithHostValidator),
89 paginationValidator,
90 videoPlaylistsSortValidator,
91 setDefaultSort,
92 setDefaultPagination,
df0b219d 93 commonVideoPlaylistFiltersValidator,
418d092a
C
94 asyncMiddleware(listVideoChannelPlaylists)
95)
96
8a19bee1
C
97videoChannelRouter.get('/:nameWithHost/videos',
98 asyncMiddleware(videoChannelsNameWithHostValidator),
cc918ac3
C
99 paginationValidator,
100 videosSortValidator,
101 setDefaultSort,
102 setDefaultPagination,
103 optionalAuthenticate,
d525fc39 104 commonVideosFiltersValidator,
cc918ac3
C
105 asyncMiddleware(listVideoChannelVideos)
106)
107
48dce1c9
C
108// ---------------------------------------------------------------------------
109
110export {
111 videoChannelRouter
112}
113
114// ---------------------------------------------------------------------------
115
dae86118 116async function listVideoChannels (req: express.Request, res: express.Response) {
f37dc0dd 117 const serverActor = await getServerActor()
bc99dfe5
RK
118 const resultList = await VideoChannelModel.listForApi({
119 actorId: serverActor.id,
120 start: req.query.start,
121 count: req.query.count,
4f5d0459 122 sort: req.query.sort
bc99dfe5 123 })
48dce1c9
C
124
125 return res.json(getFormattedObjects(resultList.data, resultList.total))
126}
cc918ac3 127
dae86118 128async function updateVideoChannelAvatar (req: express.Request, res: express.Response) {
a1587156 129 const avatarPhysicalFile = req.files['avatarfile'][0]
dae86118 130 const videoChannel = res.locals.videoChannel
80e36cd9 131 const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON())
4bbfc6c6 132
f201a749 133 const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel)
4bbfc6c6 134
f201a749 135 auditLogger.update(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannel.toFormattedJSON()), oldVideoChannelAuditKeys)
80e36cd9 136
4bbfc6c6
C
137 return res
138 .json({
139 avatar: avatar.toFormattedJSON()
140 })
141 .end()
142}
143
cc918ac3
C
144async function addVideoChannel (req: express.Request, res: express.Response) {
145 const videoChannelInfo: VideoChannelCreate = req.body
cc918ac3 146
453e83ea 147 const videoChannelCreated = await sequelizeTypescript.transaction(async t => {
dae86118 148 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
91411dba 149
1ca9f7c3 150 return createLocalVideoChannel(videoChannelInfo, account, t)
cc918ac3
C
151 })
152
153 setAsyncActorKeys(videoChannelCreated.Actor)
57cfff78 154 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.url, { err }))
cc918ac3 155
91411dba 156 auditLogger.create(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelCreated.toFormattedJSON()))
57cfff78 157 logger.info('Video channel %s created.', videoChannelCreated.Actor.url)
cc918ac3 158
90d4bb81
C
159 return res.json({
160 videoChannel: {
57cfff78 161 id: videoChannelCreated.id
90d4bb81
C
162 }
163 }).end()
cc918ac3
C
164}
165
166async function updateVideoChannel (req: express.Request, res: express.Response) {
dae86118 167 const videoChannelInstance = res.locals.videoChannel
cc918ac3 168 const videoChannelFieldsSave = videoChannelInstance.toJSON()
80e36cd9 169 const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannelInstance.toFormattedJSON())
cc918ac3 170 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
7d14d4d2 171 let doBulkVideoUpdate = false
cc918ac3
C
172
173 try {
174 await sequelizeTypescript.transaction(async t => {
175 const sequelizeOptions = {
176 transaction: t
177 }
178
7d14d4d2
C
179 if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.name = videoChannelInfoToUpdate.displayName
180 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.description = videoChannelInfoToUpdate.description
181
182 if (videoChannelInfoToUpdate.support !== undefined) {
183 const oldSupportField = videoChannelInstance.support
184 videoChannelInstance.support = videoChannelInfoToUpdate.support
185
186 if (videoChannelInfoToUpdate.bulkVideosSupportUpdate === true && oldSupportField !== videoChannelInfoToUpdate.support) {
187 doBulkVideoUpdate = true
188 await VideoModel.bulkUpdateSupportField(videoChannelInstance, t)
189 }
190 }
cc918ac3 191
b5fecbf4 192 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions) as MChannelAccountDefault
cc918ac3 193 await sendUpdateActor(videoChannelInstanceUpdated, t)
cc918ac3 194
80e36cd9 195 auditLogger.update(
993cef4b 196 getAuditIdFromRes(res),
80e36cd9
AB
197 new VideoChannelAuditView(videoChannelInstanceUpdated.toFormattedJSON()),
198 oldVideoChannelAuditKeys
199 )
7d14d4d2 200
57cfff78 201 logger.info('Video channel %s updated.', videoChannelInstance.Actor.url)
80e36cd9 202 })
cc918ac3
C
203 } catch (err) {
204 logger.debug('Cannot update the video channel.', { err })
205
206 // Force fields we want to update
207 // If the transaction is retried, sequelize will think the object has not changed
208 // So it will skip the SQL request, even if the last one was ROLLBACKed!
209 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
210
211 throw err
212 }
cc918ac3 213
7d14d4d2
C
214 res.type('json').status(204).end()
215
216 // Don't process in a transaction, and after the response because it could be long
217 if (doBulkVideoUpdate) {
218 await federateAllVideosOfChannel(videoChannelInstance)
219 }
cc918ac3
C
220}
221
222async function removeVideoChannel (req: express.Request, res: express.Response) {
dae86118 223 const videoChannelInstance = res.locals.videoChannel
cc918ac3 224
90d4bb81 225 await sequelizeTypescript.transaction(async t => {
df0b219d
C
226 await VideoPlaylistModel.resetPlaylistsOfChannel(videoChannelInstance.id, t)
227
cc918ac3
C
228 await videoChannelInstance.destroy({ transaction: t })
229
993cef4b 230 auditLogger.delete(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelInstance.toFormattedJSON()))
57cfff78 231 logger.info('Video channel %s deleted.', videoChannelInstance.Actor.url)
cc918ac3
C
232 })
233
90d4bb81 234 return res.type('json').status(204).end()
cc918ac3
C
235}
236
dae86118 237async function getVideoChannel (req: express.Request, res: express.Response) {
cc918ac3
C
238 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
239
744d0eca
C
240 if (videoChannelWithVideos.isOutdated()) {
241 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: videoChannelWithVideos.Actor.url } })
744d0eca
C
242 }
243
cc918ac3
C
244 return res.json(videoChannelWithVideos.toFormattedJSON())
245}
246
418d092a
C
247async function listVideoChannelPlaylists (req: express.Request, res: express.Response) {
248 const serverActor = await getServerActor()
249
250 const resultList = await VideoPlaylistModel.listForApi({
251 followerActorId: serverActor.id,
252 start: req.query.start,
253 count: req.query.count,
254 sort: req.query.sort,
df0b219d
C
255 videoChannelId: res.locals.videoChannel.id,
256 type: req.query.playlistType
418d092a
C
257 })
258
259 return res.json(getFormattedObjects(resultList.data, resultList.total))
260}
261
dae86118
C
262async function listVideoChannelVideos (req: express.Request, res: express.Response) {
263 const videoChannelInstance = res.locals.videoChannel
4e74e803 264 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
fe987656 265 const countVideos = getCountVideos(req)
cc918ac3
C
266
267 const resultList = await VideoModel.listForApi({
4e74e803 268 followerActorId,
cc918ac3
C
269 start: req.query.start,
270 count: req.query.count,
271 sort: req.query.sort,
8a19bee1 272 includeLocalVideos: true,
d525fc39
C
273 categoryOneOf: req.query.categoryOneOf,
274 licenceOneOf: req.query.licenceOneOf,
275 languageOneOf: req.query.languageOneOf,
276 tagsOneOf: req.query.tagsOneOf,
277 tagsAllOf: req.query.tagsAllOf,
1cd3facc 278 filter: req.query.filter,
d525fc39 279 nsfw: buildNSFWFilter(res, req.query.nsfw),
cc918ac3 280 withFiles: false,
1cd3facc 281 videoChannelId: videoChannelInstance.id,
fe987656
C
282 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
283 countVideos
cc918ac3
C
284 })
285
286 return res.json(getFormattedObjects(resultList.data, resultList.total))
287}