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