]>
Commit | Line | Data |
---|---|---|
2d3741d6 C |
1 | import * as express from 'express' |
2 | import { buildNSFWFilter } from '../../helpers/express-utils' | |
3 | import { VideoModel } from '../../models/video/video' | |
764a9657 | 4 | import { asyncMiddleware, optionalAuthenticate, videosOverviewValidator } from '../../middlewares' |
2d3741d6 | 5 | import { TagModel } from '../../models/video/tag' |
764a9657 C |
6 | import { CategoryOverview, ChannelOverview, TagOverview, VideosOverview } from '../../../shared/models/overviews' |
7 | import { MEMOIZE_TTL, OVERVIEWS } from '../../initializers/constants' | |
7348b1fd | 8 | import * as memoizee from 'memoizee' |
764a9657 | 9 | import { logger } from '@server/helpers/logger' |
2d3741d6 C |
10 | |
11 | const overviewsRouter = express.Router() | |
12 | ||
13 | overviewsRouter.get('/videos', | |
764a9657 C |
14 | videosOverviewValidator, |
15 | optionalAuthenticate, | |
2d3741d6 C |
16 | asyncMiddleware(getVideosOverview) |
17 | ) | |
18 | ||
19 | // --------------------------------------------------------------------------- | |
20 | ||
21 | export { overviewsRouter } | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
4b5384f6 C |
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), | |
a1587156 | 28 | VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT), |
4b5384f6 C |
29 | TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT) |
30 | ]) | |
31 | ||
764a9657 C |
32 | const result = { categories, channels, tags } |
33 | ||
34 | logger.debug('Building samples for overview endpoint.', { result }) | |
35 | ||
36 | return result | |
4b5384f6 C |
37 | }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE }) |
38 | ||
2d3741d6 C |
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() | |
7348b1fd | 42 | |
764a9657 C |
43 | const page = req.query.page || 1 |
44 | const index = page - 1 | |
45 | ||
46 | const categories: CategoryOverview[] = [] | |
47 | const channels: ChannelOverview[] = [] | |
48 | const tags: TagOverview[] = [] | |
49 | ||
50 | await Promise.all([ | |
51 | getVideosByCategory(attributes.categories, index, res, categories), | |
52 | getVideosByChannel(attributes.channels, index, res, channels), | |
53 | getVideosByTag(attributes.tags, index, res, tags) | |
7348b1fd C |
54 | ]) |
55 | ||
2d3741d6 | 56 | const result: VideosOverview = { |
7348b1fd C |
57 | categories, |
58 | channels, | |
59 | tags | |
2d3741d6 C |
60 | } |
61 | ||
2d3741d6 C |
62 | return res.json(result) |
63 | } | |
64 | ||
764a9657 C |
65 | async function getVideosByTag (tagsSample: string[], index: number, res: express.Response, acc: TagOverview[]) { |
66 | if (tagsSample.length <= index) return | |
67 | ||
68 | const tag = tagsSample[index] | |
2d3741d6 C |
69 | const videos = await getVideos(res, { tagsOneOf: [ tag ] }) |
70 | ||
764a9657 | 71 | if (videos.length === 0) return |
2d3741d6 | 72 | |
764a9657 | 73 | acc.push({ |
2d3741d6 C |
74 | tag, |
75 | videos | |
764a9657 | 76 | }) |
2d3741d6 C |
77 | } |
78 | ||
764a9657 C |
79 | async function getVideosByCategory (categoriesSample: number[], index: number, res: express.Response, acc: CategoryOverview[]) { |
80 | if (categoriesSample.length <= index) return | |
81 | ||
82 | const category = categoriesSample[index] | |
2d3741d6 C |
83 | const videos = await getVideos(res, { categoryOneOf: [ category ] }) |
84 | ||
764a9657 | 85 | if (videos.length === 0) return |
2d3741d6 | 86 | |
764a9657 | 87 | acc.push({ |
2d3741d6 C |
88 | category: videos[0].category, |
89 | videos | |
764a9657 | 90 | }) |
2d3741d6 C |
91 | } |
92 | ||
764a9657 C |
93 | async function getVideosByChannel (channelsSample: number[], index: number, res: express.Response, acc: ChannelOverview[]) { |
94 | if (channelsSample.length <= index) return | |
95 | ||
96 | const channelId = channelsSample[index] | |
2d3741d6 C |
97 | const videos = await getVideos(res, { videoChannelId: channelId }) |
98 | ||
764a9657 | 99 | if (videos.length === 0) return |
2d3741d6 | 100 | |
764a9657 | 101 | acc.push({ |
2d3741d6 C |
102 | channel: videos[0].channel, |
103 | videos | |
764a9657 | 104 | }) |
2d3741d6 C |
105 | } |
106 | ||
107 | async function getVideos ( | |
108 | res: express.Response, | |
109 | where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] } | |
110 | ) { | |
7348b1fd | 111 | const query = Object.assign({ |
2d3741d6 | 112 | start: 0, |
0f4905e1 | 113 | count: 12, |
2d3741d6 C |
114 | sort: '-createdAt', |
115 | includeLocalVideos: true, | |
116 | nsfw: buildNSFWFilter(res), | |
52a350a1 | 117 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, |
fe987656 C |
118 | withFiles: false, |
119 | countVideos: false | |
7348b1fd C |
120 | }, where) |
121 | ||
fe987656 | 122 | const { data } = await VideoModel.listForApi(query) |
2d3741d6 C |
123 | |
124 | return data.map(d => d.toFormattedJSON()) | |
125 | } |