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 { Hooks } from '../../lib/plugins/hooks'
6 import { JobQueue } from '../../lib/job-queue'
10 commonVideosFiltersValidator,
16 videoPlaylistsSortValidator,
17 videoRatesSortValidator,
19 } from '../../middlewares'
21 accountNameWithHostGetValidator,
22 accountsSortValidator,
23 ensureAuthUserOwnsAccountValidator,
24 videoChannelsSortValidator,
25 videoChannelStatsValidator,
27 } from '../../middlewares/validators'
28 import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
29 import { AccountModel } from '../../models/account/account'
30 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
31 import { VideoModel } from '../../models/video/video'
32 import { VideoChannelModel } from '../../models/video/video-channel'
33 import { VideoPlaylistModel } from '../../models/video/video-playlist'
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),
63 videoChannelStatsValidator,
65 videoChannelsSortValidator,
68 asyncMiddleware(listAccountChannels)
71 accountsRouter.get('/:accountName/video-playlists',
73 asyncMiddleware(accountNameWithHostGetValidator),
75 videoPlaylistsSortValidator,
78 commonVideoPlaylistFiltersValidator,
79 videoPlaylistsSearchValidator,
80 asyncMiddleware(listAccountPlaylists)
83 accountsRouter.get('/:accountName/ratings',
85 asyncMiddleware(accountNameWithHostGetValidator),
86 ensureAuthUserOwnsAccountValidator,
88 videoRatesSortValidator,
92 asyncMiddleware(listAccountRatings)
95 // ---------------------------------------------------------------------------
101 // ---------------------------------------------------------------------------
103 function getAccount (req: express.Request, res: express.Response) {
104 const account = res.locals.account
106 if (account.isOutdated()) {
107 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
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,
124 sort: req.query.sort,
125 withStats: req.query.withStats,
126 search: req.query.search
129 const resultList = await VideoChannelModel.listByAccount(options)
131 return res.json(getFormattedObjects(resultList.data, resultList.total))
134 async function listAccountPlaylists (req: express.Request, res: express.Response) {
135 const serverActor = await getServerActor()
137 // Allow users to see their private/unlisted video playlists
138 let listMyPlaylists = false
139 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
140 listMyPlaylists = true
143 const resultList = await VideoPlaylistModel.listForApi({
144 search: req.query.search,
145 followerActorId: serverActor.id,
146 start: req.query.start,
147 count: req.query.count,
148 sort: req.query.sort,
149 accountId: res.locals.account.id,
151 type: req.query.playlistType
154 return res.json(getFormattedObjects(resultList.data, resultList.total))
157 async function listAccountVideos (req: express.Request, res: express.Response) {
158 const account = res.locals.account
159 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
160 const countVideos = getCountVideos(req)
162 const apiOptions = await Hooks.wrapObject({
164 start: req.query.start,
165 count: req.query.count,
166 sort: req.query.sort,
167 includeLocalVideos: true,
168 categoryOneOf: req.query.categoryOneOf,
169 licenceOneOf: req.query.licenceOneOf,
170 languageOneOf: req.query.languageOneOf,
171 tagsOneOf: req.query.tagsOneOf,
172 tagsAllOf: req.query.tagsAllOf,
173 filter: req.query.filter,
174 nsfw: buildNSFWFilter(res, req.query.nsfw),
176 accountId: account.id,
177 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
179 search: req.query.search
180 }, 'filter:api.accounts.videos.list.params')
182 const resultList = await Hooks.wrapPromiseFun(
183 VideoModel.listForApi,
185 'filter:api.accounts.videos.list.result'
188 return res.json(getFormattedObjects(resultList.data, resultList.total))
191 async function listAccountRatings (req: express.Request, res: express.Response) {
192 const account = res.locals.account
194 const resultList = await AccountVideoRateModel.listByAccountForApi({
195 accountId: account.id,
196 start: req.query.start,
197 count: req.query.count,
198 sort: req.query.sort,
199 type: req.query.rating
201 return res.json(getFormattedObjects(resultList.rows, resultList.count))