]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/accounts.ts
03c83109233383af1f5c3d2f6835184e4dd7fac8
[github/Chocobozzz/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 commonVideosFiltersValidator,
6 optionalAuthenticate,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultSort,
10 videoPlaylistsSortValidator
11 } from '../../middlewares'
12 import { accountNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
13 import { AccountModel } from '../../models/account/account'
14 import { VideoModel } from '../../models/video/video'
15 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
16 import { VideoChannelModel } from '../../models/video/video-channel'
17 import { JobQueue } from '../../lib/job-queue'
18 import { logger } from '../../helpers/logger'
19 import { VideoPlaylistModel } from '../../models/video/video-playlist'
20 import { UserModel } from '../../models/account/user'
21
22 const accountsRouter = express.Router()
23
24 accountsRouter.get('/',
25 paginationValidator,
26 accountsSortValidator,
27 setDefaultSort,
28 setDefaultPagination,
29 asyncMiddleware(listAccounts)
30 )
31
32 accountsRouter.get('/:accountName',
33 asyncMiddleware(accountNameWithHostGetValidator),
34 getAccount
35 )
36
37 accountsRouter.get('/:accountName/videos',
38 asyncMiddleware(accountNameWithHostGetValidator),
39 paginationValidator,
40 videosSortValidator,
41 setDefaultSort,
42 setDefaultPagination,
43 optionalAuthenticate,
44 commonVideosFiltersValidator,
45 asyncMiddleware(listAccountVideos)
46 )
47
48 accountsRouter.get('/:accountName/video-channels',
49 asyncMiddleware(accountNameWithHostGetValidator),
50 asyncMiddleware(listAccountChannels)
51 )
52
53 accountsRouter.get('/:accountName/video-playlists',
54 optionalAuthenticate,
55 asyncMiddleware(accountNameWithHostGetValidator),
56 paginationValidator,
57 videoPlaylistsSortValidator,
58 setDefaultSort,
59 setDefaultPagination,
60 asyncMiddleware(listAccountPlaylists)
61 )
62
63 // ---------------------------------------------------------------------------
64
65 export {
66 accountsRouter
67 }
68
69 // ---------------------------------------------------------------------------
70
71 function getAccount (req: express.Request, res: express.Response) {
72 const account: AccountModel = res.locals.account
73
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
79 return res.json(account.toFormattedJSON())
80 }
81
82 async function listAccounts (req: express.Request, res: express.Response) {
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 }
87
88 async function listAccountChannels (req: express.Request, res: express.Response) {
89 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
90
91 return res.json(getFormattedObjects(resultList.data, resultList.total))
92 }
93
94 async 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
115 async function listAccountVideos (req: express.Request, res: express.Response) {
116 const account: AccountModel = res.locals.account
117 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
118
119 const resultList = await VideoModel.listForApi({
120 followerActorId,
121 start: req.query.start,
122 count: req.query.count,
123 sort: req.query.sort,
124 includeLocalVideos: true,
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,
130 filter: req.query.filter,
131 nsfw: buildNSFWFilter(res, req.query.nsfw),
132 withFiles: false,
133 accountId: account.id,
134 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
135 })
136
137 return res.json(getFormattedObjects(resultList.data, resultList.total))
138 }