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