]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/accounts.ts
Merge branch 'move-utils-to-shared' of https://github.com/buoyantair/PeerTube into...
[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 commonVideosFiltersValidator,
6 listVideoAccountChannelsValidator,
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
10 setDefaultSort
11 } from '../../middlewares'
12 import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
13 import { AccountModel } from '../../models/account/account'
14 import { VideoModel } from '../../models/video/video'
15 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
16 import { VideoChannelModel } from '../../models/video/video-channel'
17
18 const accountsRouter = express.Router()
19
20 accountsRouter.get('/',
21 paginationValidator,
22 accountsSortValidator,
23 setDefaultSort,
24 setDefaultPagination,
25 asyncMiddleware(listAccounts)
26 )
27
28 accountsRouter.get('/:accountName',
29 asyncMiddleware(accountsNameWithHostGetValidator),
30 getAccount
31 )
32
33 accountsRouter.get('/:accountName/videos',
34 asyncMiddleware(accountsNameWithHostGetValidator),
35 paginationValidator,
36 videosSortValidator,
37 setDefaultSort,
38 setDefaultPagination,
39 optionalAuthenticate,
40 commonVideosFiltersValidator,
41 asyncMiddleware(listAccountVideos)
42 )
43
44 accountsRouter.get('/:accountName/video-channels',
45 asyncMiddleware(listVideoAccountChannelsValidator),
46 asyncMiddleware(listVideoAccountChannels)
47 )
48
49 // ---------------------------------------------------------------------------
50
51 export {
52 accountsRouter
53 }
54
55 // ---------------------------------------------------------------------------
56
57 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
58 const account: AccountModel = res.locals.account
59
60 return res.json(account.toFormattedJSON())
61 }
62
63 async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
64 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
65
66 return res.json(getFormattedObjects(resultList.data, resultList.total))
67 }
68
69 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
70 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
71
72 return res.json(getFormattedObjects(resultList.data, resultList.total))
73 }
74
75 async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
76 const account: AccountModel = res.locals.account
77 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
78
79 const resultList = await VideoModel.listForApi({
80 followerActorId,
81 start: req.query.start,
82 count: req.query.count,
83 sort: req.query.sort,
84 includeLocalVideos: true,
85 categoryOneOf: req.query.categoryOneOf,
86 licenceOneOf: req.query.licenceOneOf,
87 languageOneOf: req.query.languageOneOf,
88 tagsOneOf: req.query.tagsOneOf,
89 tagsAllOf: req.query.tagsAllOf,
90 filter: req.query.filter,
91 nsfw: buildNSFWFilter(res, req.query.nsfw),
92 withFiles: false,
93 accountId: account.id,
94 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
95 })
96
97 return res.json(getFormattedObjects(resultList.data, resultList.total))
98 }