1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
3 import { asyncMiddleware, optionalAuthenticate, paginationValidator, setDefaultPagination, setDefaultSort } from '../../middlewares'
4 import { accountsGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
5 import { AccountModel } from '../../models/account/account'
6 import { VideoModel } from '../../models/video/video'
7 import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type'
8 import { isNSFWHidden } from '../../helpers/express-utils'
10 const accountsRouter = express.Router()
12 accountsRouter.get('/',
14 accountsSortValidator,
17 asyncMiddleware(listAccounts)
20 accountsRouter.get('/:id',
21 asyncMiddleware(accountsGetValidator),
25 accountsRouter.get('/:id/videos',
26 asyncMiddleware(accountsGetValidator),
32 asyncMiddleware(getAccountVideos)
35 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
44 const account: AccountModel = res.locals.account
46 return res.json(account.toFormattedJSON())
49 async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
50 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
52 return res.json(getFormattedObjects(resultList.data, resultList.total))
55 async function getAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
56 const account: AccountModel = res.locals.account
58 const resultList = await VideoModel.listForApi(
59 req.query.start as number,
60 req.query.count as number,
61 req.query.sort as VideoSortField,
68 return res.json(getFormattedObjects(resultList.data, resultList.total))