]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Add check params account ratings tests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
418d092a 2import { getFormattedObjects, getServerActor } from '../../helpers/utils'
48dce1c9 3import {
7ad9b984 4 asyncMiddleware,
22834691 5 authenticate,
7ad9b984 6 commonVideosFiltersValidator,
48dce1c9
C
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
418d092a 10 setDefaultSort,
c100a614 11 videoPlaylistsSortValidator,
22834691
C
12 videoRatesSortValidator,
13 videoRatingValidator
48dce1c9 14} from '../../middlewares'
c100a614
YB
15import {
16 accountNameWithHostGetValidator,
17 accountsSortValidator,
22834691
C
18 ensureAuthUserOwnsAccountValidator,
19 videosSortValidator
c100a614 20} from '../../middlewares/validators'
265ba139 21import { AccountModel } from '../../models/account/account'
c100a614 22import { AccountVideoRateModel } from '../../models/account/account-video-rate'
0626e7af 23import { VideoModel } from '../../models/video/video'
687d638c 24import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
48dce1c9 25import { VideoChannelModel } from '../../models/video/video-channel'
744d0eca
C
26import { JobQueue } from '../../lib/job-queue'
27import { logger } from '../../helpers/logger'
418d092a 28import { VideoPlaylistModel } from '../../models/video/video-playlist'
df0b219d 29import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
265ba139
C
30
31const accountsRouter = express.Router()
32
33accountsRouter.get('/',
34 paginationValidator,
35 accountsSortValidator,
1174a847 36 setDefaultSort,
f05a1c30 37 setDefaultPagination,
265ba139
C
38 asyncMiddleware(listAccounts)
39)
40
ad9e39fb 41accountsRouter.get('/:accountName',
418d092a 42 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
43 getAccount
44)
45
ad9e39fb 46accountsRouter.get('/:accountName/videos',
418d092a 47 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
48 paginationValidator,
49 videosSortValidator,
50 setDefaultSort,
51 setDefaultPagination,
52 optionalAuthenticate,
d525fc39 53 commonVideosFiltersValidator,
48dce1c9
C
54 asyncMiddleware(listAccountVideos)
55)
56
ad9e39fb 57accountsRouter.get('/:accountName/video-channels',
418d092a
C
58 asyncMiddleware(accountNameWithHostGetValidator),
59 asyncMiddleware(listAccountChannels)
60)
61
62accountsRouter.get('/:accountName/video-playlists',
63 optionalAuthenticate,
64 asyncMiddleware(accountNameWithHostGetValidator),
65 paginationValidator,
66 videoPlaylistsSortValidator,
67 setDefaultSort,
68 setDefaultPagination,
df0b219d 69 commonVideoPlaylistFiltersValidator,
418d092a 70 asyncMiddleware(listAccountPlaylists)
48dce1c9
C
71)
72
c100a614
YB
73accountsRouter.get('/:accountName/ratings',
74 authenticate,
75 asyncMiddleware(accountNameWithHostGetValidator),
76 ensureAuthUserOwnsAccountValidator,
77 paginationValidator,
78 videoRatesSortValidator,
79 setDefaultSort,
80 setDefaultPagination,
81 videoRatingValidator,
82 asyncMiddleware(listAccountRatings)
83)
84
265ba139
C
85// ---------------------------------------------------------------------------
86
87export {
88 accountsRouter
89}
90
91// ---------------------------------------------------------------------------
92
418d092a 93function getAccount (req: express.Request, res: express.Response) {
dae86118 94 const account = res.locals.account
0626e7af 95
744d0eca
C
96 if (account.isOutdated()) {
97 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
98 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
99 }
100
0626e7af 101 return res.json(account.toFormattedJSON())
265ba139
C
102}
103
418d092a 104async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
105 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
106
107 return res.json(getFormattedObjects(resultList.data, resultList.total))
108}
0626e7af 109
418d092a 110async function listAccountChannels (req: express.Request, res: express.Response) {
48dce1c9
C
111 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
112
113 return res.json(getFormattedObjects(resultList.data, resultList.total))
114}
115
418d092a
C
116async function listAccountPlaylists (req: express.Request, res: express.Response) {
117 const serverActor = await getServerActor()
118
119 // Allow users to see their private/unlisted video playlists
120 let privateAndUnlisted = false
dae86118 121 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
418d092a
C
122 privateAndUnlisted = true
123 }
124
125 const resultList = await VideoPlaylistModel.listForApi({
126 followerActorId: serverActor.id,
127 start: req.query.start,
128 count: req.query.count,
129 sort: req.query.sort,
130 accountId: res.locals.account.id,
df0b219d
C
131 privateAndUnlisted,
132 type: req.query.playlistType
418d092a
C
133 })
134
135 return res.json(getFormattedObjects(resultList.data, resultList.total))
136}
137
138async function listAccountVideos (req: express.Request, res: express.Response) {
dae86118 139 const account = res.locals.account
4e74e803 140 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
0626e7af 141
48dce1c9 142 const resultList = await VideoModel.listForApi({
4e74e803 143 followerActorId,
48dce1c9
C
144 start: req.query.start,
145 count: req.query.count,
146 sort: req.query.sort,
8a19bee1 147 includeLocalVideos: true,
d525fc39
C
148 categoryOneOf: req.query.categoryOneOf,
149 licenceOneOf: req.query.licenceOneOf,
150 languageOneOf: req.query.languageOneOf,
151 tagsOneOf: req.query.tagsOneOf,
152 tagsAllOf: req.query.tagsAllOf,
1cd3facc 153 filter: req.query.filter,
d525fc39 154 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 155 withFiles: false,
1cd3facc 156 accountId: account.id,
7ad9b984 157 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
48dce1c9 158 })
0626e7af
C
159
160 return res.json(getFormattedObjects(resultList.data, resultList.total))
161}
c100a614
YB
162
163async function listAccountRatings (req: express.Request, res: express.Response) {
164 const account = res.locals.account
165
166 const resultList = await AccountVideoRateModel.listByAccountForApi({
167 accountId: account.id,
168 start: req.query.start,
169 count: req.query.count,
170 sort: req.query.sort,
171 type: req.query.rating
172 })
173 return res.json(getFormattedObjects(resultList.rows, resultList.count))
174}