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