]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/overviews.ts
Don't expose constants directly in initializers/
[github/Chocobozzz/PeerTube.git] / server / controllers / api / overviews.ts
CommitLineData
2d3741d6
C
1import * as express from 'express'
2import { buildNSFWFilter } from '../../helpers/express-utils'
3import { VideoModel } from '../../models/video/video'
558187a7 4import { asyncMiddleware } from '../../middlewares'
2d3741d6
C
5import { TagModel } from '../../models/video/tag'
6import { VideosOverview } from '../../../shared/models/overviews'
74dc3bca 7import { MEMOIZE_TTL, OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers/constants'
2d3741d6 8import { cacheRoute } from '../../middlewares/cache'
7348b1fd 9import * as memoizee from 'memoizee'
2d3741d6
C
10
11const overviewsRouter = express.Router()
12
13overviewsRouter.get('/videos',
558187a7 14 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS)),
2d3741d6
C
15 asyncMiddleware(getVideosOverview)
16)
17
18// ---------------------------------------------------------------------------
19
20export { overviewsRouter }
21
22// ---------------------------------------------------------------------------
23
4b5384f6
C
24const 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)
29 ])
30
31 return { categories, channels, tags }
32}, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
33
2d3741d6
C
34// This endpoint could be quite long, but we cache it
35async function getVideosOverview (req: express.Request, res: express.Response) {
36 const attributes = await buildSamples()
7348b1fd
C
37
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)))
42 ])
43
2d3741d6 44 const result: VideosOverview = {
7348b1fd
C
45 categories,
46 channels,
47 tags
2d3741d6
C
48 }
49
50 // Cleanup our object
51 for (const key of Object.keys(result)) {
52 result[key] = result[key].filter(v => v !== undefined)
53 }
54
55 return res.json(result)
56}
57
2d3741d6
C
58async function getVideosByTag (tag: string, res: express.Response) {
59 const videos = await getVideos(res, { tagsOneOf: [ tag ] })
60
61 if (videos.length === 0) return undefined
62
63 return {
64 tag,
65 videos
66 }
67}
68
69async function getVideosByCategory (category: number, res: express.Response) {
70 const videos = await getVideos(res, { categoryOneOf: [ category ] })
71
72 if (videos.length === 0) return undefined
73
74 return {
75 category: videos[0].category,
76 videos
77 }
78}
79
80async function getVideosByChannel (channelId: number, res: express.Response) {
81 const videos = await getVideos(res, { videoChannelId: channelId })
82
83 if (videos.length === 0) return undefined
84
85 return {
86 channel: videos[0].channel,
87 videos
88 }
89}
90
91async function getVideos (
92 res: express.Response,
93 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
94) {
7348b1fd 95 const query = Object.assign({
2d3741d6 96 start: 0,
0f4905e1 97 count: 12,
2d3741d6
C
98 sort: '-createdAt',
99 includeLocalVideos: true,
100 nsfw: buildNSFWFilter(res),
101 withFiles: false
7348b1fd
C
102 }, where)
103
104 const { data } = await VideoModel.listForApi(query, false)
2d3741d6
C
105
106 return data.map(d => d.toFormattedJSON())
107}