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