1 import * as express from 'express'
2 import { buildNSFWFilter } from '../../helpers/express-utils'
3 import { VideoModel } from '../../models/video/video'
4 import { asyncMiddleware, executeIfActivityPub } from '../../middlewares'
5 import { TagModel } from '../../models/video/tag'
6 import { VideosOverview } from '../../../shared/models/overviews'
7 import { OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers'
8 import { cacheRoute } from '../../middlewares/cache'
10 const overviewsRouter = express.Router()
12 overviewsRouter.get('/videos',
13 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS))),
14 asyncMiddleware(getVideosOverview)
17 // ---------------------------------------------------------------------------
19 export { overviewsRouter }
21 // ---------------------------------------------------------------------------
23 // This endpoint could be quite long, but we cache it
24 async function getVideosOverview (req: express.Request, res: express.Response) {
25 const attributes = await buildSamples()
26 const result: VideosOverview = {
27 categories: await Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))),
28 channels: await Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))),
29 tags: await Promise.all(attributes.tags.map(t => getVideosByTag(t, res)))
33 for (const key of Object.keys(result)) {
34 result[key] = result[key].filter(v => v !== undefined)
37 return res.json(result)
40 async function buildSamples () {
41 const [ categories, channels, tags ] = await Promise.all([
42 VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
43 VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT),
44 TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
47 return { categories, channels, tags }
50 async function getVideosByTag (tag: string, res: express.Response) {
51 const videos = await getVideos(res, { tagsOneOf: [ tag ] })
53 if (videos.length === 0) return undefined
61 async function getVideosByCategory (category: number, res: express.Response) {
62 const videos = await getVideos(res, { categoryOneOf: [ category ] })
64 if (videos.length === 0) return undefined
67 category: videos[0].category,
72 async function getVideosByChannel (channelId: number, res: express.Response) {
73 const videos = await getVideos(res, { videoChannelId: channelId })
75 if (videos.length === 0) return undefined
78 channel: videos[0].channel,
83 async function getVideos (
84 res: express.Response,
85 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
87 const { data } = await VideoModel.listForApi(Object.assign({
91 includeLocalVideos: true,
92 nsfw: buildNSFWFilter(res),
96 return data.map(d => d.toFormattedJSON())