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