]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/accounts.ts
Add explicit error message that changing video ownership only works with local accou...
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { getFormattedObjects } from '../../helpers/utils'
3import {
4 asyncMiddleware, commonVideosFiltersValidator,
5 listVideoAccountChannelsValidator,
6 optionalAuthenticate,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultSort
10} from '../../middlewares'
11import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
12import { AccountModel } from '../../models/account/account'
13import { VideoModel } from '../../models/video/video'
14import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
15import { VideoChannelModel } from '../../models/video/video-channel'
16
17const accountsRouter = express.Router()
18
19accountsRouter.get('/',
20 paginationValidator,
21 accountsSortValidator,
22 setDefaultSort,
23 setDefaultPagination,
24 asyncMiddleware(listAccounts)
25)
26
27accountsRouter.get('/:accountName',
28 asyncMiddleware(accountsNameWithHostGetValidator),
29 getAccount
30)
31
32accountsRouter.get('/:accountName/videos',
33 asyncMiddleware(accountsNameWithHostGetValidator),
34 paginationValidator,
35 videosSortValidator,
36 setDefaultSort,
37 setDefaultPagination,
38 optionalAuthenticate,
39 commonVideosFiltersValidator,
40 asyncMiddleware(listAccountVideos)
41)
42
43accountsRouter.get('/:accountName/video-channels',
44 asyncMiddleware(listVideoAccountChannelsValidator),
45 asyncMiddleware(listVideoAccountChannels)
46)
47
48// ---------------------------------------------------------------------------
49
50export {
51 accountsRouter
52}
53
54// ---------------------------------------------------------------------------
55
56function 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
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}
67
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
74async 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 nsfw: buildNSFWFilter(res, req.query.nsfw),
90 withFiles: false,
91 accountId: account.id
92 })
93
94 return res.json(getFormattedObjects(resultList.data, resultList.total))
95}