1 import * as express from 'express'
2 import { buildNSFWFilter } from '../../helpers/express-utils'
3 import { VideoModel } from '../../models/video/video'
4 import { asyncMiddleware, optionalAuthenticate, videosOverviewValidator } from '../../middlewares'
5 import { TagModel } from '../../models/video/tag'
6 import { CategoryOverview, ChannelOverview, TagOverview, VideosOverview } from '../../../shared/models/overviews'
7 import { MEMOIZE_TTL, OVERVIEWS } from '../../initializers/constants'
8 import * as memoizee from 'memoizee'
9 import { logger } from '@server/helpers/logger'
11 const overviewsRouter = express.Router()
13 overviewsRouter.get('/videos',
14 videosOverviewValidator,
16 asyncMiddleware(getVideosOverview)
19 // ---------------------------------------------------------------------------
21 export { overviewsRouter }
23 // ---------------------------------------------------------------------------
25 const buildSamples = memoizee(async function () {
26 const [ categories, channels, tags ] = await Promise.all([
27 VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
28 VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
29 TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
32 const result = { categories, channels, tags }
34 logger.debug('Building samples for overview endpoint.', { result })
37 }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
39 // This endpoint could be quite long, but we cache it
40 async function getVideosOverview (req: express.Request, res: express.Response) {
41 const attributes = await buildSamples()
43 const page = req.query.page || 1
44 const index = page - 1
46 const categories: CategoryOverview[] = []
47 const channels: ChannelOverview[] = []
48 const tags: TagOverview[] = []
51 getVideosByCategory(attributes.categories, index, res, categories),
52 getVideosByChannel(attributes.channels, index, res, channels),
53 getVideosByTag(attributes.tags, index, res, tags)
56 const result: VideosOverview = {
62 return res.json(result)
65 async function getVideosByTag (tagsSample: string[], index: number, res: express.Response, acc: TagOverview[]) {
66 if (tagsSample.length <= index) return
68 const tag = tagsSample[index]
69 const videos = await getVideos(res, { tagsOneOf: [ tag ] })
71 if (videos.length === 0) return
79 async function getVideosByCategory (categoriesSample: number[], index: number, res: express.Response, acc: CategoryOverview[]) {
80 if (categoriesSample.length <= index) return
82 const category = categoriesSample[index]
83 const videos = await getVideos(res, { categoryOneOf: [ category ] })
85 if (videos.length === 0) return
88 category: videos[0].category,
93 async function getVideosByChannel (channelsSample: number[], index: number, res: express.Response, acc: ChannelOverview[]) {
94 if (channelsSample.length <= index) return
96 const channelId = channelsSample[index]
97 const videos = await getVideos(res, { videoChannelId: channelId })
99 if (videos.length === 0) return
102 channel: videos[0].channel,
107 async function getVideos (
108 res: express.Response,
109 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
111 const query = Object.assign({
115 includeLocalVideos: true,
116 nsfw: buildNSFWFilter(res),
117 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
122 const { data } = await VideoModel.listForApi(query)
124 return data.map(d => d.toFormattedJSON())