]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/accounts.ts
8c02372030c53961cedc96ee23c866542bd2926e
[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 import { JobQueue } from '../../lib/job-queue'
18 import { logger } from '../../helpers/logger'
19
20 const accountsRouter = express.Router()
21
22 accountsRouter.get('/',
23 paginationValidator,
24 accountsSortValidator,
25 setDefaultSort,
26 setDefaultPagination,
27 asyncMiddleware(listAccounts)
28 )
29
30 accountsRouter.get('/:accountName',
31 asyncMiddleware(accountsNameWithHostGetValidator),
32 getAccount
33 )
34
35 accountsRouter.get('/:accountName/videos',
36 asyncMiddleware(accountsNameWithHostGetValidator),
37 paginationValidator,
38 videosSortValidator,
39 setDefaultSort,
40 setDefaultPagination,
41 optionalAuthenticate,
42 commonVideosFiltersValidator,
43 asyncMiddleware(listAccountVideos)
44 )
45
46 accountsRouter.get('/:accountName/video-channels',
47 asyncMiddleware(listVideoAccountChannelsValidator),
48 asyncMiddleware(listVideoAccountChannels)
49 )
50
51 // ---------------------------------------------------------------------------
52
53 export {
54 accountsRouter
55 }
56
57 // ---------------------------------------------------------------------------
58
59 function 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
70 async 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
76 async 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
82 async 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 }