]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/accounts.ts
Add check params account ratings tests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { getFormattedObjects, getServerActor } from '../../helpers/utils'
3import {
4 asyncMiddleware,
5 authenticate,
6 commonVideosFiltersValidator,
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
10 setDefaultSort,
11 videoPlaylistsSortValidator,
12 videoRatesSortValidator,
13 videoRatingValidator
14} from '../../middlewares'
15import {
16 accountNameWithHostGetValidator,
17 accountsSortValidator,
18 ensureAuthUserOwnsAccountValidator,
19 videosSortValidator
20} from '../../middlewares/validators'
21import { AccountModel } from '../../models/account/account'
22import { AccountVideoRateModel } from '../../models/account/account-video-rate'
23import { VideoModel } from '../../models/video/video'
24import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
25import { VideoChannelModel } from '../../models/video/video-channel'
26import { JobQueue } from '../../lib/job-queue'
27import { logger } from '../../helpers/logger'
28import { VideoPlaylistModel } from '../../models/video/video-playlist'
29import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
30
31const accountsRouter = express.Router()
32
33accountsRouter.get('/',
34 paginationValidator,
35 accountsSortValidator,
36 setDefaultSort,
37 setDefaultPagination,
38 asyncMiddleware(listAccounts)
39)
40
41accountsRouter.get('/:accountName',
42 asyncMiddleware(accountNameWithHostGetValidator),
43 getAccount
44)
45
46accountsRouter.get('/:accountName/videos',
47 asyncMiddleware(accountNameWithHostGetValidator),
48 paginationValidator,
49 videosSortValidator,
50 setDefaultSort,
51 setDefaultPagination,
52 optionalAuthenticate,
53 commonVideosFiltersValidator,
54 asyncMiddleware(listAccountVideos)
55)
56
57accountsRouter.get('/:accountName/video-channels',
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,
69 commonVideoPlaylistFiltersValidator,
70 asyncMiddleware(listAccountPlaylists)
71)
72
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
85// ---------------------------------------------------------------------------
86
87export {
88 accountsRouter
89}
90
91// ---------------------------------------------------------------------------
92
93function getAccount (req: express.Request, res: express.Response) {
94 const account = res.locals.account
95
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
101 return res.json(account.toFormattedJSON())
102}
103
104async function listAccounts (req: express.Request, res: express.Response) {
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}
109
110async function listAccountChannels (req: express.Request, res: express.Response) {
111 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
112
113 return res.json(getFormattedObjects(resultList.data, resultList.total))
114}
115
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
121 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
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,
131 privateAndUnlisted,
132 type: req.query.playlistType
133 })
134
135 return res.json(getFormattedObjects(resultList.data, resultList.total))
136}
137
138async function listAccountVideos (req: express.Request, res: express.Response) {
139 const account = res.locals.account
140 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
141
142 const resultList = await VideoModel.listForApi({
143 followerActorId,
144 start: req.query.start,
145 count: req.query.count,
146 sort: req.query.sort,
147 includeLocalVideos: true,
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,
153 filter: req.query.filter,
154 nsfw: buildNSFWFilter(res, req.query.nsfw),
155 withFiles: false,
156 accountId: account.id,
157 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
158 })
159
160 return res.json(getFormattedObjects(resultList.data, resultList.total))
161}
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}