]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Update video channel routes
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
48dce1c9
C
2import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
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'
0626e7af 16import { accountsGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
265ba139 17import { AccountModel } from '../../models/account/account'
0626e7af 18import { VideoModel } from '../../models/video/video'
0626e7af 19import { isNSFWHidden } from '../../helpers/express-utils'
48dce1c9
C
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'
265ba139
C
28
29const accountsRouter = express.Router()
30
31accountsRouter.get('/',
32 paginationValidator,
33 accountsSortValidator,
1174a847 34 setDefaultSort,
f05a1c30 35 setDefaultPagination,
265ba139
C
36 asyncMiddleware(listAccounts)
37)
38
39accountsRouter.get('/:id',
40 asyncMiddleware(accountsGetValidator),
41 getAccount
42)
43
0626e7af
C
44accountsRouter.get('/:id/videos',
45 asyncMiddleware(accountsGetValidator),
46 paginationValidator,
47 videosSortValidator,
48 setDefaultSort,
49 setDefaultPagination,
50 optionalAuthenticate,
48dce1c9
C
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)
0626e7af
C
90)
91
265ba139
C
92// ---------------------------------------------------------------------------
93
94export {
95 accountsRouter
96}
97
98// ---------------------------------------------------------------------------
99
100function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
0626e7af
C
101 const account: AccountModel = res.locals.account
102
103 return res.json(account.toFormattedJSON())
265ba139
C
104}
105
106async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
107 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
108
109 return res.json(getFormattedObjects(resultList.data, resultList.total))
110}
0626e7af 111
48dce1c9
C
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) {
0626e7af
C
238 const account: AccountModel = res.locals.account
239
48dce1c9
C
240 const resultList = await VideoModel.listForApi({
241 start: req.query.start,
242 count: req.query.count,
243 sort: req.query.sort,
244 hideNSFW: isNSFWHidden(res),
245 withFiles: false,
246 accountId: account.id
247 })
0626e7af
C
248
249 return res.json(getFormattedObjects(resultList.data, resultList.total))
250}