]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Add ability for users to block an account/instance on server side
[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'
265ba139
C
17
18const accountsRouter = express.Router()
19
20accountsRouter.get('/',
21 paginationValidator,
22 accountsSortValidator,
1174a847 23 setDefaultSort,
f05a1c30 24 setDefaultPagination,
265ba139
C
25 asyncMiddleware(listAccounts)
26)
27
ad9e39fb
C
28accountsRouter.get('/:accountName',
29 asyncMiddleware(accountsNameWithHostGetValidator),
265ba139
C
30 getAccount
31)
32
ad9e39fb
C
33accountsRouter.get('/:accountName/videos',
34 asyncMiddleware(accountsNameWithHostGetValidator),
0626e7af
C
35 paginationValidator,
36 videosSortValidator,
37 setDefaultSort,
38 setDefaultPagination,
39 optionalAuthenticate,
d525fc39 40 commonVideosFiltersValidator,
48dce1c9
C
41 asyncMiddleware(listAccountVideos)
42)
43
ad9e39fb 44accountsRouter.get('/:accountName/video-channels',
48dce1c9
C
45 asyncMiddleware(listVideoAccountChannelsValidator),
46 asyncMiddleware(listVideoAccountChannels)
47)
48
265ba139
C
49// ---------------------------------------------------------------------------
50
51export {
52 accountsRouter
53}
54
55// ---------------------------------------------------------------------------
56
57function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
0626e7af
C
58 const account: AccountModel = res.locals.account
59
60 return res.json(account.toFormattedJSON())
265ba139
C
61}
62
63async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
64 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
65
66 return res.json(getFormattedObjects(resultList.data, resultList.total))
67}
0626e7af 68
48dce1c9
C
69async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
70 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
71
72 return res.json(getFormattedObjects(resultList.data, resultList.total))
73}
74
48dce1c9 75async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
0626e7af 76 const account: AccountModel = res.locals.account
687d638c 77 const actorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
0626e7af 78
48dce1c9 79 const resultList = await VideoModel.listForApi({
687d638c 80 actorId,
48dce1c9
C
81 start: req.query.start,
82 count: req.query.count,
83 sort: req.query.sort,
8a19bee1 84 includeLocalVideos: true,
d525fc39
C
85 categoryOneOf: req.query.categoryOneOf,
86 licenceOneOf: req.query.licenceOneOf,
87 languageOneOf: req.query.languageOneOf,
88 tagsOneOf: req.query.tagsOneOf,
89 tagsAllOf: req.query.tagsAllOf,
1cd3facc 90 filter: req.query.filter,
d525fc39 91 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 92 withFiles: false,
1cd3facc 93 accountId: account.id,
7ad9b984 94 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
48dce1c9 95 })
0626e7af
C
96
97 return res.json(getFormattedObjects(resultList.data, resultList.total))
98}