1 import * as express from 'express'
2 import { getFormattedObjects, getServerActor } from '../../helpers/utils'
6 commonVideosFiltersValidator,
11 videoPlaylistsSortValidator,
12 videoRatesSortValidator,
14 } from '../../middlewares'
16 accountNameWithHostGetValidator,
17 accountsSortValidator,
18 ensureAuthUserOwnsAccountValidator,
20 } from '../../middlewares/validators'
21 import { AccountModel } from '../../models/account/account'
22 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
23 import { VideoModel } from '../../models/video/video'
24 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
25 import { VideoChannelModel } from '../../models/video/video-channel'
26 import { JobQueue } from '../../lib/job-queue'
27 import { logger } from '../../helpers/logger'
28 import { VideoPlaylistModel } from '../../models/video/video-playlist'
29 import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
31 const accountsRouter = express.Router()
33 accountsRouter.get('/',
35 accountsSortValidator,
38 asyncMiddleware(listAccounts)
41 accountsRouter.get('/:accountName',
42 asyncMiddleware(accountNameWithHostGetValidator),
46 accountsRouter.get('/:accountName/videos',
47 asyncMiddleware(accountNameWithHostGetValidator),
53 commonVideosFiltersValidator,
54 asyncMiddleware(listAccountVideos)
57 accountsRouter.get('/:accountName/video-channels',
58 asyncMiddleware(accountNameWithHostGetValidator),
59 asyncMiddleware(listAccountChannels)
62 accountsRouter.get('/:accountName/video-playlists',
64 asyncMiddleware(accountNameWithHostGetValidator),
66 videoPlaylistsSortValidator,
69 commonVideoPlaylistFiltersValidator,
70 asyncMiddleware(listAccountPlaylists)
73 accountsRouter.get('/:accountName/ratings',
75 asyncMiddleware(accountNameWithHostGetValidator),
76 ensureAuthUserOwnsAccountValidator,
78 videoRatesSortValidator,
82 asyncMiddleware(listAccountRatings)
85 // ---------------------------------------------------------------------------
91 // ---------------------------------------------------------------------------
93 function getAccount (req: express.Request, res: express.Response) {
94 const account = res.locals.account
96 if (account.isOutdated()) {
97 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
98 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
101 return res.json(account.toFormattedJSON())
104 async function listAccounts (req: express.Request, res: express.Response) {
105 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
107 return res.json(getFormattedObjects(resultList.data, resultList.total))
110 async function listAccountChannels (req: express.Request, res: express.Response) {
111 const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
113 return res.json(getFormattedObjects(resultList.data, resultList.total))
116 async function listAccountPlaylists (req: express.Request, res: express.Response) {
117 const serverActor = await getServerActor()
119 // Allow users to see their private/unlisted video playlists
120 let privateAndUnlisted = false
121 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
122 privateAndUnlisted = true
125 const resultList = await VideoPlaylistModel.listForApi({
126 followerActorId: serverActor.id,
127 start: req.query.start,
128 count: req.query.count,
129 sort: req.query.sort,
130 accountId: res.locals.account.id,
132 type: req.query.playlistType
135 return res.json(getFormattedObjects(resultList.data, resultList.total))
138 async function listAccountVideos (req: express.Request, res: express.Response) {
139 const account = res.locals.account
140 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
142 const resultList = await VideoModel.listForApi({
144 start: req.query.start,
145 count: req.query.count,
146 sort: req.query.sort,
147 includeLocalVideos: true,
148 categoryOneOf: req.query.categoryOneOf,
149 licenceOneOf: req.query.licenceOneOf,
150 languageOneOf: req.query.languageOneOf,
151 tagsOneOf: req.query.tagsOneOf,
152 tagsAllOf: req.query.tagsAllOf,
153 filter: req.query.filter,
154 nsfw: buildNSFWFilter(res, req.query.nsfw),
156 accountId: account.id,
157 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
160 return res.json(getFormattedObjects(resultList.data, resultList.total))
163 async function listAccountRatings (req: express.Request, res: express.Response) {
164 const account = res.locals.account
166 const resultList = await AccountVideoRateModel.listByAccountForApi({
167 accountId: account.id,
168 start: req.query.start,
169 count: req.query.count,
170 sort: req.query.sort,
171 type: req.query.rating
173 return res.json(getFormattedObjects(resultList.rows, resultList.count))