]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Fix runner api rate limit bypass
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
41fb13c3 1import express from 'express'
d6886027 2import { pickCommonVideoQuery } from '@server/helpers/query'
4beda9e1 3import { ActorFollowModel } from '@server/models/actor/actor-follow'
8054669f 4import { getServerActor } from '@server/models/application/application'
2760b454 5import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
d4d9bbc6 6import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
8054669f 7import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
e1c55031 8import { getFormattedObjects } from '../../helpers/utils'
8054669f 9import { JobQueue } from '../../lib/job-queue'
1fd61899 10import { Hooks } from '../../lib/plugins/hooks'
48dce1c9 11import {
e915cde3 12 apiRateLimiter,
7ad9b984 13 asyncMiddleware,
22834691 14 authenticate,
7ad9b984 15 commonVideosFiltersValidator,
48dce1c9
C
16 optionalAuthenticate,
17 paginationValidator,
18 setDefaultPagination,
418d092a 19 setDefaultSort,
8054669f 20 setDefaultVideosSort,
c100a614 21 videoPlaylistsSortValidator,
22834691
C
22 videoRatesSortValidator,
23 videoRatingValidator
48dce1c9 24} from '../../middlewares'
c100a614
YB
25import {
26 accountNameWithHostGetValidator,
4beda9e1 27 accountsFollowersSortValidator,
c100a614 28 accountsSortValidator,
22834691 29 ensureAuthUserOwnsAccountValidator,
d4d9bbc6 30 ensureCanManageChannelOrAccount,
a1587156 31 videoChannelsSortValidator,
8054669f 32 videoChannelStatsValidator,
2a491182 33 videoChannelSyncsSortValidator,
8054669f 34 videosSortValidator
c100a614 35} from '../../middlewares/validators'
8054669f 36import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
265ba139 37import { AccountModel } from '../../models/account/account'
c100a614 38import { AccountVideoRateModel } from '../../models/account/account-video-rate'
0626e7af 39import { VideoModel } from '../../models/video/video'
48dce1c9 40import { VideoChannelModel } from '../../models/video/video-channel'
418d092a 41import { VideoPlaylistModel } from '../../models/video/video-playlist'
265ba139
C
42
43const accountsRouter = express.Router()
44
e915cde3
C
45accountsRouter.use(apiRateLimiter)
46
265ba139
C
47accountsRouter.get('/',
48 paginationValidator,
49 accountsSortValidator,
1174a847 50 setDefaultSort,
f05a1c30 51 setDefaultPagination,
265ba139
C
52 asyncMiddleware(listAccounts)
53)
54
ad9e39fb 55accountsRouter.get('/:accountName',
418d092a 56 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
57 getAccount
58)
59
ad9e39fb 60accountsRouter.get('/:accountName/videos',
418d092a 61 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
62 paginationValidator,
63 videosSortValidator,
8054669f 64 setDefaultVideosSort,
0626e7af
C
65 setDefaultPagination,
66 optionalAuthenticate,
d525fc39 67 commonVideosFiltersValidator,
48dce1c9
C
68 asyncMiddleware(listAccountVideos)
69)
70
ad9e39fb 71accountsRouter.get('/:accountName/video-channels',
418d092a 72 asyncMiddleware(accountNameWithHostGetValidator),
747c5628 73 videoChannelStatsValidator,
91b66319
C
74 paginationValidator,
75 videoChannelsSortValidator,
76 setDefaultSort,
77 setDefaultPagination,
418d092a
C
78 asyncMiddleware(listAccountChannels)
79)
80
2a491182
F
81accountsRouter.get('/:accountName/video-channel-syncs',
82 authenticate,
83 asyncMiddleware(accountNameWithHostGetValidator),
d4d9bbc6 84 ensureCanManageChannelOrAccount,
2a491182
F
85 paginationValidator,
86 videoChannelSyncsSortValidator,
87 setDefaultSort,
88 setDefaultPagination,
89 asyncMiddleware(listAccountChannelsSync)
90)
91
418d092a
C
92accountsRouter.get('/:accountName/video-playlists',
93 optionalAuthenticate,
94 asyncMiddleware(accountNameWithHostGetValidator),
95 paginationValidator,
96 videoPlaylistsSortValidator,
97 setDefaultSort,
98 setDefaultPagination,
df0b219d 99 commonVideoPlaylistFiltersValidator,
c06af501 100 videoPlaylistsSearchValidator,
418d092a 101 asyncMiddleware(listAccountPlaylists)
48dce1c9
C
102)
103
c100a614
YB
104accountsRouter.get('/:accountName/ratings',
105 authenticate,
106 asyncMiddleware(accountNameWithHostGetValidator),
107 ensureAuthUserOwnsAccountValidator,
108 paginationValidator,
109 videoRatesSortValidator,
110 setDefaultSort,
111 setDefaultPagination,
112 videoRatingValidator,
113 asyncMiddleware(listAccountRatings)
114)
115
4beda9e1
C
116accountsRouter.get('/:accountName/followers',
117 authenticate,
118 asyncMiddleware(accountNameWithHostGetValidator),
119 ensureAuthUserOwnsAccountValidator,
120 paginationValidator,
121 accountsFollowersSortValidator,
122 setDefaultSort,
123 setDefaultPagination,
124 asyncMiddleware(listAccountFollowers)
125)
126
265ba139
C
127// ---------------------------------------------------------------------------
128
129export {
130 accountsRouter
131}
132
133// ---------------------------------------------------------------------------
134
418d092a 135function getAccount (req: express.Request, res: express.Response) {
dae86118 136 const account = res.locals.account
0626e7af 137
744d0eca 138 if (account.isOutdated()) {
bd911b54 139 JobQueue.Instance.createJobAsync({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
744d0eca
C
140 }
141
0626e7af 142 return res.json(account.toFormattedJSON())
265ba139
C
143}
144
418d092a 145async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
146 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
147
148 return res.json(getFormattedObjects(resultList.data, resultList.total))
149}
0626e7af 150
418d092a 151async function listAccountChannels (req: express.Request, res: express.Response) {
91b66319
C
152 const options = {
153 accountId: res.locals.account.id,
154 start: req.query.start,
155 count: req.query.count,
747c5628 156 sort: req.query.sort,
4f5d0459
RK
157 withStats: req.query.withStats,
158 search: req.query.search
91b66319
C
159 }
160
4beda9e1 161 const resultList = await VideoChannelModel.listByAccountForAPI(options)
48dce1c9
C
162
163 return res.json(getFormattedObjects(resultList.data, resultList.total))
164}
165
2a491182
F
166async function listAccountChannelsSync (req: express.Request, res: express.Response) {
167 const options = {
168 accountId: res.locals.account.id,
169 start: req.query.start,
170 count: req.query.count,
171 sort: req.query.sort,
172 search: req.query.search
173 }
174
175 const resultList = await VideoChannelSyncModel.listByAccountForAPI(options)
176
177 return res.json(getFormattedObjects(resultList.data, resultList.total))
178}
179
418d092a
C
180async function listAccountPlaylists (req: express.Request, res: express.Response) {
181 const serverActor = await getServerActor()
182
183 // Allow users to see their private/unlisted video playlists
6b0c3c7c 184 let listMyPlaylists = false
dae86118 185 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
6b0c3c7c 186 listMyPlaylists = true
418d092a
C
187 }
188
189 const resultList = await VideoPlaylistModel.listForApi({
c06af501 190 search: req.query.search,
418d092a
C
191 followerActorId: serverActor.id,
192 start: req.query.start,
193 count: req.query.count,
194 sort: req.query.sort,
195 accountId: res.locals.account.id,
6b0c3c7c 196 listMyPlaylists,
df0b219d 197 type: req.query.playlistType
418d092a
C
198 })
199
200 return res.json(getFormattedObjects(resultList.data, resultList.total))
201}
202
203async function listAccountVideos (req: express.Request, res: express.Response) {
2760b454
C
204 const serverActor = await getServerActor()
205
dae86118 206 const account = res.locals.account
2760b454
C
207
208 const displayOnlyForFollower = isUserAbleToSearchRemoteURI(res)
209 ? null
210 : {
211 actorId: serverActor.id,
212 orLocalVideos: true
213 }
214
fe987656 215 const countVideos = getCountVideos(req)
d6886027 216 const query = pickCommonVideoQuery(req.query)
0626e7af 217
1bfc07e4 218 const apiOptions = await Hooks.wrapObject({
d6886027
C
219 ...query,
220
2760b454 221 displayOnlyForFollower,
1fd61899 222 nsfw: buildNSFWFilter(res, query.nsfw),
1cd3facc 223 accountId: account.id,
fe987656 224 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
d6886027 225 countVideos
38267c0c 226 }, 'filter:api.accounts.videos.list.params')
1bfc07e4 227
228 const resultList = await Hooks.wrapPromiseFun(
229 VideoModel.listForApi,
230 apiOptions,
38267c0c 231 'filter:api.accounts.videos.list.result'
1bfc07e4 232 )
0626e7af 233
2760b454 234 return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
0626e7af 235}
c100a614
YB
236
237async function listAccountRatings (req: express.Request, res: express.Response) {
238 const account = res.locals.account
239
240 const resultList = await AccountVideoRateModel.listByAccountForApi({
241 accountId: account.id,
242 start: req.query.start,
243 count: req.query.count,
244 sort: req.query.sort,
245 type: req.query.rating
246 })
d0800f76 247 return res.json(getFormattedObjects(resultList.data, resultList.total))
c100a614 248}
4beda9e1
C
249
250async function listAccountFollowers (req: express.Request, res: express.Response) {
251 const account = res.locals.account
252
253 const channels = await VideoChannelModel.listAllByAccount(account.id)
254 const actorIds = [ account.actorId ].concat(channels.map(c => c.actorId))
255
256 const resultList = await ActorFollowModel.listFollowersForApi({
257 actorIds,
258 start: req.query.start,
259 count: req.query.count,
260 sort: req.query.sort,
261 search: req.query.search,
906f46d0 262 state: 'accepted'
4beda9e1
C
263 })
264
265 return res.json(getFormattedObjects(resultList.data, resultList.total))
266}