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