]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Add ability to list all local videos
[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'
687d638c 14import { buildNSFWFilter, isUserAbleToSearchRemoteURI } 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 75 const account: AccountModel = res.locals.account
687d638c 76 const actorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
0626e7af 77
48dce1c9 78 const resultList = await VideoModel.listForApi({
687d638c 79 actorId,
48dce1c9
C
80 start: req.query.start,
81 count: req.query.count,
82 sort: req.query.sort,
8a19bee1 83 includeLocalVideos: true,
d525fc39
C
84 categoryOneOf: req.query.categoryOneOf,
85 licenceOneOf: req.query.licenceOneOf,
86 languageOneOf: req.query.languageOneOf,
87 tagsOneOf: req.query.tagsOneOf,
88 tagsAllOf: req.query.tagsAllOf,
1cd3facc 89 filter: req.query.filter,
d525fc39 90 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 91 withFiles: false,
1cd3facc
C
92 accountId: account.id,
93 userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined
48dce1c9 94 })
0626e7af
C
95
96 return res.json(getFormattedObjects(resultList.data, resultList.total))
97}