1 import * as express from 'express'
2 import { getFormattedObjects, getServerActor } from '../../helpers/utils'
5 commonVideosFiltersValidator,
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 import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
23 const accountsRouter = express.Router()
25 accountsRouter.get('/',
27 accountsSortValidator,
30 asyncMiddleware(listAccounts)
33 accountsRouter.get('/:accountName',
34 asyncMiddleware(accountNameWithHostGetValidator),
38 accountsRouter.get('/:accountName/videos',
39 asyncMiddleware(accountNameWithHostGetValidator),
45 commonVideosFiltersValidator,
46 asyncMiddleware(listAccountVideos)
49 accountsRouter.get('/:accountName/video-channels',
50 asyncMiddleware(accountNameWithHostGetValidator),
51 asyncMiddleware(listAccountChannels)
54 accountsRouter.get('/:accountName/video-playlists',
56 asyncMiddleware(accountNameWithHostGetValidator),
58 videoPlaylistsSortValidator,
61 commonVideoPlaylistFiltersValidator,
62 asyncMiddleware(listAccountPlaylists)
65 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 function getAccount (req: express.Request, res: express.Response) {
74 const account: AccountModel = res.locals.account
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 }))
81 return res.json(account.toFormattedJSON())
84 async function listAccounts (req: express.Request, res: express.Response) {
85 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
87 return res.json(getFormattedObjects(resultList.data, resultList.total))
90 async function listAccountChannels (req: express.Request, res: express.Response) {
91 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
93 return res.json(getFormattedObjects(resultList.data, resultList.total))
96 async function listAccountPlaylists (req: express.Request, res: express.Response) {
97 const serverActor = await getServerActor()
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
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,
112 type: req.query.playlistType
115 return res.json(getFormattedObjects(resultList.data, resultList.total))
118 async function listAccountVideos (req: express.Request, res: express.Response) {
119 const account: AccountModel = res.locals.account
120 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
122 const resultList = await VideoModel.listForApi({
124 start: req.query.start,
125 count: req.query.count,
126 sort: req.query.sort,
127 includeLocalVideos: true,
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,
133 filter: req.query.filter,
134 nsfw: buildNSFWFilter(res, req.query.nsfw),
136 accountId: account.id,
137 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
140 return res.json(getFormattedObjects(resultList.data, resultList.total))