]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Optimize default sort when listing videos
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
8054669f
C
2import { getServerActor } from '@server/models/application/application'
3import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
e1c55031 4import { getFormattedObjects } from '../../helpers/utils'
8054669f 5import { JobQueue } from '../../lib/job-queue'
48dce1c9 6import {
7ad9b984 7 asyncMiddleware,
22834691 8 authenticate,
7ad9b984 9 commonVideosFiltersValidator,
48dce1c9
C
10 optionalAuthenticate,
11 paginationValidator,
12 setDefaultPagination,
418d092a 13 setDefaultSort,
8054669f 14 setDefaultVideosSort,
c100a614 15 videoPlaylistsSortValidator,
22834691
C
16 videoRatesSortValidator,
17 videoRatingValidator
48dce1c9 18} from '../../middlewares'
c100a614
YB
19import {
20 accountNameWithHostGetValidator,
21 accountsSortValidator,
22834691 22 ensureAuthUserOwnsAccountValidator,
a1587156 23 videoChannelsSortValidator,
8054669f
C
24 videoChannelStatsValidator,
25 videosSortValidator
c100a614 26} from '../../middlewares/validators'
8054669f 27import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
265ba139 28import { AccountModel } from '../../models/account/account'
c100a614 29import { AccountVideoRateModel } from '../../models/account/account-video-rate'
0626e7af 30import { VideoModel } from '../../models/video/video'
48dce1c9 31import { VideoChannelModel } from '../../models/video/video-channel'
418d092a 32import { VideoPlaylistModel } from '../../models/video/video-playlist'
265ba139
C
33
34const accountsRouter = express.Router()
35
36accountsRouter.get('/',
37 paginationValidator,
38 accountsSortValidator,
1174a847 39 setDefaultSort,
f05a1c30 40 setDefaultPagination,
265ba139
C
41 asyncMiddleware(listAccounts)
42)
43
ad9e39fb 44accountsRouter.get('/:accountName',
418d092a 45 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
46 getAccount
47)
48
ad9e39fb 49accountsRouter.get('/:accountName/videos',
418d092a 50 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
51 paginationValidator,
52 videosSortValidator,
8054669f 53 setDefaultVideosSort,
0626e7af
C
54 setDefaultPagination,
55 optionalAuthenticate,
d525fc39 56 commonVideosFiltersValidator,
48dce1c9
C
57 asyncMiddleware(listAccountVideos)
58)
59
ad9e39fb 60accountsRouter.get('/:accountName/video-channels',
418d092a 61 asyncMiddleware(accountNameWithHostGetValidator),
747c5628 62 videoChannelStatsValidator,
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 } })
744d0eca
C
107 }
108
0626e7af 109 return res.json(account.toFormattedJSON())
265ba139
C
110}
111
418d092a 112async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
113 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
114
115 return res.json(getFormattedObjects(resultList.data, resultList.total))
116}
0626e7af 117
418d092a 118async function listAccountChannels (req: express.Request, res: express.Response) {
91b66319
C
119 const options = {
120 accountId: res.locals.account.id,
121 start: req.query.start,
122 count: req.query.count,
747c5628 123 sort: req.query.sort,
4f5d0459
RK
124 withStats: req.query.withStats,
125 search: req.query.search
91b66319
C
126 }
127
128 const resultList = await VideoChannelModel.listByAccount(options)
48dce1c9
C
129
130 return res.json(getFormattedObjects(resultList.data, resultList.total))
131}
132
418d092a
C
133async function listAccountPlaylists (req: express.Request, res: express.Response) {
134 const serverActor = await getServerActor()
135
136 // Allow users to see their private/unlisted video playlists
6b0c3c7c 137 let listMyPlaylists = false
dae86118 138 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
6b0c3c7c 139 listMyPlaylists = true
418d092a
C
140 }
141
142 const resultList = await VideoPlaylistModel.listForApi({
c06af501 143 search: req.query.search,
418d092a
C
144 followerActorId: serverActor.id,
145 start: req.query.start,
146 count: req.query.count,
147 sort: req.query.sort,
148 accountId: res.locals.account.id,
6b0c3c7c 149 listMyPlaylists,
df0b219d 150 type: req.query.playlistType
418d092a
C
151 })
152
153 return res.json(getFormattedObjects(resultList.data, resultList.total))
154}
155
156async function listAccountVideos (req: express.Request, res: express.Response) {
dae86118 157 const account = res.locals.account
4e74e803 158 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
fe987656 159 const countVideos = getCountVideos(req)
0626e7af 160
48dce1c9 161 const resultList = await VideoModel.listForApi({
4e74e803 162 followerActorId,
48dce1c9
C
163 start: req.query.start,
164 count: req.query.count,
165 sort: req.query.sort,
8a19bee1 166 includeLocalVideos: true,
d525fc39
C
167 categoryOneOf: req.query.categoryOneOf,
168 licenceOneOf: req.query.licenceOneOf,
169 languageOneOf: req.query.languageOneOf,
170 tagsOneOf: req.query.tagsOneOf,
171 tagsAllOf: req.query.tagsAllOf,
1cd3facc 172 filter: req.query.filter,
d525fc39 173 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 174 withFiles: false,
1cd3facc 175 accountId: account.id,
fe987656
C
176 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
177 countVideos
48dce1c9 178 })
0626e7af
C
179
180 return res.json(getFormattedObjects(resultList.data, resultList.total))
181}
c100a614
YB
182
183async function listAccountRatings (req: express.Request, res: express.Response) {
184 const account = res.locals.account
185
186 const resultList = await AccountVideoRateModel.listByAccountForApi({
187 accountId: account.id,
188 start: req.query.start,
189 count: req.query.count,
190 sort: req.query.sort,
191 type: req.query.rating
192 })
193 return res.json(getFormattedObjects(resultList.rows, resultList.count))
194}