]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Add playlist rest tests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
418d092a 2import { getFormattedObjects, getServerActor } from '../../helpers/utils'
48dce1c9 3import {
7ad9b984
C
4 asyncMiddleware,
5 commonVideosFiltersValidator,
48dce1c9
C
6 optionalAuthenticate,
7 paginationValidator,
8 setDefaultPagination,
418d092a
C
9 setDefaultSort,
10 videoPlaylistsSortValidator
48dce1c9 11} from '../../middlewares'
418d092a 12import { accountNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
265ba139 13import { AccountModel } from '../../models/account/account'
0626e7af 14import { VideoModel } from '../../models/video/video'
687d638c 15import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
48dce1c9 16import { VideoChannelModel } from '../../models/video/video-channel'
744d0eca
C
17import { JobQueue } from '../../lib/job-queue'
18import { logger } from '../../helpers/logger'
418d092a
C
19import { VideoPlaylistModel } from '../../models/video/video-playlist'
20import { UserModel } from '../../models/account/user'
df0b219d 21import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
265ba139
C
22
23const accountsRouter = express.Router()
24
25accountsRouter.get('/',
26 paginationValidator,
27 accountsSortValidator,
1174a847 28 setDefaultSort,
f05a1c30 29 setDefaultPagination,
265ba139
C
30 asyncMiddleware(listAccounts)
31)
32
ad9e39fb 33accountsRouter.get('/:accountName',
418d092a 34 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
35 getAccount
36)
37
ad9e39fb 38accountsRouter.get('/:accountName/videos',
418d092a 39 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
40 paginationValidator,
41 videosSortValidator,
42 setDefaultSort,
43 setDefaultPagination,
44 optionalAuthenticate,
d525fc39 45 commonVideosFiltersValidator,
48dce1c9
C
46 asyncMiddleware(listAccountVideos)
47)
48
ad9e39fb 49accountsRouter.get('/:accountName/video-channels',
418d092a
C
50 asyncMiddleware(accountNameWithHostGetValidator),
51 asyncMiddleware(listAccountChannels)
52)
53
54accountsRouter.get('/:accountName/video-playlists',
55 optionalAuthenticate,
56 asyncMiddleware(accountNameWithHostGetValidator),
57 paginationValidator,
58 videoPlaylistsSortValidator,
59 setDefaultSort,
60 setDefaultPagination,
df0b219d 61 commonVideoPlaylistFiltersValidator,
418d092a 62 asyncMiddleware(listAccountPlaylists)
48dce1c9
C
63)
64
265ba139
C
65// ---------------------------------------------------------------------------
66
67export {
68 accountsRouter
69}
70
71// ---------------------------------------------------------------------------
72
418d092a 73function getAccount (req: express.Request, res: express.Response) {
0626e7af
C
74 const account: AccountModel = res.locals.account
75
744d0eca
C
76 if (account.isOutdated()) {
77 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
78 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
79 }
80
0626e7af 81 return res.json(account.toFormattedJSON())
265ba139
C
82}
83
418d092a 84async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
85 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
86
87 return res.json(getFormattedObjects(resultList.data, resultList.total))
88}
0626e7af 89
418d092a 90async function listAccountChannels (req: express.Request, res: express.Response) {
48dce1c9
C
91 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
92
93 return res.json(getFormattedObjects(resultList.data, resultList.total))
94}
95
418d092a
C
96async function listAccountPlaylists (req: express.Request, res: express.Response) {
97 const serverActor = await getServerActor()
98
99 // Allow users to see their private/unlisted video playlists
100 let privateAndUnlisted = false
101 if (res.locals.oauth && (res.locals.oauth.token.User as UserModel).Account.id === res.locals.account.id) {
102 privateAndUnlisted = true
103 }
104
105 const resultList = await VideoPlaylistModel.listForApi({
106 followerActorId: serverActor.id,
107 start: req.query.start,
108 count: req.query.count,
109 sort: req.query.sort,
110 accountId: res.locals.account.id,
df0b219d
C
111 privateAndUnlisted,
112 type: req.query.playlistType
418d092a
C
113 })
114
115 return res.json(getFormattedObjects(resultList.data, resultList.total))
116}
117
118async function listAccountVideos (req: express.Request, res: express.Response) {
0626e7af 119 const account: AccountModel = res.locals.account
4e74e803 120 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
0626e7af 121
48dce1c9 122 const resultList = await VideoModel.listForApi({
4e74e803 123 followerActorId,
48dce1c9
C
124 start: req.query.start,
125 count: req.query.count,
126 sort: req.query.sort,
8a19bee1 127 includeLocalVideos: true,
d525fc39
C
128 categoryOneOf: req.query.categoryOneOf,
129 licenceOneOf: req.query.licenceOneOf,
130 languageOneOf: req.query.languageOneOf,
131 tagsOneOf: req.query.tagsOneOf,
132 tagsAllOf: req.query.tagsAllOf,
1cd3facc 133 filter: req.query.filter,
d525fc39 134 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 135 withFiles: false,
1cd3facc 136 accountId: account.id,
7ad9b984 137 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
48dce1c9 138 })
0626e7af
C
139
140 return res.json(getFormattedObjects(resultList.data, resultList.total))
141}