]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/accounts.ts
Merge branch 'release/2.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
3 import {
4 asyncMiddleware,
5 authenticate,
6 commonVideosFiltersValidator,
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
10 setDefaultSort,
11 videoPlaylistsSortValidator,
12 videoRatesSortValidator,
13 videoRatingValidator
14 } from '../../middlewares'
15 import {
16 accountNameWithHostGetValidator,
17 accountsSortValidator,
18 ensureAuthUserOwnsAccountValidator,
19 videoChannelsSortValidator,
20 videosSortValidator,
21 videoChannelStatsValidator
22 } from '../../middlewares/validators'
23 import { AccountModel } from '../../models/account/account'
24 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
25 import { VideoModel } from '../../models/video/video'
26 import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
27 import { VideoChannelModel } from '../../models/video/video-channel'
28 import { JobQueue } from '../../lib/job-queue'
29 import { VideoPlaylistModel } from '../../models/video/video-playlist'
30 import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
31 import { getServerActor } from '@server/models/application/application'
32
33 const accountsRouter = express.Router()
34
35 accountsRouter.get('/',
36 paginationValidator,
37 accountsSortValidator,
38 setDefaultSort,
39 setDefaultPagination,
40 asyncMiddleware(listAccounts)
41 )
42
43 accountsRouter.get('/:accountName',
44 asyncMiddleware(accountNameWithHostGetValidator),
45 getAccount
46 )
47
48 accountsRouter.get('/:accountName/videos',
49 asyncMiddleware(accountNameWithHostGetValidator),
50 paginationValidator,
51 videosSortValidator,
52 setDefaultSort,
53 setDefaultPagination,
54 optionalAuthenticate,
55 commonVideosFiltersValidator,
56 asyncMiddleware(listAccountVideos)
57 )
58
59 accountsRouter.get('/:accountName/video-channels',
60 asyncMiddleware(accountNameWithHostGetValidator),
61 videoChannelStatsValidator,
62 paginationValidator,
63 videoChannelsSortValidator,
64 setDefaultSort,
65 setDefaultPagination,
66 asyncMiddleware(listAccountChannels)
67 )
68
69 accountsRouter.get('/:accountName/video-playlists',
70 optionalAuthenticate,
71 asyncMiddleware(accountNameWithHostGetValidator),
72 paginationValidator,
73 videoPlaylistsSortValidator,
74 setDefaultSort,
75 setDefaultPagination,
76 commonVideoPlaylistFiltersValidator,
77 videoPlaylistsSearchValidator,
78 asyncMiddleware(listAccountPlaylists)
79 )
80
81 accountsRouter.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
93 // ---------------------------------------------------------------------------
94
95 export {
96 accountsRouter
97 }
98
99 // ---------------------------------------------------------------------------
100
101 function getAccount (req: express.Request, res: express.Response) {
102 const account = res.locals.account
103
104 if (account.isOutdated()) {
105 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
106 }
107
108 return res.json(account.toFormattedJSON())
109 }
110
111 async function listAccounts (req: express.Request, res: express.Response) {
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 }
116
117 async function listAccountChannels (req: express.Request, res: express.Response) {
118 const options = {
119 accountId: res.locals.account.id,
120 start: req.query.start,
121 count: req.query.count,
122 sort: req.query.sort,
123 withStats: req.query.withStats
124 }
125
126 const resultList = await VideoChannelModel.listByAccount(options)
127
128 return res.json(getFormattedObjects(resultList.data, resultList.total))
129 }
130
131 async function listAccountPlaylists (req: express.Request, res: express.Response) {
132 const serverActor = await getServerActor()
133
134 // Allow users to see their private/unlisted video playlists
135 let listMyPlaylists = false
136 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
137 listMyPlaylists = true
138 }
139
140 const resultList = await VideoPlaylistModel.listForApi({
141 search: req.query.search,
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,
147 listMyPlaylists,
148 type: req.query.playlistType
149 })
150
151 return res.json(getFormattedObjects(resultList.data, resultList.total))
152 }
153
154 async function listAccountVideos (req: express.Request, res: express.Response) {
155 const account = res.locals.account
156 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
157 const countVideos = getCountVideos(req)
158
159 const resultList = await VideoModel.listForApi({
160 followerActorId,
161 start: req.query.start,
162 count: req.query.count,
163 sort: req.query.sort,
164 includeLocalVideos: true,
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,
170 filter: req.query.filter,
171 nsfw: buildNSFWFilter(res, req.query.nsfw),
172 withFiles: false,
173 accountId: account.id,
174 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
175 countVideos
176 })
177
178 return res.json(getFormattedObjects(resultList.data, resultList.total))
179 }
180
181 async 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 }