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