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