1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
5 commonVideosFiltersValidator,
6 listVideoAccountChannelsValidator,
11 } from '../../middlewares'
12 import { accountsNameWithHostGetValidator, 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'
18 const accountsRouter = express.Router()
20 accountsRouter.get('/',
22 accountsSortValidator,
25 asyncMiddleware(listAccounts)
28 accountsRouter.get('/:accountName',
29 asyncMiddleware(accountsNameWithHostGetValidator),
33 accountsRouter.get('/:accountName/videos',
34 asyncMiddleware(accountsNameWithHostGetValidator),
40 commonVideosFiltersValidator,
41 asyncMiddleware(listAccountVideos)
44 accountsRouter.get('/:accountName/video-channels',
45 asyncMiddleware(listVideoAccountChannelsValidator),
46 asyncMiddleware(listVideoAccountChannels)
49 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
58 const account: AccountModel = res.locals.account
60 return res.json(account.toFormattedJSON())
63 async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
64 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
66 return res.json(getFormattedObjects(resultList.data, resultList.total))
69 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
70 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
72 return res.json(getFormattedObjects(resultList.data, resultList.total))
75 async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
76 const account: AccountModel = res.locals.account
77 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
79 const resultList = await VideoModel.listForApi({
81 start: req.query.start,
82 count: req.query.count,
84 includeLocalVideos: true,
85 categoryOneOf: req.query.categoryOneOf,
86 licenceOneOf: req.query.licenceOneOf,
87 languageOneOf: req.query.languageOneOf,
88 tagsOneOf: req.query.tagsOneOf,
89 tagsAllOf: req.query.tagsAllOf,
90 filter: req.query.filter,
91 nsfw: buildNSFWFilter(res, req.query.nsfw),
93 accountId: account.id,
94 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
97 return res.json(getFormattedObjects(resultList.data, resultList.total))