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