aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-04-24 17:05:32 +0200
committerChocobozzz <me@florianbigard.com>2018-04-24 17:12:57 +0200
commit48dce1c90dff4e90a4bcffefaecf157336cf904b (patch)
tree478de23812d0bfd93f47e9ad6ad8888c9edcc235 /server/controllers/api
parent82e392f8a42a19815e932dd386e96e61ebe6d191 (diff)
downloadPeerTube-48dce1c90dff4e90a4bcffefaecf157336cf904b.tar.gz
PeerTube-48dce1c90dff4e90a4bcffefaecf157336cf904b.tar.zst
PeerTube-48dce1c90dff4e90a4bcffefaecf157336cf904b.zip
Update video channel routes
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/accounts.ts209
-rw-r--r--server/controllers/api/index.ts2
-rw-r--r--server/controllers/api/video-channel.ts34
-rw-r--r--server/controllers/api/videos/channel.ts177
-rw-r--r--server/controllers/api/videos/index.ts17
5 files changed, 239 insertions, 200 deletions
diff --git a/server/controllers/api/accounts.ts b/server/controllers/api/accounts.ts
index 06ab04033..04c5897c5 100644
--- a/server/controllers/api/accounts.ts
+++ b/server/controllers/api/accounts.ts
@@ -1,11 +1,30 @@
1import * as express from 'express' 1import * as express from 'express'
2import { getFormattedObjects } from '../../helpers/utils' 2import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
3import { asyncMiddleware, optionalAuthenticate, paginationValidator, setDefaultPagination, setDefaultSort } from '../../middlewares' 3import {
4 asyncMiddleware,
5 authenticate,
6 listVideoAccountChannelsValidator,
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
10 setDefaultSort,
11 videoChannelsAddValidator,
12 videoChannelsGetValidator,
13 videoChannelsRemoveValidator,
14 videoChannelsUpdateValidator
15} from '../../middlewares'
4import { accountsGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators' 16import { accountsGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
5import { AccountModel } from '../../models/account/account' 17import { AccountModel } from '../../models/account/account'
6import { VideoModel } from '../../models/video/video' 18import { VideoModel } from '../../models/video/video'
7import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type'
8import { isNSFWHidden } from '../../helpers/express-utils' 19import { isNSFWHidden } from '../../helpers/express-utils'
20import { VideoChannelModel } from '../../models/video/video-channel'
21import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
22import { sendUpdateActor } from '../../lib/activitypub/send'
23import { createVideoChannel } from '../../lib/video-channel'
24import { setAsyncActorKeys } from '../../lib/activitypub'
25import { sequelizeTypescript } from '../../initializers'
26import { logger } from '../../helpers/logger'
27import { retryTransactionWrapper } from '../../helpers/database-utils'
9 28
10const accountsRouter = express.Router() 29const accountsRouter = express.Router()
11 30
@@ -29,7 +48,45 @@ accountsRouter.get('/:id/videos',
29 setDefaultSort, 48 setDefaultSort,
30 setDefaultPagination, 49 setDefaultPagination,
31 optionalAuthenticate, 50 optionalAuthenticate,
32 asyncMiddleware(getAccountVideos) 51 asyncMiddleware(listAccountVideos)
52)
53
54accountsRouter.get('/:accountId/video-channels',
55 asyncMiddleware(listVideoAccountChannelsValidator),
56 asyncMiddleware(listVideoAccountChannels)
57)
58
59accountsRouter.post('/:accountId/video-channels',
60 authenticate,
61 videoChannelsAddValidator,
62 asyncMiddleware(addVideoChannelRetryWrapper)
63)
64
65accountsRouter.put('/:accountId/video-channels/:id',
66 authenticate,
67 asyncMiddleware(videoChannelsUpdateValidator),
68 updateVideoChannelRetryWrapper
69)
70
71accountsRouter.delete('/:accountId/video-channels/:id',
72 authenticate,
73 asyncMiddleware(videoChannelsRemoveValidator),
74 asyncMiddleware(removeVideoChannelRetryWrapper)
75)
76
77accountsRouter.get('/:accountId/video-channels/:id',
78 asyncMiddleware(videoChannelsGetValidator),
79 asyncMiddleware(getVideoChannel)
80)
81
82accountsRouter.get('/:accountId/video-channels/:id/videos',
83 asyncMiddleware(videoChannelsGetValidator),
84 paginationValidator,
85 videosSortValidator,
86 setDefaultSort,
87 setDefaultPagination,
88 optionalAuthenticate,
89 asyncMiddleware(listVideoChannelVideos)
33) 90)
34 91
35// --------------------------------------------------------------------------- 92// ---------------------------------------------------------------------------
@@ -52,18 +109,142 @@ async function listAccounts (req: express.Request, res: express.Response, next:
52 return res.json(getFormattedObjects(resultList.data, resultList.total)) 109 return res.json(getFormattedObjects(resultList.data, resultList.total))
53} 110}
54 111
55async function getAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) { 112async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
113 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
114
115 return res.json(getFormattedObjects(resultList.data, resultList.total))
116}
117
118// Wrapper to video channel add that retry the async function if there is a database error
119// We need this because we run the transaction in SERIALIZABLE isolation that can fail
120async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
121 const options = {
122 arguments: [ req, res ],
123 errorMessage: 'Cannot insert the video video channel with many retries.'
124 }
125
126 const videoChannel = await retryTransactionWrapper(addVideoChannel, options)
127 return res.json({
128 videoChannel: {
129 id: videoChannel.id
130 }
131 }).end()
132}
133
134async function addVideoChannel (req: express.Request, res: express.Response) {
135 const videoChannelInfo: VideoChannelCreate = req.body
136 const account: AccountModel = res.locals.oauth.token.User.Account
137
138 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
139 return createVideoChannel(videoChannelInfo, account, t)
140 })
141
142 setAsyncActorKeys(videoChannelCreated.Actor)
143 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
144
145 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
146
147 return videoChannelCreated
148}
149
150async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
151 const options = {
152 arguments: [ req, res ],
153 errorMessage: 'Cannot update the video with many retries.'
154 }
155
156 await retryTransactionWrapper(updateVideoChannel, options)
157
158 return res.type('json').status(204).end()
159}
160
161async function updateVideoChannel (req: express.Request, res: express.Response) {
162 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
163 const videoChannelFieldsSave = videoChannelInstance.toJSON()
164 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
165
166 try {
167 await sequelizeTypescript.transaction(async t => {
168 const sequelizeOptions = {
169 transaction: t
170 }
171
172 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
173 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
174 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
175
176 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
177 await sendUpdateActor(videoChannelInstanceUpdated, t)
178 })
179
180 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
181 } catch (err) {
182 logger.debug('Cannot update the video channel.', { err })
183
184 // Force fields we want to update
185 // If the transaction is retried, sequelize will think the object has not changed
186 // So it will skip the SQL request, even if the last one was ROLLBACKed!
187 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
188
189 throw err
190 }
191}
192
193async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
194 const options = {
195 arguments: [ req, res ],
196 errorMessage: 'Cannot remove the video channel with many retries.'
197 }
198
199 await retryTransactionWrapper(removeVideoChannel, options)
200
201 return res.type('json').status(204).end()
202}
203
204async function removeVideoChannel (req: express.Request, res: express.Response) {
205 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
206
207 return sequelizeTypescript.transaction(async t => {
208 await videoChannelInstance.destroy({ transaction: t })
209
210 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
211 })
212
213}
214
215async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
216 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
217
218 return res.json(videoChannelWithVideos.toFormattedJSON())
219}
220
221async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
222 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
223
224 const resultList = await VideoModel.listForApi({
225 start: req.query.start,
226 count: req.query.count,
227 sort: req.query.sort,
228 hideNSFW: isNSFWHidden(res),
229 withFiles: false,
230 videoChannelId: videoChannelInstance.id
231 })
232
233 return res.json(getFormattedObjects(resultList.data, resultList.total))
234}
235
236
237async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
56 const account: AccountModel = res.locals.account 238 const account: AccountModel = res.locals.account
57 239
58 const resultList = await VideoModel.listForApi( 240 const resultList = await VideoModel.listForApi({
59 req.query.start as number, 241 start: req.query.start,
60 req.query.count as number, 242 count: req.query.count,
61 req.query.sort as VideoSortField, 243 sort: req.query.sort,
62 isNSFWHidden(res), 244 hideNSFW: isNSFWHidden(res),
63 null, 245 withFiles: false,
64 false, 246 accountId: account.id
65 account.id 247 })
66 )
67 248
68 return res.json(getFormattedObjects(resultList.data, resultList.total)) 249 return res.json(getFormattedObjects(resultList.data, resultList.total))
69} 250}
diff --git a/server/controllers/api/index.ts b/server/controllers/api/index.ts
index 964d5d04c..8f63b9535 100644
--- a/server/controllers/api/index.ts
+++ b/server/controllers/api/index.ts
@@ -7,6 +7,7 @@ import { usersRouter } from './users'
7import { accountsRouter } from './accounts' 7import { accountsRouter } from './accounts'
8import { videosRouter } from './videos' 8import { videosRouter } from './videos'
9import { badRequest } from '../../helpers/express-utils' 9import { badRequest } from '../../helpers/express-utils'
10import { videoChannelRouter } from './video-channel'
10 11
11const apiRouter = express.Router() 12const apiRouter = express.Router()
12 13
@@ -15,6 +16,7 @@ apiRouter.use('/oauth-clients', oauthClientsRouter)
15apiRouter.use('/config', configRouter) 16apiRouter.use('/config', configRouter)
16apiRouter.use('/users', usersRouter) 17apiRouter.use('/users', usersRouter)
17apiRouter.use('/accounts', accountsRouter) 18apiRouter.use('/accounts', accountsRouter)
19apiRouter.use('/video-channels', videoChannelRouter)
18apiRouter.use('/videos', videosRouter) 20apiRouter.use('/videos', videosRouter)
19apiRouter.use('/jobs', jobsRouter) 21apiRouter.use('/jobs', jobsRouter)
20apiRouter.use('/ping', pong) 22apiRouter.use('/ping', pong)
diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts
new file mode 100644
index 000000000..d57273747
--- /dev/null
+++ b/server/controllers/api/video-channel.ts
@@ -0,0 +1,34 @@
1import * as express from 'express'
2import { getFormattedObjects } from '../../helpers/utils'
3import {
4 asyncMiddleware,
5 paginationValidator,
6 setDefaultPagination,
7 setDefaultSort,
8 videoChannelsSortValidator
9} from '../../middlewares'
10import { VideoChannelModel } from '../../models/video/video-channel'
11
12const videoChannelRouter = express.Router()
13
14videoChannelRouter.get('/',
15 paginationValidator,
16 videoChannelsSortValidator,
17 setDefaultSort,
18 setDefaultPagination,
19 asyncMiddleware(listVideoChannels)
20)
21
22// ---------------------------------------------------------------------------
23
24export {
25 videoChannelRouter
26}
27
28// ---------------------------------------------------------------------------
29
30async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
31 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
32
33 return res.json(getFormattedObjects(resultList.data, resultList.total))
34}
diff --git a/server/controllers/api/videos/channel.ts b/server/controllers/api/videos/channel.ts
deleted file mode 100644
index e547d375f..000000000
--- a/server/controllers/api/videos/channel.ts
+++ /dev/null
@@ -1,177 +0,0 @@
1import * as express from 'express'
2import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
5import { getFormattedObjects, resetSequelizeInstance } from '../../../helpers/utils'
6import { sequelizeTypescript } from '../../../initializers'
7import { setAsyncActorKeys } from '../../../lib/activitypub'
8import { sendUpdateActor } from '../../../lib/activitypub/send'
9import { createVideoChannel } from '../../../lib/video-channel'
10import {
11 asyncMiddleware, authenticate, listVideoAccountChannelsValidator, paginationValidator, setDefaultSort, setDefaultPagination,
12 videoChannelsAddValidator, videoChannelsGetValidator, videoChannelsRemoveValidator, videoChannelsSortValidator,
13 videoChannelsUpdateValidator
14} from '../../../middlewares'
15import { AccountModel } from '../../../models/account/account'
16import { VideoChannelModel } from '../../../models/video/video-channel'
17
18const videoChannelRouter = express.Router()
19
20videoChannelRouter.get('/channels',
21 paginationValidator,
22 videoChannelsSortValidator,
23 setDefaultSort,
24 setDefaultPagination,
25 asyncMiddleware(listVideoChannels)
26)
27
28videoChannelRouter.get('/accounts/:accountId/channels',
29 asyncMiddleware(listVideoAccountChannelsValidator),
30 asyncMiddleware(listVideoAccountChannels)
31)
32
33videoChannelRouter.post('/channels',
34 authenticate,
35 videoChannelsAddValidator,
36 asyncMiddleware(addVideoChannelRetryWrapper)
37)
38
39videoChannelRouter.put('/channels/:id',
40 authenticate,
41 asyncMiddleware(videoChannelsUpdateValidator),
42 updateVideoChannelRetryWrapper
43)
44
45videoChannelRouter.delete('/channels/:id',
46 authenticate,
47 asyncMiddleware(videoChannelsRemoveValidator),
48 asyncMiddleware(removeVideoChannelRetryWrapper)
49)
50
51videoChannelRouter.get('/channels/:id',
52 asyncMiddleware(videoChannelsGetValidator),
53 asyncMiddleware(getVideoChannel)
54)
55
56// ---------------------------------------------------------------------------
57
58export {
59 videoChannelRouter
60}
61
62// ---------------------------------------------------------------------------
63
64async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
65 const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort)
66
67 return res.json(getFormattedObjects(resultList.data, resultList.total))
68}
69
70async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
71 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
72
73 return res.json(getFormattedObjects(resultList.data, resultList.total))
74}
75
76// Wrapper to video channel add that retry the async function if there is a database error
77// We need this because we run the transaction in SERIALIZABLE isolation that can fail
78async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
79 const options = {
80 arguments: [ req, res ],
81 errorMessage: 'Cannot insert the video video channel with many retries.'
82 }
83
84 const videoChannel = await retryTransactionWrapper(addVideoChannel, options)
85 return res.json({
86 videoChannel: {
87 id: videoChannel.id
88 }
89 }).end()
90}
91
92async function addVideoChannel (req: express.Request, res: express.Response) {
93 const videoChannelInfo: VideoChannelCreate = req.body
94 const account: AccountModel = res.locals.oauth.token.User.Account
95
96 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
97 return createVideoChannel(videoChannelInfo, account, t)
98 })
99
100 setAsyncActorKeys(videoChannelCreated.Actor)
101 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
102
103 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
104
105 return videoChannelCreated
106}
107
108async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
109 const options = {
110 arguments: [ req, res ],
111 errorMessage: 'Cannot update the video with many retries.'
112 }
113
114 await retryTransactionWrapper(updateVideoChannel, options)
115
116 return res.type('json').status(204).end()
117}
118
119async function updateVideoChannel (req: express.Request, res: express.Response) {
120 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
121 const videoChannelFieldsSave = videoChannelInstance.toJSON()
122 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
123
124 try {
125 await sequelizeTypescript.transaction(async t => {
126 const sequelizeOptions = {
127 transaction: t
128 }
129
130 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
131 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
132 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
133
134 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
135 await sendUpdateActor(videoChannelInstanceUpdated, t)
136 })
137
138 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
139 } catch (err) {
140 logger.debug('Cannot update the video channel.', { err })
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 }
149}
150
151async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
152 const options = {
153 arguments: [ req, res ],
154 errorMessage: 'Cannot remove the video channel with many retries.'
155 }
156
157 await retryTransactionWrapper(removeVideoChannel, options)
158
159 return res.type('json').status(204).end()
160}
161
162async function removeVideoChannel (req: express.Request, res: express.Response) {
163 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
164
165 return sequelizeTypescript.transaction(async t => {
166 await videoChannelInstance.destroy({ transaction: t })
167
168 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
169 })
170
171}
172
173async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
174 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
175
176 return res.json(videoChannelWithVideos.toFormattedJSON())
177}
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 61b6c5826..4b3198a74 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -42,7 +42,6 @@ import { VideoModel } from '../../../models/video/video'
42import { VideoFileModel } from '../../../models/video/video-file' 42import { VideoFileModel } from '../../../models/video/video-file'
43import { abuseVideoRouter } from './abuse' 43import { abuseVideoRouter } from './abuse'
44import { blacklistRouter } from './blacklist' 44import { blacklistRouter } from './blacklist'
45import { videoChannelRouter } from './channel'
46import { videoCommentRouter } from './comment' 45import { videoCommentRouter } from './comment'
47import { rateVideoRouter } from './rate' 46import { rateVideoRouter } from './rate'
48import { VideoFilter } from '../../../../shared/models/videos/video-query.type' 47import { VideoFilter } from '../../../../shared/models/videos/video-query.type'
@@ -72,7 +71,6 @@ const reqVideoFileUpdate = createReqFiles(
72videosRouter.use('/', abuseVideoRouter) 71videosRouter.use('/', abuseVideoRouter)
73videosRouter.use('/', blacklistRouter) 72videosRouter.use('/', blacklistRouter)
74videosRouter.use('/', rateVideoRouter) 73videosRouter.use('/', rateVideoRouter)
75videosRouter.use('/', videoChannelRouter)
76videosRouter.use('/', videoCommentRouter) 74videosRouter.use('/', videoCommentRouter)
77 75
78videosRouter.get('/categories', listVideoCategories) 76videosRouter.get('/categories', listVideoCategories)
@@ -397,13 +395,14 @@ async function getVideoDescription (req: express.Request, res: express.Response)
397} 395}
398 396
399async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { 397async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
400 const resultList = await VideoModel.listForApi( 398 const resultList = await VideoModel.listForApi({
401 req.query.start as number, 399 start: req.query.start,
402 req.query.count as number, 400 count: req.query.count,
403 req.query.sort as VideoSortField, 401 sort: req.query.sort,
404 isNSFWHidden(res), 402 hideNSFW: isNSFWHidden(res),
405 req.query.filter as VideoFilter 403 filter: req.query.filter as VideoFilter,
406 ) 404 withFiles: false
405 })
407 406
408 return res.json(getFormattedObjects(resultList.data, resultList.total)) 407 return res.json(getFormattedObjects(resultList.data, resultList.total))
409} 408}