diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-24 19:41:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-26 09:11:38 +0200 |
commit | 72c7248b6fdcdb2175e726ff51b42e7555f2bd84 (patch) | |
tree | 1bfdee99dbe2392cc997edba8e314e2a8a401c72 /server/controllers/api/videos/channel.ts | |
parent | 8113a93a0d9f31aa9e23702bbc31b8a76275ae22 (diff) | |
download | PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.gz PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.zst PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.zip |
Add video channels
Diffstat (limited to 'server/controllers/api/videos/channel.ts')
-rw-r--r-- | server/controllers/api/videos/channel.ts | 196 |
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 @@ | |||
1 | import * as express from 'express' | ||
2 | |||
3 | import { database as db } from '../../../initializers' | ||
4 | import { | ||
5 | logger, | ||
6 | getFormattedObjects, | ||
7 | retryTransactionWrapper | ||
8 | } from '../../../helpers' | ||
9 | import { | ||
10 | authenticate, | ||
11 | paginationValidator, | ||
12 | videoChannelsSortValidator, | ||
13 | videoChannelsAddValidator, | ||
14 | setVideoChannelsSort, | ||
15 | setPagination, | ||
16 | videoChannelsRemoveValidator, | ||
17 | videoChannelGetValidator, | ||
18 | videoChannelsUpdateValidator, | ||
19 | listVideoAuthorChannelsValidator | ||
20 | } from '../../../middlewares' | ||
21 | import { | ||
22 | createVideoChannel, | ||
23 | updateVideoChannelToFriends | ||
24 | } from '../../../lib' | ||
25 | import { VideoChannelInstance, AuthorInstance } from '../../../models' | ||
26 | import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared' | ||
27 | |||
28 | const videoChannelRouter = express.Router() | ||
29 | |||
30 | videoChannelRouter.get('/channels', | ||
31 | paginationValidator, | ||
32 | videoChannelsSortValidator, | ||
33 | setVideoChannelsSort, | ||
34 | setPagination, | ||
35 | listVideoChannels | ||
36 | ) | ||
37 | |||
38 | videoChannelRouter.get('/authors/:authorId/channels', | ||
39 | listVideoAuthorChannelsValidator, | ||
40 | listVideoAuthorChannels | ||
41 | ) | ||
42 | |||
43 | videoChannelRouter.post('/channels', | ||
44 | authenticate, | ||
45 | videoChannelsAddValidator, | ||
46 | addVideoChannelRetryWrapper | ||
47 | ) | ||
48 | |||
49 | videoChannelRouter.put('/channels/:id', | ||
50 | authenticate, | ||
51 | videoChannelsUpdateValidator, | ||
52 | updateVideoChannelRetryWrapper | ||
53 | ) | ||
54 | |||
55 | videoChannelRouter.delete('/channels/:id', | ||
56 | authenticate, | ||
57 | videoChannelsRemoveValidator, | ||
58 | removeVideoChannelRetryWrapper | ||
59 | ) | ||
60 | |||
61 | videoChannelRouter.get('/channels/:id', | ||
62 | videoChannelGetValidator, | ||
63 | getVideoChannel | ||
64 | ) | ||
65 | |||
66 | // --------------------------------------------------------------------------- | ||
67 | |||
68 | export { | ||
69 | videoChannelRouter | ||
70 | } | ||
71 | |||
72 | // --------------------------------------------------------------------------- | ||
73 | |||
74 | function 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 | |||
80 | function 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 | ||
88 | function 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 | |||
102 | function 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 | |||
116 | function 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 | |||
127 | function 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 | |||
166 | function 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 | |||
177 | function 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 | |||
192 | function 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 | } | ||