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,
124 search: req.query.search
127 const resultList = await VideoChannelModel.listByAccount(options)
129 return res.json(getFormattedObjects(resultList.data, resultList.total))
132 async function listAccountPlaylists (req: express.Request, res: express.Response) {
133 const serverActor = await getServerActor()
135 // Allow users to see their private/unlisted video playlists
136 let listMyPlaylists = false
137 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
138 listMyPlaylists = true
141 const resultList = await VideoPlaylistModel.listForApi({
142 search: req.query.search,
143 followerActorId: serverActor.id,
144 start: req.query.start,
145 count: req.query.count,
146 sort: req.query.sort,
147 accountId: res.locals.account.id,
149 type: req.query.playlistType
152 return res.json(getFormattedObjects(resultList.data, resultList.total))
155 async function listAccountVideos (req: express.Request, res: express.Response) {
156 const account = res.locals.account
157 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
158 const countVideos = getCountVideos(req)
160 const resultList = await VideoModel.listForApi({
162 start: req.query.start,
163 count: req.query.count,
164 sort: req.query.sort,
165 includeLocalVideos: true,
166 categoryOneOf: req.query.categoryOneOf,
167 licenceOneOf: req.query.licenceOneOf,
168 languageOneOf: req.query.languageOneOf,
169 tagsOneOf: req.query.tagsOneOf,
170 tagsAllOf: req.query.tagsAllOf,
171 filter: req.query.filter,
172 nsfw: buildNSFWFilter(res, req.query.nsfw),
174 accountId: account.id,
175 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
179 return res.json(getFormattedObjects(resultList.data, resultList.total))
182 async function listAccountRatings (req: express.Request, res: express.Response) {
183 const account = res.locals.account
185 const resultList = await AccountVideoRateModel.listByAccountForApi({
186 accountId: account.id,
187 start: req.query.start,
188 count: req.query.count,
189 sort: req.query.sort,
190 type: req.query.rating
192 return res.json(getFormattedObjects(resultList.rows, resultList.count))