]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Optimize comment RSS sql query
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
e1c55031 2import { getFormattedObjects } 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,
a1587156 19 videoChannelsSortValidator,
747c5628
RK
20 videosSortValidator,
21 videoChannelStatsValidator
c100a614 22} from '../../middlewares/validators'
265ba139 23import { AccountModel } from '../../models/account/account'
c100a614 24import { AccountVideoRateModel } from '../../models/account/account-video-rate'
0626e7af 25import { VideoModel } from '../../models/video/video'
a1587156 26import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
48dce1c9 27import { VideoChannelModel } from '../../models/video/video-channel'
744d0eca 28import { JobQueue } from '../../lib/job-queue'
418d092a 29import { VideoPlaylistModel } from '../../models/video/video-playlist'
a1587156 30import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
8dc8a34e 31import { getServerActor } from '@server/models/application/application'
265ba139
C
32
33const accountsRouter = express.Router()
34
35accountsRouter.get('/',
36 paginationValidator,
37 accountsSortValidator,
1174a847 38 setDefaultSort,
f05a1c30 39 setDefaultPagination,
265ba139
C
40 asyncMiddleware(listAccounts)
41)
42
ad9e39fb 43accountsRouter.get('/:accountName',
418d092a 44 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
45 getAccount
46)
47
ad9e39fb 48accountsRouter.get('/:accountName/videos',
418d092a 49 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
50 paginationValidator,
51 videosSortValidator,
52 setDefaultSort,
53 setDefaultPagination,
54 optionalAuthenticate,
d525fc39 55 commonVideosFiltersValidator,
48dce1c9
C
56 asyncMiddleware(listAccountVideos)
57)
58
ad9e39fb 59accountsRouter.get('/:accountName/video-channels',
418d092a 60 asyncMiddleware(accountNameWithHostGetValidator),
747c5628 61 videoChannelStatsValidator,
91b66319
C
62 paginationValidator,
63 videoChannelsSortValidator,
64 setDefaultSort,
65 setDefaultPagination,
418d092a
C
66 asyncMiddleware(listAccountChannels)
67)
68
69accountsRouter.get('/:accountName/video-playlists',
70 optionalAuthenticate,
71 asyncMiddleware(accountNameWithHostGetValidator),
72 paginationValidator,
73 videoPlaylistsSortValidator,
74 setDefaultSort,
75 setDefaultPagination,
df0b219d 76 commonVideoPlaylistFiltersValidator,
c06af501 77 videoPlaylistsSearchValidator,
418d092a 78 asyncMiddleware(listAccountPlaylists)
48dce1c9
C
79)
80
c100a614
YB
81accountsRouter.get('/:accountName/ratings',
82 authenticate,
83 asyncMiddleware(accountNameWithHostGetValidator),
84 ensureAuthUserOwnsAccountValidator,
85 paginationValidator,
86 videoRatesSortValidator,
87 setDefaultSort,
88 setDefaultPagination,
89 videoRatingValidator,
90 asyncMiddleware(listAccountRatings)
91)
92
265ba139
C
93// ---------------------------------------------------------------------------
94
95export {
96 accountsRouter
97}
98
99// ---------------------------------------------------------------------------
100
418d092a 101function getAccount (req: express.Request, res: express.Response) {
dae86118 102 const account = res.locals.account
0626e7af 103
744d0eca
C
104 if (account.isOutdated()) {
105 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
744d0eca
C
106 }
107
0626e7af 108 return res.json(account.toFormattedJSON())
265ba139
C
109}
110
418d092a 111async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
112 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
113
114 return res.json(getFormattedObjects(resultList.data, resultList.total))
115}
0626e7af 116
418d092a 117async function listAccountChannels (req: express.Request, res: express.Response) {
91b66319
C
118 const options = {
119 accountId: res.locals.account.id,
120 start: req.query.start,
121 count: req.query.count,
747c5628 122 sort: req.query.sort,
4f5d0459
RK
123 withStats: req.query.withStats,
124 search: req.query.search
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
6b0c3c7c 136 let listMyPlaylists = false
dae86118 137 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
6b0c3c7c 138 listMyPlaylists = true
418d092a
C
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,
6b0c3c7c 148 listMyPlaylists,
df0b219d 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
fe987656 158 const countVideos = getCountVideos(req)
0626e7af 159
48dce1c9 160 const resultList = await VideoModel.listForApi({
4e74e803 161 followerActorId,
48dce1c9
C
162 start: req.query.start,
163 count: req.query.count,
164 sort: req.query.sort,
8a19bee1 165 includeLocalVideos: true,
d525fc39
C
166 categoryOneOf: req.query.categoryOneOf,
167 licenceOneOf: req.query.licenceOneOf,
168 languageOneOf: req.query.languageOneOf,
169 tagsOneOf: req.query.tagsOneOf,
170 tagsAllOf: req.query.tagsAllOf,
1cd3facc 171 filter: req.query.filter,
d525fc39 172 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 173 withFiles: false,
1cd3facc 174 accountId: account.id,
fe987656
C
175 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
176 countVideos
48dce1c9 177 })
0626e7af
C
178
179 return res.json(getFormattedObjects(resultList.data, resultList.total))
180}
c100a614
YB
181
182async function listAccountRatings (req: express.Request, res: express.Response) {
183 const account = res.locals.account
184
185 const resultList = await AccountVideoRateModel.listByAccountForApi({
186 accountId: account.id,
187 start: req.query.start,
188 count: req.query.count,
189 sort: req.query.sort,
190 type: req.query.rating
191 })
192 return res.json(getFormattedObjects(resultList.rows, resultList.count))
193}