1 import * as express from 'express'
2 import { getFormattedObjects } 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'
31 import { getServerActor } from '@server/models/application/application'
33 const accountsRouter = express.Router()
35 accountsRouter.get('/',
37 accountsSortValidator,
40 asyncMiddleware(listAccounts)
43 accountsRouter.get('/:accountName',
44 asyncMiddleware(accountNameWithHostGetValidator),
48 accountsRouter.get('/:accountName/videos',
49 asyncMiddleware(accountNameWithHostGetValidator),
55 commonVideosFiltersValidator,
56 asyncMiddleware(listAccountVideos)
59 accountsRouter.get('/:accountName/video-channels',
60 asyncMiddleware(accountNameWithHostGetValidator),
61 videoChannelStatsValidator,
63 videoChannelsSortValidator,
66 asyncMiddleware(listAccountChannels)
69 accountsRouter.get('/:accountName/video-playlists',
71 asyncMiddleware(accountNameWithHostGetValidator),
73 videoPlaylistsSortValidator,
76 commonVideoPlaylistFiltersValidator,
77 videoPlaylistsSearchValidator,
78 asyncMiddleware(listAccountPlaylists)
81 accountsRouter.get('/:accountName/ratings',
83 asyncMiddleware(accountNameWithHostGetValidator),
84 ensureAuthUserOwnsAccountValidator,
86 videoRatesSortValidator,
90 asyncMiddleware(listAccountRatings)
93 // ---------------------------------------------------------------------------
99 // ---------------------------------------------------------------------------
101 function getAccount (req: express.Request, res: express.Response) {
102 const account = res.locals.account
104 if (account.isOutdated()) {
105 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
108 return res.json(account.toFormattedJSON())
111 async function listAccounts (req: express.Request, res: express.Response) {
112 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
114 return res.json(getFormattedObjects(resultList.data, resultList.total))
117 async function listAccountChannels (req: express.Request, res: express.Response) {
119 accountId: res.locals.account.id,
120 start: req.query.start,
121 count: req.query.count,
122 sort: req.query.sort,
123 withStats: req.query.withStats
126 const resultList = await VideoChannelModel.listByAccount(options)
128 return res.json(getFormattedObjects(resultList.data, resultList.total))
131 async function listAccountPlaylists (req: express.Request, res: express.Response) {
132 const serverActor = await getServerActor()
134 // Allow users to see their private/unlisted video playlists
135 let listMyPlaylists = false
136 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
137 listMyPlaylists = true
140 const resultList = await VideoPlaylistModel.listForApi({
141 search: req.query.search,
142 followerActorId: serverActor.id,
143 start: req.query.start,
144 count: req.query.count,
145 sort: req.query.sort,
146 accountId: res.locals.account.id,
148 type: req.query.playlistType
151 return res.json(getFormattedObjects(resultList.data, resultList.total))
154 async function listAccountVideos (req: express.Request, res: express.Response) {
155 const account = res.locals.account
156 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
157 const countVideos = getCountVideos(req)
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,
178 return res.json(getFormattedObjects(resultList.data, resultList.total))
181 async function listAccountRatings (req: express.Request, res: express.Response) {
182 const account = res.locals.account
184 const resultList = await AccountVideoRateModel.listByAccountForApi({
185 accountId: account.id,
186 start: req.query.start,
187 count: req.query.count,
188 sort: req.query.sort,
189 type: req.query.rating
191 return res.json(getFormattedObjects(resultList.rows, resultList.count))