1 import express from 'express'
2 import memoizee from 'memoizee'
3 import { logger } from '@server/helpers/logger'
4 import { Hooks } from '@server/lib/plugins/hooks'
5 import { VideoModel } from '@server/models/video/video'
6 import { CategoryOverview, ChannelOverview, TagOverview, VideosOverview } from '../../../shared/models/overviews'
7 import { buildNSFWFilter } from '../../helpers/express-utils'
8 import { MEMOIZE_TTL, OVERVIEWS } from '../../initializers/constants'
9 import { asyncMiddleware, optionalAuthenticate, videosOverviewValidator } from '../../middlewares'
10 import { TagModel } from '../../models/video/tag'
12 const overviewsRouter = express.Router()
14 overviewsRouter.get('/videos',
15 videosOverviewValidator,
17 asyncMiddleware(getVideosOverview)
20 // ---------------------------------------------------------------------------
22 export { overviewsRouter }
24 // ---------------------------------------------------------------------------
26 const buildSamples = memoizee(async function () {
27 const [ categories, channels, tags ] = await Promise.all([
28 VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
29 VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
30 TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
33 const result = { categories, channels, tags }
35 logger.debug('Building samples for overview endpoint.', { result })
38 }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
40 // This endpoint could be quite long, but we cache it
41 async function getVideosOverview (req: express.Request, res: express.Response) {
42 const attributes = await buildSamples()
44 const page = req.query.page || 1
45 const index = page - 1
47 const categories: CategoryOverview[] = []
48 const channels: ChannelOverview[] = []
49 const tags: TagOverview[] = []
52 getVideosByCategory(attributes.categories, index, res, categories),
53 getVideosByChannel(attributes.channels, index, res, channels),
54 getVideosByTag(attributes.tags, index, res, tags)
57 const result: VideosOverview = {
63 return res.json(result)
66 async function getVideosByTag (tagsSample: string[], index: number, res: express.Response, acc: TagOverview[]) {
67 if (tagsSample.length <= index) return
69 const tag = tagsSample[index]
70 const videos = await getVideos(res, { tagsOneOf: [ tag ] })
72 if (videos.length === 0) return
80 async function getVideosByCategory (categoriesSample: number[], index: number, res: express.Response, acc: CategoryOverview[]) {
81 if (categoriesSample.length <= index) return
83 const category = categoriesSample[index]
84 const videos = await getVideos(res, { categoryOneOf: [ category ] })
86 if (videos.length === 0) return
89 category: videos[0].category,
94 async function getVideosByChannel (channelsSample: number[], index: number, res: express.Response, acc: ChannelOverview[]) {
95 if (channelsSample.length <= index) return
97 const channelId = channelsSample[index]
98 const videos = await getVideos(res, { videoChannelId: channelId })
100 if (videos.length === 0) return
103 channel: videos[0].channel,
108 async function getVideos (
109 res: express.Response,
110 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
112 const query = await Hooks.wrapObject({
116 includeLocalVideos: true,
117 nsfw: buildNSFWFilter(res),
118 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
123 }, 'filter:api.overviews.videos.list.params')
125 const { data } = await Hooks.wrapPromiseFun(
126 VideoModel.listForApi,
128 'filter:api.overviews.videos.list.result'
131 return data.map(d => d.toFormattedJSON())