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