1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
4 asyncMiddleware, commonVideosFiltersValidator,
5 listVideoAccountChannelsValidator,
10 } from '../../middlewares'
11 import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
12 import { AccountModel } from '../../models/account/account'
13 import { VideoModel } from '../../models/video/video'
14 import { buildNSFWFilter } from '../../helpers/express-utils'
15 import { VideoChannelModel } from '../../models/video/video-channel'
17 const accountsRouter = express.Router()
19 accountsRouter.get('/',
21 accountsSortValidator,
24 asyncMiddleware(listAccounts)
27 accountsRouter.get('/:accountName',
28 asyncMiddleware(accountsNameWithHostGetValidator),
32 accountsRouter.get('/:accountName/videos',
33 asyncMiddleware(accountsNameWithHostGetValidator),
39 commonVideosFiltersValidator,
40 asyncMiddleware(listAccountVideos)
43 accountsRouter.get('/:accountName/video-channels',
44 asyncMiddleware(listVideoAccountChannelsValidator),
45 asyncMiddleware(listVideoAccountChannels)
48 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
56 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
57 const account: AccountModel = res.locals.account
59 return res.json(account.toFormattedJSON())
62 async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
63 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
65 return res.json(getFormattedObjects(resultList.data, resultList.total))
68 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
69 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
71 return res.json(getFormattedObjects(resultList.data, resultList.total))
74 async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
75 const account: AccountModel = res.locals.account
77 const resultList = await VideoModel.listForApi({
78 start: req.query.start,
79 count: req.query.count,
81 categoryOneOf: req.query.categoryOneOf,
82 licenceOneOf: req.query.licenceOneOf,
83 languageOneOf: req.query.languageOneOf,
84 tagsOneOf: req.query.tagsOneOf,
85 tagsAllOf: req.query.tagsAllOf,
86 nsfw: buildNSFWFilter(res, req.query.nsfw),
91 return res.json(getFormattedObjects(resultList.data, resultList.total))