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