]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Upgrade server dependencies
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
cc918ac3 2import { getFormattedObjects } from '../../helpers/utils'
48dce1c9 3import {
7ad9b984
C
4 asyncMiddleware,
5 commonVideosFiltersValidator,
48dce1c9
C
6 listVideoAccountChannelsValidator,
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
cc918ac3 10 setDefaultSort
48dce1c9 11} from '../../middlewares'
ad9e39fb 12import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
265ba139 13import { AccountModel } from '../../models/account/account'
0626e7af 14import { VideoModel } from '../../models/video/video'
687d638c 15import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
48dce1c9 16import { VideoChannelModel } from '../../models/video/video-channel'
744d0eca
C
17import { JobQueue } from '../../lib/job-queue'
18import { logger } from '../../helpers/logger'
265ba139
C
19
20const accountsRouter = express.Router()
21
22accountsRouter.get('/',
23 paginationValidator,
24 accountsSortValidator,
1174a847 25 setDefaultSort,
f05a1c30 26 setDefaultPagination,
265ba139
C
27 asyncMiddleware(listAccounts)
28)
29
ad9e39fb
C
30accountsRouter.get('/:accountName',
31 asyncMiddleware(accountsNameWithHostGetValidator),
265ba139
C
32 getAccount
33)
34
ad9e39fb
C
35accountsRouter.get('/:accountName/videos',
36 asyncMiddleware(accountsNameWithHostGetValidator),
0626e7af
C
37 paginationValidator,
38 videosSortValidator,
39 setDefaultSort,
40 setDefaultPagination,
41 optionalAuthenticate,
d525fc39 42 commonVideosFiltersValidator,
48dce1c9
C
43 asyncMiddleware(listAccountVideos)
44)
45
ad9e39fb 46accountsRouter.get('/:accountName/video-channels',
48dce1c9
C
47 asyncMiddleware(listVideoAccountChannelsValidator),
48 asyncMiddleware(listVideoAccountChannels)
49)
50
265ba139
C
51// ---------------------------------------------------------------------------
52
53export {
54 accountsRouter
55}
56
57// ---------------------------------------------------------------------------
58
59function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
0626e7af
C
60 const account: AccountModel = res.locals.account
61
744d0eca
C
62 if (account.isOutdated()) {
63 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
64 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
65 }
66
0626e7af 67 return res.json(account.toFormattedJSON())
265ba139
C
68}
69
70async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
71 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
72
73 return res.json(getFormattedObjects(resultList.data, resultList.total))
74}
0626e7af 75
48dce1c9
C
76async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
77 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
78
79 return res.json(getFormattedObjects(resultList.data, resultList.total))
80}
81
48dce1c9 82async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
0626e7af 83 const account: AccountModel = res.locals.account
4e74e803 84 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
0626e7af 85
48dce1c9 86 const resultList = await VideoModel.listForApi({
4e74e803 87 followerActorId,
48dce1c9
C
88 start: req.query.start,
89 count: req.query.count,
90 sort: req.query.sort,
8a19bee1 91 includeLocalVideos: true,
d525fc39
C
92 categoryOneOf: req.query.categoryOneOf,
93 licenceOneOf: req.query.licenceOneOf,
94 languageOneOf: req.query.languageOneOf,
95 tagsOneOf: req.query.tagsOneOf,
96 tagsAllOf: req.query.tagsAllOf,
1cd3facc 97 filter: req.query.filter,
d525fc39 98 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 99 withFiles: false,
1cd3facc 100 accountId: account.id,
7ad9b984 101 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
48dce1c9 102 })
0626e7af
C
103
104 return res.json(getFormattedObjects(resultList.data, resultList.total))
105}