]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/accounts.ts
Upgrade server dependencies
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { getFormattedObjects } from '../../helpers/utils'
3import {
4 asyncMiddleware,
5 commonVideosFiltersValidator,
6 listVideoAccountChannelsValidator,
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
10 setDefaultSort
11} from '../../middlewares'
12import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
13import { AccountModel } from '../../models/account/account'
14import { VideoModel } from '../../models/video/video'
15import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
16import { VideoChannelModel } from '../../models/video/video-channel'
17import { JobQueue } from '../../lib/job-queue'
18import { logger } from '../../helpers/logger'
19
20const accountsRouter = express.Router()
21
22accountsRouter.get('/',
23 paginationValidator,
24 accountsSortValidator,
25 setDefaultSort,
26 setDefaultPagination,
27 asyncMiddleware(listAccounts)
28)
29
30accountsRouter.get('/:accountName',
31 asyncMiddleware(accountsNameWithHostGetValidator),
32 getAccount
33)
34
35accountsRouter.get('/:accountName/videos',
36 asyncMiddleware(accountsNameWithHostGetValidator),
37 paginationValidator,
38 videosSortValidator,
39 setDefaultSort,
40 setDefaultPagination,
41 optionalAuthenticate,
42 commonVideosFiltersValidator,
43 asyncMiddleware(listAccountVideos)
44)
45
46accountsRouter.get('/:accountName/video-channels',
47 asyncMiddleware(listVideoAccountChannelsValidator),
48 asyncMiddleware(listVideoAccountChannels)
49)
50
51// ---------------------------------------------------------------------------
52
53export {
54 accountsRouter
55}
56
57// ---------------------------------------------------------------------------
58
59function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
60 const account: AccountModel = res.locals.account
61
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
67 return res.json(account.toFormattedJSON())
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}
75
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
82async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
83 const account: AccountModel = res.locals.account
84 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
85
86 const resultList = await VideoModel.listForApi({
87 followerActorId,
88 start: req.query.start,
89 count: req.query.count,
90 sort: req.query.sort,
91 includeLocalVideos: true,
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,
97 filter: req.query.filter,
98 nsfw: buildNSFWFilter(res, req.query.nsfw),
99 withFiles: false,
100 accountId: account.id,
101 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
102 })
103
104 return res.json(getFormattedObjects(resultList.data, resultList.total))
105}