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