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