]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Introduce comments command
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
8054669f 2import { getServerActor } from '@server/models/application/application'
1fd61899 3import { VideosWithSearchCommonQuery } from '@shared/models'
8054669f 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)
1fd61899 162 const query = req.query as VideosWithSearchCommonQuery
0626e7af 163
1bfc07e4 164 const apiOptions = await Hooks.wrapObject({
4e74e803 165 followerActorId,
1fd61899
C
166 start: query.start,
167 count: query.count,
168 sort: query.sort,
8a19bee1 169 includeLocalVideos: true,
1fd61899
C
170 categoryOneOf: query.categoryOneOf,
171 licenceOneOf: query.licenceOneOf,
172 languageOneOf: query.languageOneOf,
173 tagsOneOf: query.tagsOneOf,
174 tagsAllOf: query.tagsAllOf,
175 filter: query.filter,
176 isLive: query.isLive,
177 nsfw: buildNSFWFilter(res, query.nsfw),
48dce1c9 178 withFiles: false,
1cd3facc 179 accountId: account.id,
fe987656 180 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
37024082 181 countVideos,
1fd61899 182 search: query.search
38267c0c 183 }, 'filter:api.accounts.videos.list.params')
1bfc07e4 184
185 const resultList = await Hooks.wrapPromiseFun(
186 VideoModel.listForApi,
187 apiOptions,
38267c0c 188 'filter:api.accounts.videos.list.result'
1bfc07e4 189 )
0626e7af
C
190
191 return res.json(getFormattedObjects(resultList.data, resultList.total))
192}
c100a614
YB
193
194async function listAccountRatings (req: express.Request, res: express.Response) {
195 const account = res.locals.account
196
197 const resultList = await AccountVideoRateModel.listByAccountForApi({
198 accountId: account.id,
199 start: req.query.start,
200 count: req.query.count,
201 sort: req.query.sort,
202 type: req.query.rating
203 })
204 return res.json(getFormattedObjects(resultList.rows, resultList.count))
205}