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'
31 commonVideoPlaylistFiltersValidator,
32 videoPlaylistsSearchValidator
33 } from '../../middlewares/validators/videos/video-playlists'
35 const accountsRouter = express.Router()
37 accountsRouter.get('/',
39 accountsSortValidator,
42 asyncMiddleware(listAccounts)
45 accountsRouter.get('/:accountName',
46 asyncMiddleware(accountNameWithHostGetValidator),
50 accountsRouter.get('/:accountName/videos',
51 asyncMiddleware(accountNameWithHostGetValidator),
57 commonVideosFiltersValidator,
58 asyncMiddleware(listAccountVideos)
61 accountsRouter.get('/:accountName/video-channels',
62 asyncMiddleware(accountNameWithHostGetValidator),
64 videoChannelsSortValidator,
67 asyncMiddleware(listAccountChannels)
70 accountsRouter.get('/:accountName/video-playlists',
72 asyncMiddleware(accountNameWithHostGetValidator),
74 videoPlaylistsSortValidator,
77 commonVideoPlaylistFiltersValidator,
78 videoPlaylistsSearchValidator,
79 asyncMiddleware(listAccountPlaylists)
82 accountsRouter.get('/:accountName/ratings',
84 asyncMiddleware(accountNameWithHostGetValidator),
85 ensureAuthUserOwnsAccountValidator,
87 videoRatesSortValidator,
91 asyncMiddleware(listAccountRatings)
94 // ---------------------------------------------------------------------------
100 // ---------------------------------------------------------------------------
102 function getAccount (req: express.Request, res: express.Response) {
103 const account = res.locals.account
105 if (account.isOutdated()) {
106 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
107 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
110 return res.json(account.toFormattedJSON())
113 async function listAccounts (req: express.Request, res: express.Response) {
114 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
116 return res.json(getFormattedObjects(resultList.data, resultList.total))
119 async function listAccountChannels (req: express.Request, res: express.Response) {
121 accountId: res.locals.account.id,
122 start: req.query.start,
123 count: req.query.count,
127 const resultList = await VideoChannelModel.listByAccount(options)
129 return res.json(getFormattedObjects(resultList.data, resultList.total))
132 async function listAccountPlaylists (req: express.Request, res: express.Response) {
133 const serverActor = await getServerActor()
135 // Allow users to see their private/unlisted video playlists
136 let privateAndUnlisted = false
137 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
138 privateAndUnlisted = true
141 const resultList = await VideoPlaylistModel.listForApi({
142 search: req.query.search,
143 followerActorId: serverActor.id,
144 start: req.query.start,
145 count: req.query.count,
146 sort: req.query.sort,
147 accountId: res.locals.account.id,
149 type: req.query.playlistType
152 return res.json(getFormattedObjects(resultList.data, resultList.total))
155 async function listAccountVideos (req: express.Request, res: express.Response) {
156 const account = res.locals.account
157 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
159 const resultList = await VideoModel.listForApi({
161 start: req.query.start,
162 count: req.query.count,
163 sort: req.query.sort,
164 includeLocalVideos: true,
165 categoryOneOf: req.query.categoryOneOf,
166 licenceOneOf: req.query.licenceOneOf,
167 languageOneOf: req.query.languageOneOf,
168 tagsOneOf: req.query.tagsOneOf,
169 tagsAllOf: req.query.tagsAllOf,
170 filter: req.query.filter,
171 nsfw: buildNSFWFilter(res, req.query.nsfw),
173 accountId: account.id,
174 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
177 return res.json(getFormattedObjects(resultList.data, resultList.total))
180 async function listAccountRatings (req: express.Request, res: express.Response) {
181 const account = res.locals.account
183 const resultList = await AccountVideoRateModel.listByAccountForApi({
184 accountId: account.id,
185 start: req.query.start,
186 count: req.query.count,
187 sort: req.query.sort,
188 type: req.query.rating
190 return res.json(getFormattedObjects(resultList.rows, resultList.count))