]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Fix rowsPerPage change, add filter clear button, update video-abuse-list search query...
[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,
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'
265ba139
C
31
32const accountsRouter = express.Router()
33
34accountsRouter.get('/',
35 paginationValidator,
36 accountsSortValidator,
1174a847 37 setDefaultSort,
f05a1c30 38 setDefaultPagination,
265ba139
C
39 asyncMiddleware(listAccounts)
40)
41
ad9e39fb 42accountsRouter.get('/:accountName',
418d092a 43 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
44 getAccount
45)
46
ad9e39fb 47accountsRouter.get('/:accountName/videos',
418d092a 48 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
49 paginationValidator,
50 videosSortValidator,
51 setDefaultSort,
52 setDefaultPagination,
53 optionalAuthenticate,
d525fc39 54 commonVideosFiltersValidator,
48dce1c9
C
55 asyncMiddleware(listAccountVideos)
56)
57
ad9e39fb 58accountsRouter.get('/:accountName/video-channels',
418d092a 59 asyncMiddleware(accountNameWithHostGetValidator),
747c5628 60 videoChannelStatsValidator,
91b66319
C
61 paginationValidator,
62 videoChannelsSortValidator,
63 setDefaultSort,
64 setDefaultPagination,
418d092a
C
65 asyncMiddleware(listAccountChannels)
66)
67
68accountsRouter.get('/:accountName/video-playlists',
69 optionalAuthenticate,
70 asyncMiddleware(accountNameWithHostGetValidator),
71 paginationValidator,
72 videoPlaylistsSortValidator,
73 setDefaultSort,
74 setDefaultPagination,
df0b219d 75 commonVideoPlaylistFiltersValidator,
c06af501 76 videoPlaylistsSearchValidator,
418d092a 77 asyncMiddleware(listAccountPlaylists)
48dce1c9
C
78)
79
c100a614
YB
80accountsRouter.get('/:accountName/ratings',
81 authenticate,
82 asyncMiddleware(accountNameWithHostGetValidator),
83 ensureAuthUserOwnsAccountValidator,
84 paginationValidator,
85 videoRatesSortValidator,
86 setDefaultSort,
87 setDefaultPagination,
88 videoRatingValidator,
89 asyncMiddleware(listAccountRatings)
90)
91
265ba139
C
92// ---------------------------------------------------------------------------
93
94export {
95 accountsRouter
96}
97
98// ---------------------------------------------------------------------------
99
418d092a 100function getAccount (req: express.Request, res: express.Response) {
dae86118 101 const account = res.locals.account
0626e7af 102
744d0eca
C
103 if (account.isOutdated()) {
104 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
744d0eca
C
105 }
106
0626e7af 107 return res.json(account.toFormattedJSON())
265ba139
C
108}
109
418d092a 110async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
111 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
112
113 return res.json(getFormattedObjects(resultList.data, resultList.total))
114}
0626e7af 115
418d092a 116async function listAccountChannels (req: express.Request, res: express.Response) {
91b66319
C
117 const options = {
118 accountId: res.locals.account.id,
119 start: req.query.start,
120 count: req.query.count,
747c5628
RK
121 sort: req.query.sort,
122 withStats: req.query.withStats
91b66319
C
123 }
124
125 const resultList = await VideoChannelModel.listByAccount(options)
48dce1c9
C
126
127 return res.json(getFormattedObjects(resultList.data, resultList.total))
128}
129
418d092a
C
130async function listAccountPlaylists (req: express.Request, res: express.Response) {
131 const serverActor = await getServerActor()
132
133 // Allow users to see their private/unlisted video playlists
6b0c3c7c 134 let listMyPlaylists = false
dae86118 135 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
6b0c3c7c 136 listMyPlaylists = true
418d092a
C
137 }
138
139 const resultList = await VideoPlaylistModel.listForApi({
c06af501 140 search: req.query.search,
418d092a
C
141 followerActorId: serverActor.id,
142 start: req.query.start,
143 count: req.query.count,
144 sort: req.query.sort,
145 accountId: res.locals.account.id,
6b0c3c7c 146 listMyPlaylists,
df0b219d 147 type: req.query.playlistType
418d092a
C
148 })
149
150 return res.json(getFormattedObjects(resultList.data, resultList.total))
151}
152
153async function listAccountVideos (req: express.Request, res: express.Response) {
dae86118 154 const account = res.locals.account
4e74e803 155 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
fe987656 156 const countVideos = getCountVideos(req)
0626e7af 157
48dce1c9 158 const resultList = await VideoModel.listForApi({
4e74e803 159 followerActorId,
48dce1c9
C
160 start: req.query.start,
161 count: req.query.count,
162 sort: req.query.sort,
8a19bee1 163 includeLocalVideos: true,
d525fc39
C
164 categoryOneOf: req.query.categoryOneOf,
165 licenceOneOf: req.query.licenceOneOf,
166 languageOneOf: req.query.languageOneOf,
167 tagsOneOf: req.query.tagsOneOf,
168 tagsAllOf: req.query.tagsAllOf,
1cd3facc 169 filter: req.query.filter,
d525fc39 170 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 171 withFiles: false,
1cd3facc 172 accountId: account.id,
fe987656
C
173 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
174 countVideos
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}