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 { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
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 commonVideoPlaylistFiltersValidator,
61 asyncMiddleware(listAccountPlaylists)
64 // ---------------------------------------------------------------------------
70 // ---------------------------------------------------------------------------
72 function getAccount (req: express.Request, res: express.Response) {
73 const account = res.locals.account
75 if (account.isOutdated()) {
76 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
77 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
80 return res.json(account.toFormattedJSON())
83 async function listAccounts (req: express.Request, res: express.Response) {
84 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
86 return res.json(getFormattedObjects(resultList.data, resultList.total))
89 async function listAccountChannels (req: express.Request, res: express.Response) {
90 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
92 return res.json(getFormattedObjects(resultList.data, resultList.total))
95 async function listAccountPlaylists (req: express.Request, res: express.Response) {
96 const serverActor = await getServerActor()
98 // Allow users to see their private/unlisted video playlists
99 let privateAndUnlisted = false
100 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
101 privateAndUnlisted = true
104 const resultList = await VideoPlaylistModel.listForApi({
105 followerActorId: serverActor.id,
106 start: req.query.start,
107 count: req.query.count,
108 sort: req.query.sort,
109 accountId: res.locals.account.id,
111 type: req.query.playlistType
114 return res.json(getFormattedObjects(resultList.data, resultList.total))
117 async function listAccountVideos (req: express.Request, res: express.Response) {
118 const account = res.locals.account
119 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
121 const resultList = await VideoModel.listForApi({
123 start: req.query.start,
124 count: req.query.count,
125 sort: req.query.sort,
126 includeLocalVideos: true,
127 categoryOneOf: req.query.categoryOneOf,
128 licenceOneOf: req.query.licenceOneOf,
129 languageOneOf: req.query.languageOneOf,
130 tagsOneOf: req.query.tagsOneOf,
131 tagsAllOf: req.query.tagsAllOf,
132 filter: req.query.filter,
133 nsfw: buildNSFWFilter(res, req.query.nsfw),
135 accountId: account.id,
136 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
139 return res.json(getFormattedObjects(resultList.data, resultList.total))