PeerTube/server/controllers/api/overviews.ts

133 lines
4.0 KiB
TypeScript
Raw Normal View History

2018-08-30 14:58:00 +02:00
import * as express from 'express'
2021-07-16 14:27:30 +02:00
import * as memoizee from 'memoizee'
import { logger } from '@server/helpers/logger'
import { Hooks } from '@server/lib/plugins/hooks'
import { VideoModel } from '@server/models/video/video'
2021-07-16 14:27:30 +02:00
import { CategoryOverview, ChannelOverview, TagOverview, VideosOverview } from '../../../shared/models/overviews'
2018-08-30 14:58:00 +02:00
import { buildNSFWFilter } from '../../helpers/express-utils'
2021-07-16 14:27:30 +02:00
import { MEMOIZE_TTL, OVERVIEWS } from '../../initializers/constants'
import { asyncMiddleware, optionalAuthenticate, videosOverviewValidator } from '../../middlewares'
2018-08-30 14:58:00 +02:00
import { TagModel } from '../../models/video/tag'
const overviewsRouter = express.Router()
overviewsRouter.get('/videos',
videosOverviewValidator,
optionalAuthenticate,
2018-08-30 14:58:00 +02:00
asyncMiddleware(getVideosOverview)
)
// ---------------------------------------------------------------------------
export { overviewsRouter }
// ---------------------------------------------------------------------------
2018-09-14 14:57:59 +02:00
const buildSamples = memoizee(async function () {
const [ categories, channels, tags ] = await Promise.all([
VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
2020-01-31 16:56:52 +01:00
VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
2018-09-14 14:57:59 +02:00
TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
])
const result = { categories, channels, tags }
logger.debug('Building samples for overview endpoint.', { result })
return result
2018-09-14 14:57:59 +02:00
}, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
2018-08-30 14:58:00 +02:00
// This endpoint could be quite long, but we cache it
async function getVideosOverview (req: express.Request, res: express.Response) {
const attributes = await buildSamples()
2018-09-14 11:52:23 +02:00
const page = req.query.page || 1
const index = page - 1
const categories: CategoryOverview[] = []
const channels: ChannelOverview[] = []
const tags: TagOverview[] = []
await Promise.all([
getVideosByCategory(attributes.categories, index, res, categories),
getVideosByChannel(attributes.channels, index, res, channels),
getVideosByTag(attributes.tags, index, res, tags)
2018-09-14 11:52:23 +02:00
])
2018-08-30 14:58:00 +02:00
const result: VideosOverview = {
2018-09-14 11:52:23 +02:00
categories,
channels,
tags
2018-08-30 14:58:00 +02:00
}
return res.json(result)
}
async function getVideosByTag (tagsSample: string[], index: number, res: express.Response, acc: TagOverview[]) {
if (tagsSample.length <= index) return
const tag = tagsSample[index]
2018-08-30 14:58:00 +02:00
const videos = await getVideos(res, { tagsOneOf: [ tag ] })
if (videos.length === 0) return
2018-08-30 14:58:00 +02:00
acc.push({
2018-08-30 14:58:00 +02:00
tag,
videos
})
2018-08-30 14:58:00 +02:00
}
async function getVideosByCategory (categoriesSample: number[], index: number, res: express.Response, acc: CategoryOverview[]) {
if (categoriesSample.length <= index) return
const category = categoriesSample[index]
2018-08-30 14:58:00 +02:00
const videos = await getVideos(res, { categoryOneOf: [ category ] })
if (videos.length === 0) return
2018-08-30 14:58:00 +02:00
acc.push({
2018-08-30 14:58:00 +02:00
category: videos[0].category,
videos
})
2018-08-30 14:58:00 +02:00
}
async function getVideosByChannel (channelsSample: number[], index: number, res: express.Response, acc: ChannelOverview[]) {
if (channelsSample.length <= index) return
const channelId = channelsSample[index]
2018-08-30 14:58:00 +02:00
const videos = await getVideos(res, { videoChannelId: channelId })
if (videos.length === 0) return
2018-08-30 14:58:00 +02:00
acc.push({
2018-08-30 14:58:00 +02:00
channel: videos[0].channel,
videos
})
2018-08-30 14:58:00 +02:00
}
async function getVideos (
res: express.Response,
where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
) {
2021-07-21 16:13:20 +02:00
const query = await Hooks.wrapObject({
2018-08-30 14:58:00 +02:00
start: 0,
2019-04-05 17:00:09 +02:00
count: 12,
2018-08-30 14:58:00 +02:00
sort: '-createdAt',
includeLocalVideos: true,
nsfw: buildNSFWFilter(res),
2020-11-10 11:06:36 +01:00
user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
2020-01-08 14:15:16 +01:00
withFiles: false,
2021-07-21 16:13:20 +02:00
countVideos: false,
2018-09-14 11:52:23 +02:00
2021-07-21 16:13:20 +02:00
...where
}, 'filter:api.overviews.videos.list.params')
const { data } = await Hooks.wrapPromiseFun(
VideoModel.listForApi,
query,
'filter:api.overviews.videos.list.result'
)
2018-08-30 14:58:00 +02:00
return data.map(d => d.toFormattedJSON())
}