]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/accounts.ts
Fix old DB enum names
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
... / ...
CommitLineData
1import express from 'express'
2import { pickCommonVideoQuery } from '@server/helpers/query'
3import { ActorFollowModel } from '@server/models/actor/actor-follow'
4import { getServerActor } from '@server/models/application/application'
5import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
6import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
7import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
8import { getFormattedObjects } from '../../helpers/utils'
9import { JobQueue } from '../../lib/job-queue'
10import { Hooks } from '../../lib/plugins/hooks'
11import {
12 asyncMiddleware,
13 authenticate,
14 commonVideosFiltersValidator,
15 optionalAuthenticate,
16 paginationValidator,
17 setDefaultPagination,
18 setDefaultSort,
19 setDefaultVideosSort,
20 videoPlaylistsSortValidator,
21 videoRatesSortValidator,
22 videoRatingValidator
23} from '../../middlewares'
24import {
25 accountNameWithHostGetValidator,
26 accountsFollowersSortValidator,
27 accountsSortValidator,
28 ensureAuthUserOwnsAccountValidator,
29 ensureCanManageChannelOrAccount,
30 videoChannelsSortValidator,
31 videoChannelStatsValidator,
32 videoChannelSyncsSortValidator,
33 videosSortValidator
34} from '../../middlewares/validators'
35import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
36import { AccountModel } from '../../models/account/account'
37import { AccountVideoRateModel } from '../../models/account/account-video-rate'
38import { VideoModel } from '../../models/video/video'
39import { VideoChannelModel } from '../../models/video/video-channel'
40import { VideoPlaylistModel } from '../../models/video/video-playlist'
41
42const accountsRouter = express.Router()
43
44accountsRouter.get('/',
45 paginationValidator,
46 accountsSortValidator,
47 setDefaultSort,
48 setDefaultPagination,
49 asyncMiddleware(listAccounts)
50)
51
52accountsRouter.get('/:accountName',
53 asyncMiddleware(accountNameWithHostGetValidator),
54 getAccount
55)
56
57accountsRouter.get('/:accountName/videos',
58 asyncMiddleware(accountNameWithHostGetValidator),
59 paginationValidator,
60 videosSortValidator,
61 setDefaultVideosSort,
62 setDefaultPagination,
63 optionalAuthenticate,
64 commonVideosFiltersValidator,
65 asyncMiddleware(listAccountVideos)
66)
67
68accountsRouter.get('/:accountName/video-channels',
69 asyncMiddleware(accountNameWithHostGetValidator),
70 videoChannelStatsValidator,
71 paginationValidator,
72 videoChannelsSortValidator,
73 setDefaultSort,
74 setDefaultPagination,
75 asyncMiddleware(listAccountChannels)
76)
77
78accountsRouter.get('/:accountName/video-channel-syncs',
79 authenticate,
80 asyncMiddleware(accountNameWithHostGetValidator),
81 ensureCanManageChannelOrAccount,
82 paginationValidator,
83 videoChannelSyncsSortValidator,
84 setDefaultSort,
85 setDefaultPagination,
86 asyncMiddleware(listAccountChannelsSync)
87)
88
89accountsRouter.get('/:accountName/video-playlists',
90 optionalAuthenticate,
91 asyncMiddleware(accountNameWithHostGetValidator),
92 paginationValidator,
93 videoPlaylistsSortValidator,
94 setDefaultSort,
95 setDefaultPagination,
96 commonVideoPlaylistFiltersValidator,
97 videoPlaylistsSearchValidator,
98 asyncMiddleware(listAccountPlaylists)
99)
100
101accountsRouter.get('/:accountName/ratings',
102 authenticate,
103 asyncMiddleware(accountNameWithHostGetValidator),
104 ensureAuthUserOwnsAccountValidator,
105 paginationValidator,
106 videoRatesSortValidator,
107 setDefaultSort,
108 setDefaultPagination,
109 videoRatingValidator,
110 asyncMiddleware(listAccountRatings)
111)
112
113accountsRouter.get('/:accountName/followers',
114 authenticate,
115 asyncMiddleware(accountNameWithHostGetValidator),
116 ensureAuthUserOwnsAccountValidator,
117 paginationValidator,
118 accountsFollowersSortValidator,
119 setDefaultSort,
120 setDefaultPagination,
121 asyncMiddleware(listAccountFollowers)
122)
123
124// ---------------------------------------------------------------------------
125
126export {
127 accountsRouter
128}
129
130// ---------------------------------------------------------------------------
131
132function getAccount (req: express.Request, res: express.Response) {
133 const account = res.locals.account
134
135 if (account.isOutdated()) {
136 JobQueue.Instance.createJobAsync({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
137 }
138
139 return res.json(account.toFormattedJSON())
140}
141
142async function listAccounts (req: express.Request, res: express.Response) {
143 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
144
145 return res.json(getFormattedObjects(resultList.data, resultList.total))
146}
147
148async function listAccountChannels (req: express.Request, res: express.Response) {
149 const options = {
150 accountId: res.locals.account.id,
151 start: req.query.start,
152 count: req.query.count,
153 sort: req.query.sort,
154 withStats: req.query.withStats,
155 search: req.query.search
156 }
157
158 const resultList = await VideoChannelModel.listByAccountForAPI(options)
159
160 return res.json(getFormattedObjects(resultList.data, resultList.total))
161}
162
163async function listAccountChannelsSync (req: express.Request, res: express.Response) {
164 const options = {
165 accountId: res.locals.account.id,
166 start: req.query.start,
167 count: req.query.count,
168 sort: req.query.sort,
169 search: req.query.search
170 }
171
172 const resultList = await VideoChannelSyncModel.listByAccountForAPI(options)
173
174 return res.json(getFormattedObjects(resultList.data, resultList.total))
175}
176
177async function listAccountPlaylists (req: express.Request, res: express.Response) {
178 const serverActor = await getServerActor()
179
180 // Allow users to see their private/unlisted video playlists
181 let listMyPlaylists = false
182 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
183 listMyPlaylists = true
184 }
185
186 const resultList = await VideoPlaylistModel.listForApi({
187 search: req.query.search,
188 followerActorId: serverActor.id,
189 start: req.query.start,
190 count: req.query.count,
191 sort: req.query.sort,
192 accountId: res.locals.account.id,
193 listMyPlaylists,
194 type: req.query.playlistType
195 })
196
197 return res.json(getFormattedObjects(resultList.data, resultList.total))
198}
199
200async function listAccountVideos (req: express.Request, res: express.Response) {
201 const serverActor = await getServerActor()
202
203 const account = res.locals.account
204
205 const displayOnlyForFollower = isUserAbleToSearchRemoteURI(res)
206 ? null
207 : {
208 actorId: serverActor.id,
209 orLocalVideos: true
210 }
211
212 const countVideos = getCountVideos(req)
213 const query = pickCommonVideoQuery(req.query)
214
215 const apiOptions = await Hooks.wrapObject({
216 ...query,
217
218 displayOnlyForFollower,
219 nsfw: buildNSFWFilter(res, query.nsfw),
220 accountId: account.id,
221 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
222 countVideos
223 }, 'filter:api.accounts.videos.list.params')
224
225 const resultList = await Hooks.wrapPromiseFun(
226 VideoModel.listForApi,
227 apiOptions,
228 'filter:api.accounts.videos.list.result'
229 )
230
231 return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
232}
233
234async function listAccountRatings (req: express.Request, res: express.Response) {
235 const account = res.locals.account
236
237 const resultList = await AccountVideoRateModel.listByAccountForApi({
238 accountId: account.id,
239 start: req.query.start,
240 count: req.query.count,
241 sort: req.query.sort,
242 type: req.query.rating
243 })
244 return res.json(getFormattedObjects(resultList.data, resultList.total))
245}
246
247async function listAccountFollowers (req: express.Request, res: express.Response) {
248 const account = res.locals.account
249
250 const channels = await VideoChannelModel.listAllByAccount(account.id)
251 const actorIds = [ account.actorId ].concat(channels.map(c => c.actorId))
252
253 const resultList = await ActorFollowModel.listFollowersForApi({
254 actorIds,
255 start: req.query.start,
256 count: req.query.count,
257 sort: req.query.sort,
258 search: req.query.search,
259 state: 'accepted'
260 })
261
262 return res.json(getFormattedObjects(resultList.data, resultList.total))
263}