]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/video-channel.ts
Fix images size limit
[github/Chocobozzz/PeerTube.git] / server / controllers / api / video-channel.ts
CommitLineData
48dce1c9 1import * as express from 'express'
cc918ac3 2import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
48dce1c9
C
3import {
4 asyncMiddleware,
90d4bb81 5 asyncRetryTransactionMiddleware,
cc918ac3
C
6 authenticate,
7 optionalAuthenticate,
48dce1c9
C
8 paginationValidator,
9 setDefaultPagination,
10 setDefaultSort,
cc918ac3
C
11 videoChannelsAddValidator,
12 videoChannelsGetValidator,
13 videoChannelsRemoveValidator,
14 videoChannelsSortValidator,
15 videoChannelsUpdateValidator
48dce1c9
C
16} from '../../middlewares'
17import { VideoChannelModel } from '../../models/video/video-channel'
cc918ac3
C
18import { videosSortValidator } from '../../middlewares/validators'
19import { sendUpdateActor } from '../../lib/activitypub/send'
20import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
21import { createVideoChannel } from '../../lib/video-channel'
22import { isNSFWHidden } from '../../helpers/express-utils'
23import { setAsyncActorKeys } from '../../lib/activitypub'
cc918ac3
C
24import { AccountModel } from '../../models/account/account'
25import { sequelizeTypescript } from '../../initializers'
26import { logger } from '../../helpers/logger'
27import { VideoModel } from '../../models/video/video'
48dce1c9
C
28
29const videoChannelRouter = express.Router()
30
31videoChannelRouter.get('/',
32 paginationValidator,
33 videoChannelsSortValidator,
34 setDefaultSort,
35 setDefaultPagination,
36 asyncMiddleware(listVideoChannels)
37)
38
cc918ac3
C
39videoChannelRouter.post('/',
40 authenticate,
41 videoChannelsAddValidator,
90d4bb81 42 asyncRetryTransactionMiddleware(addVideoChannel)
cc918ac3
C
43)
44
45videoChannelRouter.put('/:id',
46 authenticate,
47 asyncMiddleware(videoChannelsUpdateValidator),
90d4bb81 48 asyncRetryTransactionMiddleware(updateVideoChannel)
cc918ac3
C
49)
50
51videoChannelRouter.delete('/:id',
52 authenticate,
53 asyncMiddleware(videoChannelsRemoveValidator),
90d4bb81 54 asyncRetryTransactionMiddleware(removeVideoChannel)
cc918ac3
C
55)
56
57videoChannelRouter.get('/:id',
58 asyncMiddleware(videoChannelsGetValidator),
59 asyncMiddleware(getVideoChannel)
60)
61
62videoChannelRouter.get('/:id/videos',
63 asyncMiddleware(videoChannelsGetValidator),
64 paginationValidator,
65 videosSortValidator,
66 setDefaultSort,
67 setDefaultPagination,
68 optionalAuthenticate,
69 asyncMiddleware(listVideoChannelVideos)
70)
71
48dce1c9
C
72// ---------------------------------------------------------------------------
73
74export {
75 videoChannelRouter
76}
77
78// ---------------------------------------------------------------------------
79
80async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
81 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
82
83 return res.json(getFormattedObjects(resultList.data, resultList.total))
84}
cc918ac3 85
cc918ac3
C
86async function addVideoChannel (req: express.Request, res: express.Response) {
87 const videoChannelInfo: VideoChannelCreate = req.body
88 const account: AccountModel = res.locals.oauth.token.User.Account
89
90 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
91 return createVideoChannel(videoChannelInfo, account, t)
92 })
93
94 setAsyncActorKeys(videoChannelCreated.Actor)
95 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
96
97 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
98
90d4bb81
C
99 return res.json({
100 videoChannel: {
101 id: videoChannelCreated.id,
102 uuid: videoChannelCreated.Actor.uuid
103 }
104 }).end()
cc918ac3
C
105}
106
107async function updateVideoChannel (req: express.Request, res: express.Response) {
108 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
109 const videoChannelFieldsSave = videoChannelInstance.toJSON()
110 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
111
112 try {
113 await sequelizeTypescript.transaction(async t => {
114 const sequelizeOptions = {
115 transaction: t
116 }
117
08c1efbe 118 if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.displayName)
cc918ac3
C
119 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
120 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
121
122 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
123 await sendUpdateActor(videoChannelInstanceUpdated, t)
124 })
125
126 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
127 } catch (err) {
128 logger.debug('Cannot update the video channel.', { err })
129
130 // Force fields we want to update
131 // If the transaction is retried, sequelize will think the object has not changed
132 // So it will skip the SQL request, even if the last one was ROLLBACKed!
133 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
134
135 throw err
136 }
cc918ac3
C
137
138 return res.type('json').status(204).end()
139}
140
141async function removeVideoChannel (req: express.Request, res: express.Response) {
142 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
143
90d4bb81 144 await sequelizeTypescript.transaction(async t => {
cc918ac3
C
145 await videoChannelInstance.destroy({ transaction: t })
146
147 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
148 })
149
90d4bb81 150 return res.type('json').status(204).end()
cc918ac3
C
151}
152
153async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
154 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
155
156 return res.json(videoChannelWithVideos.toFormattedJSON())
157}
158
159async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
160 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
161
162 const resultList = await VideoModel.listForApi({
163 start: req.query.start,
164 count: req.query.count,
165 sort: req.query.sort,
166 hideNSFW: isNSFWHidden(res),
167 withFiles: false,
168 videoChannelId: videoChannelInstance.id
169 })
170
171 return res.json(getFormattedObjects(resultList.data, resultList.total))
172}