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'
22 const accountsRouter = express.Router()
24 accountsRouter.get('/',
26 accountsSortValidator,
29 asyncMiddleware(listAccounts)
32 accountsRouter.get('/:accountName',
33 asyncMiddleware(accountNameWithHostGetValidator),
37 accountsRouter.get('/:accountName/videos',
38 asyncMiddleware(accountNameWithHostGetValidator),
44 commonVideosFiltersValidator,
45 asyncMiddleware(listAccountVideos)
48 accountsRouter.get('/:accountName/video-channels',
49 asyncMiddleware(accountNameWithHostGetValidator),
50 asyncMiddleware(listAccountChannels)
53 accountsRouter.get('/:accountName/video-playlists',
55 asyncMiddleware(accountNameWithHostGetValidator),
57 videoPlaylistsSortValidator,
60 asyncMiddleware(listAccountPlaylists)
63 // ---------------------------------------------------------------------------
69 // ---------------------------------------------------------------------------
71 function getAccount (req: express.Request, res: express.Response) {
72 const account: AccountModel = res.locals.account
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 }))
79 return res.json(account.toFormattedJSON())
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)
85 return res.json(getFormattedObjects(resultList.data, resultList.total))
88 async function listAccountChannels (req: express.Request, res: express.Response) {
89 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
91 return res.json(getFormattedObjects(resultList.data, resultList.total))
94 async function listAccountPlaylists (req: express.Request, res: express.Response) {
95 const serverActor = await getServerActor()
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
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,
112 return res.json(getFormattedObjects(resultList.data, resultList.total))
115 async function listAccountVideos (req: express.Request, res: express.Response) {
116 const account: AccountModel = res.locals.account
117 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
119 const resultList = await VideoModel.listForApi({
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),
133 accountId: account.id,
134 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
137 return res.json(getFormattedObjects(resultList.data, resultList.total))