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 videoChannelsSortValidator
21 } from '../../middlewares/validators'
22 import { AccountModel } from '../../models/account/account'
23 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
24 import { VideoModel } from '../../models/video/video'
25 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
26 import { VideoChannelModel } from '../../models/video/video-channel'
27 import { JobQueue } from '../../lib/job-queue'
28 import { logger } from '../../helpers/logger'
29 import { VideoPlaylistModel } from '../../models/video/video-playlist'
30 import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
32 const accountsRouter = express.Router()
34 accountsRouter.get('/',
36 accountsSortValidator,
39 asyncMiddleware(listAccounts)
42 accountsRouter.get('/:accountName',
43 asyncMiddleware(accountNameWithHostGetValidator),
47 accountsRouter.get('/:accountName/videos',
48 asyncMiddleware(accountNameWithHostGetValidator),
54 commonVideosFiltersValidator,
55 asyncMiddleware(listAccountVideos)
58 accountsRouter.get('/:accountName/video-channels',
59 asyncMiddleware(accountNameWithHostGetValidator),
61 videoChannelsSortValidator,
64 asyncMiddleware(listAccountChannels)
67 accountsRouter.get('/:accountName/video-playlists',
69 asyncMiddleware(accountNameWithHostGetValidator),
71 videoPlaylistsSortValidator,
74 commonVideoPlaylistFiltersValidator,
75 asyncMiddleware(listAccountPlaylists)
78 accountsRouter.get('/:accountName/ratings',
80 asyncMiddleware(accountNameWithHostGetValidator),
81 ensureAuthUserOwnsAccountValidator,
83 videoRatesSortValidator,
87 asyncMiddleware(listAccountRatings)
90 // ---------------------------------------------------------------------------
96 // ---------------------------------------------------------------------------
98 function getAccount (req: express.Request, res: express.Response) {
99 const account = res.locals.account
101 if (account.isOutdated()) {
102 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
103 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
106 return res.json(account.toFormattedJSON())
109 async function listAccounts (req: express.Request, res: express.Response) {
110 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
112 return res.json(getFormattedObjects(resultList.data, resultList.total))
115 async function listAccountChannels (req: express.Request, res: express.Response) {
117 accountId: res.locals.account.id,
118 start: req.query.start,
119 count: req.query.count,
120 sort: req.query.sort,
123 const resultList = await VideoChannelModel.listByAccount(options)
125 return res.json(getFormattedObjects(resultList.data, resultList.total))
128 async function listAccountPlaylists (req: express.Request, res: express.Response) {
129 const serverActor = await getServerActor()
131 // Allow users to see their private/unlisted video playlists
132 let privateAndUnlisted = false
133 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
134 privateAndUnlisted = true
137 const resultList = await VideoPlaylistModel.listForApi({
138 followerActorId: serverActor.id,
139 start: req.query.start,
140 count: req.query.count,
141 sort: req.query.sort,
142 accountId: res.locals.account.id,
144 type: req.query.playlistType
147 return res.json(getFormattedObjects(resultList.data, resultList.total))
150 async function listAccountVideos (req: express.Request, res: express.Response) {
151 const account = res.locals.account
152 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
154 const resultList = await VideoModel.listForApi({
156 start: req.query.start,
157 count: req.query.count,
158 sort: req.query.sort,
159 includeLocalVideos: true,
160 categoryOneOf: req.query.categoryOneOf,
161 licenceOneOf: req.query.licenceOneOf,
162 languageOneOf: req.query.languageOneOf,
163 tagsOneOf: req.query.tagsOneOf,
164 tagsAllOf: req.query.tagsAllOf,
165 filter: req.query.filter,
166 nsfw: buildNSFWFilter(res, req.query.nsfw),
168 accountId: account.id,
169 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
172 return res.json(getFormattedObjects(resultList.data, resultList.total))
175 async function listAccountRatings (req: express.Request, res: express.Response) {
176 const account = res.locals.account
178 const resultList = await AccountVideoRateModel.listByAccountForApi({
179 accountId: account.id,
180 start: req.query.start,
181 count: req.query.count,
182 sort: req.query.sort,
183 type: req.query.rating
185 return res.json(getFormattedObjects(resultList.rows, resultList.count))