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