diff options
author | Chocobozzz <me@florianbigard.com> | 2018-04-24 17:05:32 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-04-24 17:12:57 +0200 |
commit | 48dce1c90dff4e90a4bcffefaecf157336cf904b (patch) | |
tree | 478de23812d0bfd93f47e9ad6ad8888c9edcc235 /server/controllers/api/videos | |
parent | 82e392f8a42a19815e932dd386e96e61ebe6d191 (diff) | |
download | PeerTube-48dce1c90dff4e90a4bcffefaecf157336cf904b.tar.gz PeerTube-48dce1c90dff4e90a4bcffefaecf157336cf904b.tar.zst PeerTube-48dce1c90dff4e90a4bcffefaecf157336cf904b.zip |
Update video channel routes
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r-- | server/controllers/api/videos/channel.ts | 177 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 17 |
2 files changed, 8 insertions, 186 deletions
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 @@ | |||
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 { sendUpdateActor } from '../../../lib/activitypub/send' | ||
9 | import { createVideoChannel } from '../../../lib/video-channel' | ||
10 | import { | ||
11 | asyncMiddleware, authenticate, listVideoAccountChannelsValidator, paginationValidator, setDefaultSort, setDefaultPagination, | ||
12 | videoChannelsAddValidator, videoChannelsGetValidator, videoChannelsRemoveValidator, videoChannelsSortValidator, | ||
13 | videoChannelsUpdateValidator | ||
14 | } from '../../../middlewares' | ||
15 | import { AccountModel } from '../../../models/account/account' | ||
16 | import { VideoChannelModel } from '../../../models/video/video-channel' | ||
17 | |||
18 | const videoChannelRouter = express.Router() | ||
19 | |||
20 | videoChannelRouter.get('/channels', | ||
21 | paginationValidator, | ||
22 | videoChannelsSortValidator, | ||
23 | setDefaultSort, | ||
24 | setDefaultPagination, | ||
25 | asyncMiddleware(listVideoChannels) | ||
26 | ) | ||
27 | |||
28 | videoChannelRouter.get('/accounts/:accountId/channels', | ||
29 | asyncMiddleware(listVideoAccountChannelsValidator), | ||
30 | asyncMiddleware(listVideoAccountChannels) | ||
31 | ) | ||
32 | |||
33 | videoChannelRouter.post('/channels', | ||
34 | authenticate, | ||
35 | videoChannelsAddValidator, | ||
36 | asyncMiddleware(addVideoChannelRetryWrapper) | ||
37 | ) | ||
38 | |||
39 | videoChannelRouter.put('/channels/:id', | ||
40 | authenticate, | ||
41 | asyncMiddleware(videoChannelsUpdateValidator), | ||
42 | updateVideoChannelRetryWrapper | ||
43 | ) | ||
44 | |||
45 | videoChannelRouter.delete('/channels/:id', | ||
46 | authenticate, | ||
47 | asyncMiddleware(videoChannelsRemoveValidator), | ||
48 | asyncMiddleware(removeVideoChannelRetryWrapper) | ||
49 | ) | ||
50 | |||
51 | videoChannelRouter.get('/channels/:id', | ||
52 | asyncMiddleware(videoChannelsGetValidator), | ||
53 | asyncMiddleware(getVideoChannel) | ||
54 | ) | ||
55 | |||
56 | // --------------------------------------------------------------------------- | ||
57 | |||
58 | export { | ||
59 | videoChannelRouter | ||
60 | } | ||
61 | |||
62 | // --------------------------------------------------------------------------- | ||
63 | |||
64 | async 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 | |||
70 | async 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 | ||
78 | async 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 | |||
92 | async 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 | |||
108 | async 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 | |||
119 | async 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 | |||
151 | async 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 | |||
162 | async 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 | |||
173 | async 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' | |||
42 | import { VideoFileModel } from '../../../models/video/video-file' | 42 | import { VideoFileModel } from '../../../models/video/video-file' |
43 | import { abuseVideoRouter } from './abuse' | 43 | import { abuseVideoRouter } from './abuse' |
44 | import { blacklistRouter } from './blacklist' | 44 | import { blacklistRouter } from './blacklist' |
45 | import { videoChannelRouter } from './channel' | ||
46 | import { videoCommentRouter } from './comment' | 45 | import { videoCommentRouter } from './comment' |
47 | import { rateVideoRouter } from './rate' | 46 | import { rateVideoRouter } from './rate' |
48 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' | 47 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' |
@@ -72,7 +71,6 @@ const reqVideoFileUpdate = createReqFiles( | |||
72 | videosRouter.use('/', abuseVideoRouter) | 71 | videosRouter.use('/', abuseVideoRouter) |
73 | videosRouter.use('/', blacklistRouter) | 72 | videosRouter.use('/', blacklistRouter) |
74 | videosRouter.use('/', rateVideoRouter) | 73 | videosRouter.use('/', rateVideoRouter) |
75 | videosRouter.use('/', videoChannelRouter) | ||
76 | videosRouter.use('/', videoCommentRouter) | 74 | videosRouter.use('/', videoCommentRouter) |
77 | 75 | ||
78 | videosRouter.get('/categories', listVideoCategories) | 76 | videosRouter.get('/categories', listVideoCategories) |
@@ -397,13 +395,14 @@ async function getVideoDescription (req: express.Request, res: express.Response) | |||
397 | } | 395 | } |
398 | 396 | ||
399 | async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | 397 | async 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 | } |