]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/accounts.ts
Only use account name in routes
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
3 import {
4 asyncMiddleware,
5 listVideoAccountChannelsValidator,
6 optionalAuthenticate,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultSort
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 { isNSFWHidden } from '../../helpers/express-utils'
15 import { VideoChannelModel } from '../../models/video/video-channel'
16
17 const accountsRouter = express.Router()
18
19 accountsRouter.get('/',
20 paginationValidator,
21 accountsSortValidator,
22 setDefaultSort,
23 setDefaultPagination,
24 asyncMiddleware(listAccounts)
25 )
26
27 accountsRouter.get('/:accountName',
28 asyncMiddleware(accountsNameWithHostGetValidator),
29 getAccount
30 )
31
32 accountsRouter.get('/:accountName/videos',
33 asyncMiddleware(accountsNameWithHostGetValidator),
34 paginationValidator,
35 videosSortValidator,
36 setDefaultSort,
37 setDefaultPagination,
38 optionalAuthenticate,
39 asyncMiddleware(listAccountVideos)
40 )
41
42 accountsRouter.get('/:accountName/video-channels',
43 asyncMiddleware(listVideoAccountChannelsValidator),
44 asyncMiddleware(listVideoAccountChannels)
45 )
46
47 // ---------------------------------------------------------------------------
48
49 export {
50 accountsRouter
51 }
52
53 // ---------------------------------------------------------------------------
54
55 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
56 const account: AccountModel = res.locals.account
57
58 return res.json(account.toFormattedJSON())
59 }
60
61 async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
62 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
63
64 return res.json(getFormattedObjects(resultList.data, resultList.total))
65 }
66
67 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
68 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
69
70 return res.json(getFormattedObjects(resultList.data, resultList.total))
71 }
72
73 async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
74 const account: AccountModel = res.locals.account
75
76 const resultList = await VideoModel.listForApi({
77 start: req.query.start,
78 count: req.query.count,
79 sort: req.query.sort,
80 hideNSFW: isNSFWHidden(res),
81 withFiles: false,
82 accountId: account.id
83 })
84
85 return res.json(getFormattedObjects(resultList.data, resultList.total))
86 }