]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/accounts.ts
Add inspect to test script
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { getServerActor } from '@server/models/application/application'
3import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
4import { getFormattedObjects } from '../../helpers/utils'
5import { JobQueue } from '../../lib/job-queue'
6import {
7 asyncMiddleware,
8 authenticate,
9 commonVideosFiltersValidator,
10 optionalAuthenticate,
11 paginationValidator,
12 setDefaultPagination,
13 setDefaultSort,
14 setDefaultVideosSort,
15 videoPlaylistsSortValidator,
16 videoRatesSortValidator,
17 videoRatingValidator
18} from '../../middlewares'
19import {
20 accountNameWithHostGetValidator,
21 accountsSortValidator,
22 ensureAuthUserOwnsAccountValidator,
23 videoChannelsSortValidator,
24 videoChannelStatsValidator,
25 videosSortValidator
26} from '../../middlewares/validators'
27import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
28import { AccountModel } from '../../models/account/account'
29import { AccountVideoRateModel } from '../../models/account/account-video-rate'
30import { VideoModel } from '../../models/video/video'
31import { VideoChannelModel } from '../../models/video/video-channel'
32import { VideoPlaylistModel } from '../../models/video/video-playlist'
33
34const accountsRouter = express.Router()
35
36accountsRouter.get('/',
37 paginationValidator,
38 accountsSortValidator,
39 setDefaultSort,
40 setDefaultPagination,
41 asyncMiddleware(listAccounts)
42)
43
44accountsRouter.get('/:accountName',
45 asyncMiddleware(accountNameWithHostGetValidator),
46 getAccount
47)
48
49accountsRouter.get('/:accountName/videos',
50 asyncMiddleware(accountNameWithHostGetValidator),
51 paginationValidator,
52 videosSortValidator,
53 setDefaultVideosSort,
54 setDefaultPagination,
55 optionalAuthenticate,
56 commonVideosFiltersValidator,
57 asyncMiddleware(listAccountVideos)
58)
59
60accountsRouter.get('/:accountName/video-channels',
61 asyncMiddleware(accountNameWithHostGetValidator),
62 videoChannelStatsValidator,
63 paginationValidator,
64 videoChannelsSortValidator,
65 setDefaultSort,
66 setDefaultPagination,
67 asyncMiddleware(listAccountChannels)
68)
69
70accountsRouter.get('/:accountName/video-playlists',
71 optionalAuthenticate,
72 asyncMiddleware(accountNameWithHostGetValidator),
73 paginationValidator,
74 videoPlaylistsSortValidator,
75 setDefaultSort,
76 setDefaultPagination,
77 commonVideoPlaylistFiltersValidator,
78 videoPlaylistsSearchValidator,
79 asyncMiddleware(listAccountPlaylists)
80)
81
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
94// ---------------------------------------------------------------------------
95
96export {
97 accountsRouter
98}
99
100// ---------------------------------------------------------------------------
101
102function getAccount (req: express.Request, res: express.Response) {
103 const account = res.locals.account
104
105 if (account.isOutdated()) {
106 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
107 }
108
109 return res.json(account.toFormattedJSON())
110}
111
112async function listAccounts (req: express.Request, res: express.Response) {
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}
117
118async function listAccountChannels (req: express.Request, res: express.Response) {
119 const options = {
120 accountId: res.locals.account.id,
121 start: req.query.start,
122 count: req.query.count,
123 sort: req.query.sort,
124 withStats: req.query.withStats,
125 search: req.query.search
126 }
127
128 const resultList = await VideoChannelModel.listByAccount(options)
129
130 return res.json(getFormattedObjects(resultList.data, resultList.total))
131}
132
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
137 let listMyPlaylists = false
138 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
139 listMyPlaylists = true
140 }
141
142 const resultList = await VideoPlaylistModel.listForApi({
143 search: req.query.search,
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,
149 listMyPlaylists,
150 type: req.query.playlistType
151 })
152
153 return res.json(getFormattedObjects(resultList.data, resultList.total))
154}
155
156async function listAccountVideos (req: express.Request, res: express.Response) {
157 const account = res.locals.account
158 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
159 const countVideos = getCountVideos(req)
160
161 const resultList = await VideoModel.listForApi({
162 followerActorId,
163 start: req.query.start,
164 count: req.query.count,
165 sort: req.query.sort,
166 includeLocalVideos: true,
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,
172 filter: req.query.filter,
173 nsfw: buildNSFWFilter(res, req.query.nsfw),
174 withFiles: false,
175 accountId: account.id,
176 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
177 countVideos
178 })
179
180 return res.json(getFormattedObjects(resultList.data, resultList.total))
181}
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}