]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/accounts.ts
Add account view
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
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'
9
10 const accountsRouter = express.Router()
11
12 accountsRouter.get('/',
13 paginationValidator,
14 accountsSortValidator,
15 setDefaultSort,
16 setDefaultPagination,
17 asyncMiddleware(listAccounts)
18 )
19
20 accountsRouter.get('/:id',
21 asyncMiddleware(accountsGetValidator),
22 getAccount
23 )
24
25 accountsRouter.get('/:id/videos',
26 asyncMiddleware(accountsGetValidator),
27 paginationValidator,
28 videosSortValidator,
29 setDefaultSort,
30 setDefaultPagination,
31 optionalAuthenticate,
32 asyncMiddleware(getAccountVideos)
33 )
34
35 // ---------------------------------------------------------------------------
36
37 export {
38 accountsRouter
39 }
40
41 // ---------------------------------------------------------------------------
42
43 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
44 const account: AccountModel = res.locals.account
45
46 return res.json(account.toFormattedJSON())
47 }
48
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)
51
52 return res.json(getFormattedObjects(resultList.data, resultList.total))
53 }
54
55 async function getAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
56 const account: AccountModel = res.locals.account
57
58 const resultList = await VideoModel.listForApi(
59 req.query.start as number,
60 req.query.count as number,
61 req.query.sort as VideoSortField,
62 isNSFWHidden(res),
63 null,
64 false,
65 account.id
66 )
67
68 return res.json(getFormattedObjects(resultList.data, resultList.total))
69 }