1 import * as express from 'express'
2 import { getServerActor } from '@server/models/application/application'
3 import { VideosWithSearchCommonQuery } from '@shared/models'
4 import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
5 import { getFormattedObjects } from '../../helpers/utils'
6 import { JobQueue } from '../../lib/job-queue'
7 import { Hooks } from '../../lib/plugins/hooks'
11 commonVideosFiltersValidator,
17 videoPlaylistsSortValidator,
18 videoRatesSortValidator,
20 } from '../../middlewares'
22 accountNameWithHostGetValidator,
23 accountsSortValidator,
24 ensureAuthUserOwnsAccountValidator,
25 videoChannelsSortValidator,
26 videoChannelStatsValidator,
28 } from '../../middlewares/validators'
29 import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
30 import { AccountModel } from '../../models/account/account'
31 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
32 import { VideoModel } from '../../models/video/video'
33 import { VideoChannelModel } from '../../models/video/video-channel'
34 import { VideoPlaylistModel } from '../../models/video/video-playlist'
36 const accountsRouter = express.Router()
38 accountsRouter.get('/',
40 accountsSortValidator,
43 asyncMiddleware(listAccounts)
46 accountsRouter.get('/:accountName',
47 asyncMiddleware(accountNameWithHostGetValidator),
51 accountsRouter.get('/:accountName/videos',
52 asyncMiddleware(accountNameWithHostGetValidator),
58 commonVideosFiltersValidator,
59 asyncMiddleware(listAccountVideos)
62 accountsRouter.get('/:accountName/video-channels',
63 asyncMiddleware(accountNameWithHostGetValidator),
64 videoChannelStatsValidator,
66 videoChannelsSortValidator,
69 asyncMiddleware(listAccountChannels)
72 accountsRouter.get('/:accountName/video-playlists',
74 asyncMiddleware(accountNameWithHostGetValidator),
76 videoPlaylistsSortValidator,
79 commonVideoPlaylistFiltersValidator,
80 videoPlaylistsSearchValidator,
81 asyncMiddleware(listAccountPlaylists)
84 accountsRouter.get('/:accountName/ratings',
86 asyncMiddleware(accountNameWithHostGetValidator),
87 ensureAuthUserOwnsAccountValidator,
89 videoRatesSortValidator,
93 asyncMiddleware(listAccountRatings)
96 // ---------------------------------------------------------------------------
102 // ---------------------------------------------------------------------------
104 function getAccount (req: express.Request, res: express.Response) {
105 const account = res.locals.account
107 if (account.isOutdated()) {
108 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
111 return res.json(account.toFormattedJSON())
114 async function listAccounts (req: express.Request, res: express.Response) {
115 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
117 return res.json(getFormattedObjects(resultList.data, resultList.total))
120 async function listAccountChannels (req: express.Request, res: express.Response) {
122 accountId: res.locals.account.id,
123 start: req.query.start,
124 count: req.query.count,
125 sort: req.query.sort,
126 withStats: req.query.withStats,
127 search: req.query.search
130 const resultList = await VideoChannelModel.listByAccount(options)
132 return res.json(getFormattedObjects(resultList.data, resultList.total))
135 async function listAccountPlaylists (req: express.Request, res: express.Response) {
136 const serverActor = await getServerActor()
138 // Allow users to see their private/unlisted video playlists
139 let listMyPlaylists = false
140 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
141 listMyPlaylists = true
144 const resultList = await VideoPlaylistModel.listForApi({
145 search: req.query.search,
146 followerActorId: serverActor.id,
147 start: req.query.start,
148 count: req.query.count,
149 sort: req.query.sort,
150 accountId: res.locals.account.id,
152 type: req.query.playlistType
155 return res.json(getFormattedObjects(resultList.data, resultList.total))
158 async function listAccountVideos (req: express.Request, res: express.Response) {
159 const account = res.locals.account
160 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
161 const countVideos = getCountVideos(req)
162 const query = req.query as VideosWithSearchCommonQuery
164 const apiOptions = await Hooks.wrapObject({
169 includeLocalVideos: true,
170 categoryOneOf: query.categoryOneOf,
171 licenceOneOf: query.licenceOneOf,
172 languageOneOf: query.languageOneOf,
173 tagsOneOf: query.tagsOneOf,
174 tagsAllOf: query.tagsAllOf,
175 filter: query.filter,
176 isLive: query.isLive,
177 nsfw: buildNSFWFilter(res, query.nsfw),
179 accountId: account.id,
180 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
183 }, 'filter:api.accounts.videos.list.params')
185 const resultList = await Hooks.wrapPromiseFun(
186 VideoModel.listForApi,
188 'filter:api.accounts.videos.list.result'
191 return res.json(getFormattedObjects(resultList.data, resultList.total))
194 async function listAccountRatings (req: express.Request, res: express.Response) {
195 const account = res.locals.account
197 const resultList = await AccountVideoRateModel.listByAccountForApi({
198 accountId: account.id,
199 start: req.query.start,
200 count: req.query.count,
201 sort: req.query.sort,
202 type: req.query.rating
204 return res.json(getFormattedObjects(resultList.rows, resultList.count))