]>
Commit | Line | Data |
---|---|---|
265ba139 | 1 | import * as express from 'express' |
cc918ac3 | 2 | import { getFormattedObjects } from '../../helpers/utils' |
48dce1c9 C |
3 | import { |
4 | asyncMiddleware, | |
48dce1c9 C |
5 | listVideoAccountChannelsValidator, |
6 | optionalAuthenticate, | |
7 | paginationValidator, | |
8 | setDefaultPagination, | |
cc918ac3 | 9 | setDefaultSort |
48dce1c9 | 10 | } from '../../middlewares' |
ad9e39fb | 11 | import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators' |
265ba139 | 12 | import { AccountModel } from '../../models/account/account' |
0626e7af | 13 | import { VideoModel } from '../../models/video/video' |
0626e7af | 14 | import { isNSFWHidden } from '../../helpers/express-utils' |
48dce1c9 | 15 | import { VideoChannelModel } from '../../models/video/video-channel' |
265ba139 C |
16 | |
17 | const accountsRouter = express.Router() | |
18 | ||
19 | accountsRouter.get('/', | |
20 | paginationValidator, | |
21 | accountsSortValidator, | |
1174a847 | 22 | setDefaultSort, |
f05a1c30 | 23 | setDefaultPagination, |
265ba139 C |
24 | asyncMiddleware(listAccounts) |
25 | ) | |
26 | ||
ad9e39fb C |
27 | accountsRouter.get('/:accountName', |
28 | asyncMiddleware(accountsNameWithHostGetValidator), | |
265ba139 C |
29 | getAccount |
30 | ) | |
31 | ||
ad9e39fb C |
32 | accountsRouter.get('/:accountName/videos', |
33 | asyncMiddleware(accountsNameWithHostGetValidator), | |
0626e7af C |
34 | paginationValidator, |
35 | videosSortValidator, | |
36 | setDefaultSort, | |
37 | setDefaultPagination, | |
38 | optionalAuthenticate, | |
48dce1c9 C |
39 | asyncMiddleware(listAccountVideos) |
40 | ) | |
41 | ||
ad9e39fb | 42 | accountsRouter.get('/:accountName/video-channels', |
48dce1c9 C |
43 | asyncMiddleware(listVideoAccountChannelsValidator), |
44 | asyncMiddleware(listVideoAccountChannels) | |
45 | ) | |
46 | ||
265ba139 C |
47 | // --------------------------------------------------------------------------- |
48 | ||
49 | export { | |
50 | accountsRouter | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) { | |
0626e7af C |
56 | const account: AccountModel = res.locals.account |
57 | ||
58 | return res.json(account.toFormattedJSON()) | |
265ba139 C |
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 | } | |
0626e7af | 66 | |
48dce1c9 C |
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 | ||
48dce1c9 | 73 | async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
0626e7af C |
74 | const account: AccountModel = res.locals.account |
75 | ||
48dce1c9 C |
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 | }) | |
0626e7af C |
84 | |
85 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
86 | } |