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