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'
17 import { JobQueue } from '../../lib/job-queue'
18 import { logger } from '../../helpers/logger'
20 const accountsRouter = express.Router()
22 accountsRouter.get('/',
24 accountsSortValidator,
27 asyncMiddleware(listAccounts)
30 accountsRouter.get('/:accountName',
31 asyncMiddleware(accountsNameWithHostGetValidator),
35 accountsRouter.get('/:accountName/videos',
36 asyncMiddleware(accountsNameWithHostGetValidator),
42 commonVideosFiltersValidator,
43 asyncMiddleware(listAccountVideos)
46 accountsRouter.get('/:accountName/video-channels',
47 asyncMiddleware(listVideoAccountChannelsValidator),
48 asyncMiddleware(listVideoAccountChannels)
51 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
60 const account: AccountModel = res.locals.account
62 if (account.isOutdated()) {
63 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
64 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
67 return res.json(account.toFormattedJSON())
70 async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
71 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
73 return res.json(getFormattedObjects(resultList.data, resultList.total))
76 async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) {
77 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
79 return res.json(getFormattedObjects(resultList.data, resultList.total))
82 async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
83 const account: AccountModel = res.locals.account
84 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
86 const resultList = await VideoModel.listForApi({
88 start: req.query.start,
89 count: req.query.count,
91 includeLocalVideos: true,
92 categoryOneOf: req.query.categoryOneOf,
93 licenceOneOf: req.query.licenceOneOf,
94 languageOneOf: req.query.languageOneOf,
95 tagsOneOf: req.query.tagsOneOf,
96 tagsAllOf: req.query.tagsAllOf,
97 filter: req.query.filter,
98 nsfw: buildNSFWFilter(res, req.query.nsfw),
100 accountId: account.id,
101 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
104 return res.json(getFormattedObjects(resultList.data, resultList.total))