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,
19 videoChannelsSortValidator,
21 videoChannelStatsValidator
22 } from '../../middlewares/validators'
23 import { AccountModel } from '../../models/account/account'
24 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
25 import { VideoModel } from '../../models/video/video'
26 import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
27 import { VideoChannelModel } from '../../models/video/video-channel'
28 import { JobQueue } from '../../lib/job-queue'
29 import { VideoPlaylistModel } from '../../models/video/video-playlist'
30 import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } 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),
60 videoChannelStatsValidator,
62 videoChannelsSortValidator,
65 asyncMiddleware(listAccountChannels)
68 accountsRouter.get('/:accountName/video-playlists',
70 asyncMiddleware(accountNameWithHostGetValidator),
72 videoPlaylistsSortValidator,
75 commonVideoPlaylistFiltersValidator,
76 videoPlaylistsSearchValidator,
77 asyncMiddleware(listAccountPlaylists)
80 accountsRouter.get('/:accountName/ratings',
82 asyncMiddleware(accountNameWithHostGetValidator),
83 ensureAuthUserOwnsAccountValidator,
85 videoRatesSortValidator,
89 asyncMiddleware(listAccountRatings)
92 // ---------------------------------------------------------------------------
98 // ---------------------------------------------------------------------------
100 function getAccount (req: express.Request, res: express.Response) {
101 const account = res.locals.account
103 if (account.isOutdated()) {
104 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
107 return res.json(account.toFormattedJSON())
110 async function listAccounts (req: express.Request, res: express.Response) {
111 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
113 return res.json(getFormattedObjects(resultList.data, resultList.total))
116 async function listAccountChannels (req: express.Request, res: express.Response) {
118 accountId: res.locals.account.id,
119 start: req.query.start,
120 count: req.query.count,
121 sort: req.query.sort,
122 withStats: req.query.withStats
125 const resultList = await VideoChannelModel.listByAccount(options)
127 return res.json(getFormattedObjects(resultList.data, resultList.total))
130 async function listAccountPlaylists (req: express.Request, res: express.Response) {
131 const serverActor = await getServerActor()
133 // Allow users to see their private/unlisted video playlists
134 let listMyPlaylists = false
135 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
136 listMyPlaylists = true
139 const resultList = await VideoPlaylistModel.listForApi({
140 search: req.query.search,
141 followerActorId: serverActor.id,
142 start: req.query.start,
143 count: req.query.count,
144 sort: req.query.sort,
145 accountId: res.locals.account.id,
147 type: req.query.playlistType
150 return res.json(getFormattedObjects(resultList.data, resultList.total))
153 async function listAccountVideos (req: express.Request, res: express.Response) {
154 const account = res.locals.account
155 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
156 const countVideos = getCountVideos(req)
158 const resultList = await VideoModel.listForApi({
160 start: req.query.start,
161 count: req.query.count,
162 sort: req.query.sort,
163 includeLocalVideos: true,
164 categoryOneOf: req.query.categoryOneOf,
165 licenceOneOf: req.query.licenceOneOf,
166 languageOneOf: req.query.languageOneOf,
167 tagsOneOf: req.query.tagsOneOf,
168 tagsAllOf: req.query.tagsAllOf,
169 filter: req.query.filter,
170 nsfw: buildNSFWFilter(res, req.query.nsfw),
172 accountId: account.id,
173 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))