]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Add subscriptions endpoints to REST API
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
cc918ac3 2import { getFormattedObjects } from '../../helpers/utils'
48dce1c9 3import {
d525fc39 4 asyncMiddleware, commonVideosFiltersValidator,
48dce1c9
C
5 listVideoAccountChannelsValidator,
6 optionalAuthenticate,
7 paginationValidator,
8 setDefaultPagination,
cc918ac3 9 setDefaultSort
48dce1c9 10} from '../../middlewares'
ad9e39fb 11import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
265ba139 12import { AccountModel } from '../../models/account/account'
0626e7af 13import { VideoModel } from '../../models/video/video'
d525fc39 14import { buildNSFWFilter } from '../../helpers/express-utils'
48dce1c9 15import { VideoChannelModel } from '../../models/video/video-channel'
265ba139
C
16
17const accountsRouter = express.Router()
18
19accountsRouter.get('/',
20 paginationValidator,
21 accountsSortValidator,
1174a847 22 setDefaultSort,
f05a1c30 23 setDefaultPagination,
265ba139
C
24 asyncMiddleware(listAccounts)
25)
26
ad9e39fb
C
27accountsRouter.get('/:accountName',
28 asyncMiddleware(accountsNameWithHostGetValidator),
265ba139
C
29 getAccount
30)
31
ad9e39fb
C
32accountsRouter.get('/:accountName/videos',
33 asyncMiddleware(accountsNameWithHostGetValidator),
0626e7af
C
34 paginationValidator,
35 videosSortValidator,
36 setDefaultSort,
37 setDefaultPagination,
38 optionalAuthenticate,
d525fc39 39 commonVideosFiltersValidator,
48dce1c9
C
40 asyncMiddleware(listAccountVideos)
41)
42
ad9e39fb 43accountsRouter.get('/:accountName/video-channels',
48dce1c9
C
44 asyncMiddleware(listVideoAccountChannelsValidator),
45 asyncMiddleware(listVideoAccountChannels)
46)
47
265ba139
C
48// ---------------------------------------------------------------------------
49
50export {
51 accountsRouter
52}
53
54// ---------------------------------------------------------------------------
55
56function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
0626e7af
C
57 const account: AccountModel = res.locals.account
58
59 return res.json(account.toFormattedJSON())
265ba139
C
60}
61
62async 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)
64
65 return res.json(getFormattedObjects(resultList.data, resultList.total))
66}
0626e7af 67
48dce1c9
C
68async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
69 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
70
71 return res.json(getFormattedObjects(resultList.data, resultList.total))
72}
73
48dce1c9 74async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
0626e7af
C
75 const account: AccountModel = res.locals.account
76
48dce1c9
C
77 const resultList = await VideoModel.listForApi({
78 start: req.query.start,
79 count: req.query.count,
80 sort: req.query.sort,
06a05d5f 81 includeLocalVideos: false,
d525fc39
C
82 categoryOneOf: req.query.categoryOneOf,
83 licenceOneOf: req.query.licenceOneOf,
84 languageOneOf: req.query.languageOneOf,
85 tagsOneOf: req.query.tagsOneOf,
86 tagsAllOf: req.query.tagsAllOf,
87 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9
C
88 withFiles: false,
89 accountId: account.id
90 })
0626e7af
C
91
92 return res.json(getFormattedObjects(resultList.data, resultList.total))
93}