1 import * as express from 'express'
2 import { getServerActor } from '@server/models/application/application'
3 import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
4 import { getFormattedObjects } from '../../helpers/utils'
5 import { JobQueue } from '../../lib/job-queue'
9 commonVideosFiltersValidator,
15 videoPlaylistsSortValidator,
16 videoRatesSortValidator,
18 } from '../../middlewares'
20 accountNameWithHostGetValidator,
21 accountsSortValidator,
22 ensureAuthUserOwnsAccountValidator,
23 videoChannelsSortValidator,
24 videoChannelStatsValidator,
26 } from '../../middlewares/validators'
27 import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
28 import { AccountModel } from '../../models/account/account'
29 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
30 import { VideoModel } from '../../models/video/video'
31 import { VideoChannelModel } from '../../models/video/video-channel'
32 import { VideoPlaylistModel } from '../../models/video/video-playlist'
34 const accountsRouter = express.Router()
36 accountsRouter.get('/',
38 accountsSortValidator,
41 asyncMiddleware(listAccounts)
44 accountsRouter.get('/:accountName',
45 asyncMiddleware(accountNameWithHostGetValidator),
49 accountsRouter.get('/:accountName/videos',
50 asyncMiddleware(accountNameWithHostGetValidator),
56 commonVideosFiltersValidator,
57 asyncMiddleware(listAccountVideos)
60 accountsRouter.get('/:accountName/video-channels',
61 asyncMiddleware(accountNameWithHostGetValidator),
62 videoChannelStatsValidator,
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 } })
109 return res.json(account.toFormattedJSON())
112 async function listAccounts (req: express.Request, res: express.Response) {
113 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
115 return res.json(getFormattedObjects(resultList.data, resultList.total))
118 async function listAccountChannels (req: express.Request, res: express.Response) {
120 accountId: res.locals.account.id,
121 start: req.query.start,
122 count: req.query.count,
123 sort: req.query.sort,
124 withStats: req.query.withStats,
125 search: req.query.search
128 const resultList = await VideoChannelModel.listByAccount(options)
130 return res.json(getFormattedObjects(resultList.data, resultList.total))
133 async function listAccountPlaylists (req: express.Request, res: express.Response) {
134 const serverActor = await getServerActor()
136 // Allow users to see their private/unlisted video playlists
137 let listMyPlaylists = false
138 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
139 listMyPlaylists = true
142 const resultList = await VideoPlaylistModel.listForApi({
143 search: req.query.search,
144 followerActorId: serverActor.id,
145 start: req.query.start,
146 count: req.query.count,
147 sort: req.query.sort,
148 accountId: res.locals.account.id,
150 type: req.query.playlistType
153 return res.json(getFormattedObjects(resultList.data, resultList.total))
156 async function listAccountVideos (req: express.Request, res: express.Response) {
157 const account = res.locals.account
158 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
159 const countVideos = getCountVideos(req)
161 const resultList = await VideoModel.listForApi({
163 start: req.query.start,
164 count: req.query.count,
165 sort: req.query.sort,
166 includeLocalVideos: true,
167 categoryOneOf: req.query.categoryOneOf,
168 licenceOneOf: req.query.licenceOneOf,
169 languageOneOf: req.query.languageOneOf,
170 tagsOneOf: req.query.tagsOneOf,
171 tagsAllOf: req.query.tagsAllOf,
172 filter: req.query.filter,
173 nsfw: buildNSFWFilter(res, req.query.nsfw),
175 accountId: account.id,
176 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
180 return res.json(getFormattedObjects(resultList.data, resultList.total))
183 async function listAccountRatings (req: express.Request, res: express.Response) {
184 const account = res.locals.account
186 const resultList = await AccountVideoRateModel.listByAccountForApi({
187 accountId: account.id,
188 start: req.query.start,
189 count: req.query.count,
190 sort: req.query.sort,
191 type: req.query.rating
193 return res.json(getFormattedObjects(resultList.rows, resultList.count))