1 import * as express from 'express'
2 import { buildNSFWFilter } from '../../helpers/express-utils'
3 import { VideoModel } from '../../models/video/video'
4 import { asyncMiddleware } from '../../middlewares'
5 import { TagModel } from '../../models/video/tag'
6 import { VideosOverview } from '../../../shared/models/overviews'
7 import { MEMOIZE_TTL, OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers/constants'
8 import { cacheRoute } from '../../middlewares/cache'
9 import * as memoizee from 'memoizee'
11 const overviewsRouter = express.Router()
13 overviewsRouter.get('/videos',
14 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS)),
15 asyncMiddleware(getVideosOverview)
18 // ---------------------------------------------------------------------------
20 export { overviewsRouter }
22 // ---------------------------------------------------------------------------
24 const buildSamples = memoizee(async function () {
25 const [ categories, channels, tags ] = await Promise.all([
26 VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
27 VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT),
28 TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
31 return { categories, channels, tags }
32 }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
34 // This endpoint could be quite long, but we cache it
35 async function getVideosOverview (req: express.Request, res: express.Response) {
36 const attributes = await buildSamples()
38 const [ categories, channels, tags ] = await Promise.all([
39 Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))),
40 Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))),
41 Promise.all(attributes.tags.map(t => getVideosByTag(t, res)))
44 const result: VideosOverview = {
51 for (const key of Object.keys(result)) {
52 result[key] = result[key].filter(v => v !== undefined)
55 return res.json(result)
58 async function getVideosByTag (tag: string, res: express.Response) {
59 const videos = await getVideos(res, { tagsOneOf: [ tag ] })
61 if (videos.length === 0) return undefined
69 async function getVideosByCategory (category: number, res: express.Response) {
70 const videos = await getVideos(res, { categoryOneOf: [ category ] })
72 if (videos.length === 0) return undefined
75 category: videos[0].category,
80 async function getVideosByChannel (channelId: number, res: express.Response) {
81 const videos = await getVideos(res, { videoChannelId: channelId })
83 if (videos.length === 0) return undefined
86 channel: videos[0].channel,
91 async function getVideos (
92 res: express.Response,
93 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
95 const query = Object.assign({
99 includeLocalVideos: true,
100 nsfw: buildNSFWFilter(res),
104 const { data } = await VideoModel.listForApi(query, false)
106 return data.map(d => d.toFormattedJSON())