]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/accounts.ts
harmonize search for libraries
[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 search: req.query.search
125 }
126
127 const resultList = await VideoChannelModel.listByAccount(options)
128
129 return res.json(getFormattedObjects(resultList.data, resultList.total))
130 }
131
132 async function listAccountPlaylists (req: express.Request, res: express.Response) {
133 const serverActor = await getServerActor()
134
135 // Allow users to see their private/unlisted video playlists
136 let listMyPlaylists = false
137 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
138 listMyPlaylists = true
139 }
140
141 const resultList = await VideoPlaylistModel.listForApi({
142 search: req.query.search,
143 followerActorId: serverActor.id,
144 start: req.query.start,
145 count: req.query.count,
146 sort: req.query.sort,
147 accountId: res.locals.account.id,
148 listMyPlaylists,
149 type: req.query.playlistType
150 })
151
152 return res.json(getFormattedObjects(resultList.data, resultList.total))
153 }
154
155 async function listAccountVideos (req: express.Request, res: express.Response) {
156 const account = res.locals.account
157 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
158 const countVideos = getCountVideos(req)
159
160 const resultList = await VideoModel.listForApi({
161 followerActorId,
162 start: req.query.start,
163 count: req.query.count,
164 sort: req.query.sort,
165 includeLocalVideos: true,
166 categoryOneOf: req.query.categoryOneOf,
167 licenceOneOf: req.query.licenceOneOf,
168 languageOneOf: req.query.languageOneOf,
169 tagsOneOf: req.query.tagsOneOf,
170 tagsAllOf: req.query.tagsAllOf,
171 filter: req.query.filter,
172 nsfw: buildNSFWFilter(res, req.query.nsfw),
173 withFiles: false,
174 accountId: account.id,
175 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
176 countVideos
177 })
178
179 return res.json(getFormattedObjects(resultList.data, resultList.total))
180 }
181
182 async function listAccountRatings (req: express.Request, res: express.Response) {
183 const account = res.locals.account
184
185 const resultList = await AccountVideoRateModel.listByAccountForApi({
186 accountId: account.id,
187 start: req.query.start,
188 count: req.query.count,
189 sort: req.query.sort,
190 type: req.query.rating
191 })
192 return res.json(getFormattedObjects(resultList.rows, resultList.count))
193 }