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