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 } from '../../middlewares/validators'
22 import { AccountModel } from '../../models/account/account'
23 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
24 import { VideoModel } from '../../models/video/video'
25 import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
26 import { VideoChannelModel } from '../../models/video/video-channel'
27 import { JobQueue } from '../../lib/job-queue'
28 import { VideoPlaylistModel } from '../../models/video/video-playlist'
29 import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
31 const accountsRouter = express.Router()
33 accountsRouter.get('/',
35 accountsSortValidator,
38 asyncMiddleware(listAccounts)
41 accountsRouter.get('/:accountName',
42 asyncMiddleware(accountNameWithHostGetValidator),
46 accountsRouter.get('/:accountName/videos',
47 asyncMiddleware(accountNameWithHostGetValidator),
53 commonVideosFiltersValidator,
54 asyncMiddleware(listAccountVideos)
57 accountsRouter.get('/:accountName/video-channels',
58 asyncMiddleware(accountNameWithHostGetValidator),
60 videoChannelsSortValidator,
63 asyncMiddleware(listAccountChannels)
66 accountsRouter.get('/:accountName/video-playlists',
68 asyncMiddleware(accountNameWithHostGetValidator),
70 videoPlaylistsSortValidator,
73 commonVideoPlaylistFiltersValidator,
74 videoPlaylistsSearchValidator,
75 asyncMiddleware(listAccountPlaylists)
78 accountsRouter.get('/:accountName/ratings',
80 asyncMiddleware(accountNameWithHostGetValidator),
81 ensureAuthUserOwnsAccountValidator,
83 videoRatesSortValidator,
87 asyncMiddleware(listAccountRatings)
90 // ---------------------------------------------------------------------------
96 // ---------------------------------------------------------------------------
98 function getAccount (req: express.Request, res: express.Response) {
99 const account = res.locals.account
101 if (account.isOutdated()) {
102 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
105 return res.json(account.toFormattedJSON())
108 async function listAccounts (req: express.Request, res: express.Response) {
109 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
111 return res.json(getFormattedObjects(resultList.data, resultList.total))
114 async function listAccountChannels (req: express.Request, res: express.Response) {
116 accountId: res.locals.account.id,
117 start: req.query.start,
118 count: req.query.count,
122 const resultList = await VideoChannelModel.listByAccount(options)
124 return res.json(getFormattedObjects(resultList.data, resultList.total))
127 async function listAccountPlaylists (req: express.Request, res: express.Response) {
128 const serverActor = await getServerActor()
130 // Allow users to see their private/unlisted video playlists
131 let listMyPlaylists = false
132 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
133 listMyPlaylists = true
136 const resultList = await VideoPlaylistModel.listForApi({
137 search: req.query.search,
138 followerActorId: serverActor.id,
139 start: req.query.start,
140 count: req.query.count,
141 sort: req.query.sort,
142 accountId: res.locals.account.id,
144 type: req.query.playlistType
147 return res.json(getFormattedObjects(resultList.data, resultList.total))
150 async function listAccountVideos (req: express.Request, res: express.Response) {
151 const account = res.locals.account
152 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
153 const countVideos = getCountVideos(req)
155 const resultList = await VideoModel.listForApi({
157 start: req.query.start,
158 count: req.query.count,
159 sort: req.query.sort,
160 includeLocalVideos: true,
161 categoryOneOf: req.query.categoryOneOf,
162 licenceOneOf: req.query.licenceOneOf,
163 languageOneOf: req.query.languageOneOf,
164 tagsOneOf: req.query.tagsOneOf,
165 tagsAllOf: req.query.tagsAllOf,
166 filter: req.query.filter,
167 nsfw: buildNSFWFilter(res, req.query.nsfw),
169 accountId: account.id,
170 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
174 return res.json(getFormattedObjects(resultList.data, resultList.total))
177 async function listAccountRatings (req: express.Request, res: express.Response) {
178 const account = res.locals.account
180 const resultList = await AccountVideoRateModel.listByAccountForApi({
181 accountId: account.id,
182 start: req.query.start,
183 count: req.query.count,
184 sort: req.query.sort,
185 type: req.query.rating
187 return res.json(getFormattedObjects(resultList.rows, resultList.count))