Fix private video download
[oweals/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   videosSortValidator,
20   videoChannelsSortValidator
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, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
26 import { VideoChannelModel } from '../../models/video/video-channel'
27 import { JobQueue } from '../../lib/job-queue'
28 import { logger } from '../../helpers/logger'
29 import { VideoPlaylistModel } from '../../models/video/video-playlist'
30 import { commonVideoPlaylistFiltersValidator } 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   paginationValidator,
61   videoChannelsSortValidator,
62   setDefaultSort,
63   setDefaultPagination,
64   asyncMiddleware(listAccountChannels)
65 )
66
67 accountsRouter.get('/:accountName/video-playlists',
68   optionalAuthenticate,
69   asyncMiddleware(accountNameWithHostGetValidator),
70   paginationValidator,
71   videoPlaylistsSortValidator,
72   setDefaultSort,
73   setDefaultPagination,
74   commonVideoPlaylistFiltersValidator,
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             .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
104   }
105
106   return res.json(account.toFormattedJSON())
107 }
108
109 async function listAccounts (req: express.Request, res: express.Response) {
110   const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
111
112   return res.json(getFormattedObjects(resultList.data, resultList.total))
113 }
114
115 async function listAccountChannels (req: express.Request, res: express.Response) {
116   const options = {
117     accountId: res.locals.account.id,
118     start: req.query.start,
119     count: req.query.count,
120     sort: req.query.sort
121   }
122
123   const resultList = await VideoChannelModel.listByAccount(options)
124
125   return res.json(getFormattedObjects(resultList.data, resultList.total))
126 }
127
128 async function listAccountPlaylists (req: express.Request, res: express.Response) {
129   const serverActor = await getServerActor()
130
131   // Allow users to see their private/unlisted video playlists
132   let privateAndUnlisted = false
133   if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
134     privateAndUnlisted = true
135   }
136
137   const resultList = await VideoPlaylistModel.listForApi({
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     privateAndUnlisted,
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
154   const resultList = await VideoModel.listForApi({
155     followerActorId,
156     start: req.query.start,
157     count: req.query.count,
158     sort: req.query.sort,
159     includeLocalVideos: true,
160     categoryOneOf: req.query.categoryOneOf,
161     licenceOneOf: req.query.licenceOneOf,
162     languageOneOf: req.query.languageOneOf,
163     tagsOneOf: req.query.tagsOneOf,
164     tagsAllOf: req.query.tagsAllOf,
165     filter: req.query.filter,
166     nsfw: buildNSFWFilter(res, req.query.nsfw),
167     withFiles: false,
168     accountId: account.id,
169     user: res.locals.oauth ? res.locals.oauth.token.User : undefined
170   })
171
172   return res.json(getFormattedObjects(resultList.data, resultList.total))
173 }
174
175 async function listAccountRatings (req: express.Request, res: express.Response) {
176   const account = res.locals.account
177
178   const resultList = await AccountVideoRateModel.listByAccountForApi({
179     accountId: account.id,
180     start: req.query.start,
181     count: req.query.count,
182     sort: req.query.sort,
183     type: req.query.rating
184   })
185   return res.json(getFormattedObjects(resultList.rows, resultList.count))
186 }