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