diff options
Diffstat (limited to 'server/controllers/api/overviews.ts')
-rw-r--r-- | server/controllers/api/overviews.ts | 139 |
1 files changed, 0 insertions, 139 deletions
diff --git a/server/controllers/api/overviews.ts b/server/controllers/api/overviews.ts deleted file mode 100644 index fc616281e..000000000 --- a/server/controllers/api/overviews.ts +++ /dev/null | |||
@@ -1,139 +0,0 @@ | |||
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 { getServerActor } from '@server/models/application/application' | ||
6 | import { VideoModel } from '@server/models/video/video' | ||
7 | import { CategoryOverview, ChannelOverview, TagOverview, VideosOverview } from '../../../shared/models/overviews' | ||
8 | import { buildNSFWFilter } from '../../helpers/express-utils' | ||
9 | import { MEMOIZE_TTL, OVERVIEWS } from '../../initializers/constants' | ||
10 | import { apiRateLimiter, asyncMiddleware, optionalAuthenticate, videosOverviewValidator } from '../../middlewares' | ||
11 | import { TagModel } from '../../models/video/tag' | ||
12 | |||
13 | const overviewsRouter = express.Router() | ||
14 | |||
15 | overviewsRouter.use(apiRateLimiter) | ||
16 | |||
17 | overviewsRouter.get('/videos', | ||
18 | videosOverviewValidator, | ||
19 | optionalAuthenticate, | ||
20 | asyncMiddleware(getVideosOverview) | ||
21 | ) | ||
22 | |||
23 | // --------------------------------------------------------------------------- | ||
24 | |||
25 | export { overviewsRouter } | ||
26 | |||
27 | // --------------------------------------------------------------------------- | ||
28 | |||
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), | ||
32 | VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT), | ||
33 | TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT) | ||
34 | ]) | ||
35 | |||
36 | const result = { categories, channels, tags } | ||
37 | |||
38 | logger.debug('Building samples for overview endpoint.', { result }) | ||
39 | |||
40 | return result | ||
41 | }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE }) | ||
42 | |||
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() | ||
46 | |||
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) | ||
58 | ]) | ||
59 | |||
60 | const result: VideosOverview = { | ||
61 | categories, | ||
62 | channels, | ||
63 | tags | ||
64 | } | ||
65 | |||
66 | return res.json(result) | ||
67 | } | ||
68 | |||
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] | ||
73 | const videos = await getVideos(res, { tagsOneOf: [ tag ] }) | ||
74 | |||
75 | if (videos.length === 0) return | ||
76 | |||
77 | acc.push({ | ||
78 | tag, | ||
79 | videos | ||
80 | }) | ||
81 | } | ||
82 | |||
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] | ||
87 | const videos = await getVideos(res, { categoryOneOf: [ category ] }) | ||
88 | |||
89 | if (videos.length === 0) return | ||
90 | |||
91 | acc.push({ | ||
92 | category: videos[0].category, | ||
93 | videos | ||
94 | }) | ||
95 | } | ||
96 | |||
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] | ||
101 | const videos = await getVideos(res, { videoChannelId: channelId }) | ||
102 | |||
103 | if (videos.length === 0) return | ||
104 | |||
105 | acc.push({ | ||
106 | channel: videos[0].channel, | ||
107 | videos | ||
108 | }) | ||
109 | } | ||
110 | |||
111 | async function getVideos ( | ||
112 | res: express.Response, | ||
113 | where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] } | ||
114 | ) { | ||
115 | const serverActor = await getServerActor() | ||
116 | |||
117 | const query = await Hooks.wrapObject({ | ||
118 | start: 0, | ||
119 | count: 12, | ||
120 | sort: '-createdAt', | ||
121 | displayOnlyForFollower: { | ||
122 | actorId: serverActor.id, | ||
123 | orLocalVideos: true | ||
124 | }, | ||
125 | nsfw: buildNSFWFilter(res), | ||
126 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, | ||
127 | countVideos: false, | ||
128 | |||
129 | ...where | ||
130 | }, 'filter:api.overviews.videos.list.params') | ||
131 | |||
132 | const { data } = await Hooks.wrapPromiseFun( | ||
133 | VideoModel.listForApi, | ||
134 | query, | ||
135 | 'filter:api.overviews.videos.list.result' | ||
136 | ) | ||
137 | |||
138 | return data.map(d => d.toFormattedJSON()) | ||
139 | } | ||