]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Fix job queue tests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
41fb13c3 1import express from 'express'
d6886027 2import { pickCommonVideoQuery } from '@server/helpers/query'
4beda9e1 3import { ActorFollowModel } from '@server/models/actor/actor-follow'
8054669f 4import { getServerActor } from '@server/models/application/application'
2760b454 5import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
8054669f 6import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
e1c55031 7import { getFormattedObjects } from '../../helpers/utils'
8054669f 8import { JobQueue } from '../../lib/job-queue'
1fd61899 9import { Hooks } from '../../lib/plugins/hooks'
48dce1c9 10import {
7ad9b984 11 asyncMiddleware,
22834691 12 authenticate,
7ad9b984 13 commonVideosFiltersValidator,
48dce1c9
C
14 optionalAuthenticate,
15 paginationValidator,
16 setDefaultPagination,
418d092a 17 setDefaultSort,
8054669f 18 setDefaultVideosSort,
c100a614 19 videoPlaylistsSortValidator,
22834691
C
20 videoRatesSortValidator,
21 videoRatingValidator
48dce1c9 22} from '../../middlewares'
c100a614
YB
23import {
24 accountNameWithHostGetValidator,
4beda9e1 25 accountsFollowersSortValidator,
c100a614 26 accountsSortValidator,
22834691 27 ensureAuthUserOwnsAccountValidator,
a1587156 28 videoChannelsSortValidator,
8054669f
C
29 videoChannelStatsValidator,
30 videosSortValidator
c100a614 31} from '../../middlewares/validators'
8054669f 32import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
265ba139 33import { AccountModel } from '../../models/account/account'
c100a614 34import { AccountVideoRateModel } from '../../models/account/account-video-rate'
0626e7af 35import { VideoModel } from '../../models/video/video'
48dce1c9 36import { VideoChannelModel } from '../../models/video/video-channel'
418d092a 37import { VideoPlaylistModel } from '../../models/video/video-playlist'
265ba139
C
38
39const accountsRouter = express.Router()
40
41accountsRouter.get('/',
42 paginationValidator,
43 accountsSortValidator,
1174a847 44 setDefaultSort,
f05a1c30 45 setDefaultPagination,
265ba139
C
46 asyncMiddleware(listAccounts)
47)
48
ad9e39fb 49accountsRouter.get('/:accountName',
418d092a 50 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
51 getAccount
52)
53
ad9e39fb 54accountsRouter.get('/:accountName/videos',
418d092a 55 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
56 paginationValidator,
57 videosSortValidator,
8054669f 58 setDefaultVideosSort,
0626e7af
C
59 setDefaultPagination,
60 optionalAuthenticate,
d525fc39 61 commonVideosFiltersValidator,
48dce1c9
C
62 asyncMiddleware(listAccountVideos)
63)
64
ad9e39fb 65accountsRouter.get('/:accountName/video-channels',
418d092a 66 asyncMiddleware(accountNameWithHostGetValidator),
747c5628 67 videoChannelStatsValidator,
91b66319
C
68 paginationValidator,
69 videoChannelsSortValidator,
70 setDefaultSort,
71 setDefaultPagination,
418d092a
C
72 asyncMiddleware(listAccountChannels)
73)
74
75accountsRouter.get('/:accountName/video-playlists',
76 optionalAuthenticate,
77 asyncMiddleware(accountNameWithHostGetValidator),
78 paginationValidator,
79 videoPlaylistsSortValidator,
80 setDefaultSort,
81 setDefaultPagination,
df0b219d 82 commonVideoPlaylistFiltersValidator,
c06af501 83 videoPlaylistsSearchValidator,
418d092a 84 asyncMiddleware(listAccountPlaylists)
48dce1c9
C
85)
86
c100a614
YB
87accountsRouter.get('/:accountName/ratings',
88 authenticate,
89 asyncMiddleware(accountNameWithHostGetValidator),
90 ensureAuthUserOwnsAccountValidator,
91 paginationValidator,
92 videoRatesSortValidator,
93 setDefaultSort,
94 setDefaultPagination,
95 videoRatingValidator,
96 asyncMiddleware(listAccountRatings)
97)
98
4beda9e1
C
99accountsRouter.get('/:accountName/followers',
100 authenticate,
101 asyncMiddleware(accountNameWithHostGetValidator),
102 ensureAuthUserOwnsAccountValidator,
103 paginationValidator,
104 accountsFollowersSortValidator,
105 setDefaultSort,
106 setDefaultPagination,
107 asyncMiddleware(listAccountFollowers)
108)
109
265ba139
C
110// ---------------------------------------------------------------------------
111
112export {
113 accountsRouter
114}
115
116// ---------------------------------------------------------------------------
117
418d092a 118function getAccount (req: express.Request, res: express.Response) {
dae86118 119 const account = res.locals.account
0626e7af 120
744d0eca 121 if (account.isOutdated()) {
bd911b54 122 JobQueue.Instance.createJobAsync({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
744d0eca
C
123 }
124
0626e7af 125 return res.json(account.toFormattedJSON())
265ba139
C
126}
127
418d092a 128async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
129 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
130
131 return res.json(getFormattedObjects(resultList.data, resultList.total))
132}
0626e7af 133
418d092a 134async function listAccountChannels (req: express.Request, res: express.Response) {
91b66319
C
135 const options = {
136 accountId: res.locals.account.id,
137 start: req.query.start,
138 count: req.query.count,
747c5628 139 sort: req.query.sort,
4f5d0459
RK
140 withStats: req.query.withStats,
141 search: req.query.search
91b66319
C
142 }
143
4beda9e1 144 const resultList = await VideoChannelModel.listByAccountForAPI(options)
48dce1c9
C
145
146 return res.json(getFormattedObjects(resultList.data, resultList.total))
147}
148
418d092a
C
149async function listAccountPlaylists (req: express.Request, res: express.Response) {
150 const serverActor = await getServerActor()
151
152 // Allow users to see their private/unlisted video playlists
6b0c3c7c 153 let listMyPlaylists = false
dae86118 154 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
6b0c3c7c 155 listMyPlaylists = true
418d092a
C
156 }
157
158 const resultList = await VideoPlaylistModel.listForApi({
c06af501 159 search: req.query.search,
418d092a
C
160 followerActorId: serverActor.id,
161 start: req.query.start,
162 count: req.query.count,
163 sort: req.query.sort,
164 accountId: res.locals.account.id,
6b0c3c7c 165 listMyPlaylists,
df0b219d 166 type: req.query.playlistType
418d092a
C
167 })
168
169 return res.json(getFormattedObjects(resultList.data, resultList.total))
170}
171
172async function listAccountVideos (req: express.Request, res: express.Response) {
2760b454
C
173 const serverActor = await getServerActor()
174
dae86118 175 const account = res.locals.account
2760b454
C
176
177 const displayOnlyForFollower = isUserAbleToSearchRemoteURI(res)
178 ? null
179 : {
180 actorId: serverActor.id,
181 orLocalVideos: true
182 }
183
fe987656 184 const countVideos = getCountVideos(req)
d6886027 185 const query = pickCommonVideoQuery(req.query)
0626e7af 186
1bfc07e4 187 const apiOptions = await Hooks.wrapObject({
d6886027
C
188 ...query,
189
2760b454 190 displayOnlyForFollower,
1fd61899 191 nsfw: buildNSFWFilter(res, query.nsfw),
1cd3facc 192 accountId: account.id,
fe987656 193 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
d6886027 194 countVideos
38267c0c 195 }, 'filter:api.accounts.videos.list.params')
1bfc07e4 196
197 const resultList = await Hooks.wrapPromiseFun(
198 VideoModel.listForApi,
199 apiOptions,
38267c0c 200 'filter:api.accounts.videos.list.result'
1bfc07e4 201 )
0626e7af 202
2760b454 203 return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
0626e7af 204}
c100a614
YB
205
206async function listAccountRatings (req: express.Request, res: express.Response) {
207 const account = res.locals.account
208
209 const resultList = await AccountVideoRateModel.listByAccountForApi({
210 accountId: account.id,
211 start: req.query.start,
212 count: req.query.count,
213 sort: req.query.sort,
214 type: req.query.rating
215 })
d0800f76 216 return res.json(getFormattedObjects(resultList.data, resultList.total))
c100a614 217}
4beda9e1
C
218
219async function listAccountFollowers (req: express.Request, res: express.Response) {
220 const account = res.locals.account
221
222 const channels = await VideoChannelModel.listAllByAccount(account.id)
223 const actorIds = [ account.actorId ].concat(channels.map(c => c.actorId))
224
225 const resultList = await ActorFollowModel.listFollowersForApi({
226 actorIds,
227 start: req.query.start,
228 count: req.query.count,
229 sort: req.query.sort,
230 search: req.query.search,
906f46d0 231 state: 'accepted'
4beda9e1
C
232 })
233
234 return res.json(getFormattedObjects(resultList.data, resultList.total))
235}