Merge branch 'release/v1.0.0' into develop
[oweals/peertube.git] / server / controllers / api / accounts.ts
1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
3 import {
4   asyncMiddleware, commonVideosFiltersValidator,
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 { buildNSFWFilter, isUserAbleToSearchRemoteURI } 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   commonVideosFiltersValidator,
40   asyncMiddleware(listAccountVideos)
41 )
42
43 accountsRouter.get('/:accountName/video-channels',
44   asyncMiddleware(listVideoAccountChannelsValidator),
45   asyncMiddleware(listVideoAccountChannels)
46 )
47
48 // ---------------------------------------------------------------------------
49
50 export {
51   accountsRouter
52 }
53
54 // ---------------------------------------------------------------------------
55
56 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
57   const account: AccountModel = res.locals.account
58
59   return res.json(account.toFormattedJSON())
60 }
61
62 async 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 }
67
68 async 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
74 async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
75   const account: AccountModel = res.locals.account
76   const actorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
77
78   const resultList = await VideoModel.listForApi({
79     actorId,
80     start: req.query.start,
81     count: req.query.count,
82     sort: req.query.sort,
83     includeLocalVideos: true,
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,
89     filter: req.query.filter,
90     nsfw: buildNSFWFilter(res, req.query.nsfw),
91     withFiles: false,
92     accountId: account.id,
93     userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined
94   })
95
96   return res.json(getFormattedObjects(resultList.data, resultList.total))
97 }