diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/overviews.ts | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/server/controllers/api/overviews.ts b/server/controllers/api/overviews.ts index da941c0ac..cc3cc54a7 100644 --- a/server/controllers/api/overviews.ts +++ b/server/controllers/api/overviews.ts | |||
@@ -4,8 +4,9 @@ import { VideoModel } from '../../models/video/video' | |||
4 | import { asyncMiddleware } from '../../middlewares' | 4 | import { asyncMiddleware } from '../../middlewares' |
5 | import { TagModel } from '../../models/video/tag' | 5 | import { TagModel } from '../../models/video/tag' |
6 | import { VideosOverview } from '../../../shared/models/overviews' | 6 | import { VideosOverview } from '../../../shared/models/overviews' |
7 | import { OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers' | 7 | import { MEMOIZE_TTL, OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers' |
8 | import { cacheRoute } from '../../middlewares/cache' | 8 | import { cacheRoute } from '../../middlewares/cache' |
9 | import * as memoizee from 'memoizee' | ||
9 | 10 | ||
10 | const overviewsRouter = express.Router() | 11 | const overviewsRouter = express.Router() |
11 | 12 | ||
@@ -23,10 +24,17 @@ export { overviewsRouter } | |||
23 | // This endpoint could be quite long, but we cache it | 24 | // This endpoint could be quite long, but we cache it |
24 | async function getVideosOverview (req: express.Request, res: express.Response) { | 25 | async function getVideosOverview (req: express.Request, res: express.Response) { |
25 | const attributes = await buildSamples() | 26 | const attributes = await buildSamples() |
27 | |||
28 | const [ categories, channels, tags ] = await Promise.all([ | ||
29 | Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))), | ||
30 | Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))), | ||
31 | Promise.all(attributes.tags.map(t => getVideosByTag(t, res))) | ||
32 | ]) | ||
33 | |||
26 | const result: VideosOverview = { | 34 | const result: VideosOverview = { |
27 | categories: await Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))), | 35 | categories, |
28 | channels: await Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))), | 36 | channels, |
29 | tags: await Promise.all(attributes.tags.map(t => getVideosByTag(t, res))) | 37 | tags |
30 | } | 38 | } |
31 | 39 | ||
32 | // Cleanup our object | 40 | // Cleanup our object |
@@ -37,7 +45,7 @@ async function getVideosOverview (req: express.Request, res: express.Response) { | |||
37 | return res.json(result) | 45 | return res.json(result) |
38 | } | 46 | } |
39 | 47 | ||
40 | async function buildSamples () { | 48 | const buildSamples = memoizee(async function () { |
41 | const [ categories, channels, tags ] = await Promise.all([ | 49 | const [ categories, channels, tags ] = await Promise.all([ |
42 | VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT), | 50 | VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT), |
43 | VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT), | 51 | VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT), |
@@ -45,7 +53,7 @@ async function buildSamples () { | |||
45 | ]) | 53 | ]) |
46 | 54 | ||
47 | return { categories, channels, tags } | 55 | return { categories, channels, tags } |
48 | } | 56 | }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE }) |
49 | 57 | ||
50 | async function getVideosByTag (tag: string, res: express.Response) { | 58 | async function getVideosByTag (tag: string, res: express.Response) { |
51 | const videos = await getVideos(res, { tagsOneOf: [ tag ] }) | 59 | const videos = await getVideos(res, { tagsOneOf: [ tag ] }) |
@@ -84,14 +92,16 @@ async function getVideos ( | |||
84 | res: express.Response, | 92 | res: express.Response, |
85 | where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] } | 93 | where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] } |
86 | ) { | 94 | ) { |
87 | const { data } = await VideoModel.listForApi(Object.assign({ | 95 | const query = Object.assign({ |
88 | start: 0, | 96 | start: 0, |
89 | count: 10, | 97 | count: 10, |
90 | sort: '-createdAt', | 98 | sort: '-createdAt', |
91 | includeLocalVideos: true, | 99 | includeLocalVideos: true, |
92 | nsfw: buildNSFWFilter(res), | 100 | nsfw: buildNSFWFilter(res), |
93 | withFiles: false | 101 | withFiles: false |
94 | }, where)) | 102 | }, where) |
103 | |||
104 | const { data } = await VideoModel.listForApi(query, false) | ||
95 | 105 | ||
96 | return data.map(d => d.toFormattedJSON()) | 106 | return data.map(d => d.toFormattedJSON()) |
97 | } | 107 | } |