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