1 import * as express from 'express'
2 import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
6 listVideoAccountChannelsValidator,
11 videoChannelsAddValidator,
12 videoChannelsGetValidator,
13 videoChannelsRemoveValidator,
14 videoChannelsUpdateValidator
15 } from '../../middlewares'
16 import { accountsGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
17 import { AccountModel } from '../../models/account/account'
18 import { VideoModel } from '../../models/video/video'
19 import { isNSFWHidden } from '../../helpers/express-utils'
20 import { VideoChannelModel } from '../../models/video/video-channel'
21 import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
22 import { sendUpdateActor } from '../../lib/activitypub/send'
23 import { createVideoChannel } from '../../lib/video-channel'
24 import { setAsyncActorKeys } from '../../lib/activitypub'
25 import { sequelizeTypescript } from '../../initializers'
26 import { logger } from '../../helpers/logger'
27 import { retryTransactionWrapper } from '../../helpers/database-utils'
29 const accountsRouter = express.Router()
31 accountsRouter.get('/',
33 accountsSortValidator,
36 asyncMiddleware(listAccounts)
39 accountsRouter.get('/:id',
40 asyncMiddleware(accountsGetValidator),
44 accountsRouter.get('/:id/videos',
45 asyncMiddleware(accountsGetValidator),
51 asyncMiddleware(listAccountVideos)
54 accountsRouter.get('/:accountId/video-channels',
55 asyncMiddleware(listVideoAccountChannelsValidator),
56 asyncMiddleware(listVideoAccountChannels)
59 accountsRouter.post('/:accountId/video-channels',
61 videoChannelsAddValidator,
62 asyncMiddleware(addVideoChannelRetryWrapper)
65 accountsRouter.put('/:accountId/video-channels/:id',
67 asyncMiddleware(videoChannelsUpdateValidator),
68 updateVideoChannelRetryWrapper
71 accountsRouter.delete('/:accountId/video-channels/:id',
73 asyncMiddleware(videoChannelsRemoveValidator),
74 asyncMiddleware(removeVideoChannelRetryWrapper)
77 accountsRouter.get('/:accountId/video-channels/:id',
78 asyncMiddleware(videoChannelsGetValidator),
79 asyncMiddleware(getVideoChannel)
82 accountsRouter.get('/:accountId/video-channels/:id/videos',
83 asyncMiddleware(videoChannelsGetValidator),
89 asyncMiddleware(listVideoChannelVideos)
92 // ---------------------------------------------------------------------------
98 // ---------------------------------------------------------------------------
100 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
101 const account: AccountModel = res.locals.account
103 return res.json(account.toFormattedJSON())
106 async 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)
109 return res.json(getFormattedObjects(resultList.data, resultList.total))
112 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
113 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
115 return res.json(getFormattedObjects(resultList.data, resultList.total))
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
120 async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
122 arguments: [ req, res ],
123 errorMessage: 'Cannot insert the video video channel with many retries.'
126 const videoChannel = await retryTransactionWrapper(addVideoChannel, options)
130 uuid: videoChannel.Actor.uuid
135 async function addVideoChannel (req: express.Request, res: express.Response) {
136 const videoChannelInfo: VideoChannelCreate = req.body
137 const account: AccountModel = res.locals.oauth.token.User.Account
139 const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => {
140 return createVideoChannel(videoChannelInfo, account, t)
143 setAsyncActorKeys(videoChannelCreated.Actor)
144 .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err }))
146 logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid)
148 return videoChannelCreated
151 async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
153 arguments: [ req, res ],
154 errorMessage: 'Cannot update the video with many retries.'
157 await retryTransactionWrapper(updateVideoChannel, options)
159 return res.type('json').status(204).end()
162 async function updateVideoChannel (req: express.Request, res: express.Response) {
163 const videoChannelInstance = res.locals.videoChannel as VideoChannelModel
164 const videoChannelFieldsSave = videoChannelInstance.toJSON()
165 const videoChannelInfoToUpdate = req.body as VideoChannelUpdate
168 await sequelizeTypescript.transaction(async t => {
169 const sequelizeOptions = {
173 if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name)
174 if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description)
175 if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support)
177 const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions)
178 await sendUpdateActor(videoChannelInstanceUpdated, t)
181 logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
183 logger.debug('Cannot update the video channel.', { err })
185 // Force fields we want to update
186 // If the transaction is retried, sequelize will think the object has not changed
187 // So it will skip the SQL request, even if the last one was ROLLBACKed!
188 resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave)
194 async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
196 arguments: [ req, res ],
197 errorMessage: 'Cannot remove the video channel with many retries.'
200 await retryTransactionWrapper(removeVideoChannel, options)
202 return res.type('json').status(204).end()
205 async function removeVideoChannel (req: express.Request, res: express.Response) {
206 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
208 return sequelizeTypescript.transaction(async t => {
209 await videoChannelInstance.destroy({ transaction: t })
211 logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid)
216 async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
217 const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id)
219 return res.json(videoChannelWithVideos.toFormattedJSON())
222 async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
223 const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
225 const resultList = await VideoModel.listForApi({
226 start: req.query.start,
227 count: req.query.count,
228 sort: req.query.sort,
229 hideNSFW: isNSFWHidden(res),
231 videoChannelId: videoChannelInstance.id
234 return res.json(getFormattedObjects(resultList.data, resultList.total))
237 async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
238 const account: AccountModel = res.locals.account
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),
246 accountId: account.id
249 return res.json(getFormattedObjects(resultList.data, resultList.total))