diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/controllers/api/overviews.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
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 | } | ||