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