aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/sitemap.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/controllers/sitemap.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-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/sitemap.ts')
-rw-r--r--server/controllers/sitemap.ts115
1 files changed, 0 insertions, 115 deletions
diff --git a/server/controllers/sitemap.ts b/server/controllers/sitemap.ts
deleted file mode 100644
index 07f4c554e..000000000
--- a/server/controllers/sitemap.ts
+++ /dev/null
@@ -1,115 +0,0 @@
1import express from 'express'
2import { truncate } from 'lodash'
3import { ErrorLevel, SitemapStream, streamToPromise } from 'sitemap'
4import { logger } from '@server/helpers/logger'
5import { getServerActor } from '@server/models/application/application'
6import { buildNSFWFilter } from '../helpers/express-utils'
7import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
8import { apiRateLimiter, asyncMiddleware } from '../middlewares'
9import { cacheRoute } from '../middlewares/cache/cache'
10import { AccountModel } from '../models/account/account'
11import { VideoModel } from '../models/video/video'
12import { VideoChannelModel } from '../models/video/video-channel'
13
14const sitemapRouter = express.Router()
15
16sitemapRouter.use('/sitemap.xml',
17 apiRateLimiter,
18 cacheRoute(ROUTE_CACHE_LIFETIME.SITEMAP),
19 asyncMiddleware(getSitemap)
20)
21
22// ---------------------------------------------------------------------------
23
24export {
25 sitemapRouter
26}
27
28// ---------------------------------------------------------------------------
29
30async function getSitemap (req: express.Request, res: express.Response) {
31 let urls = getSitemapBasicUrls()
32
33 urls = urls.concat(await getSitemapLocalVideoUrls())
34 urls = urls.concat(await getSitemapVideoChannelUrls())
35 urls = urls.concat(await getSitemapAccountUrls())
36
37 const sitemapStream = new SitemapStream({
38 hostname: WEBSERVER.URL,
39 errorHandler: (err: Error, level: ErrorLevel) => {
40 if (level === 'warn') {
41 logger.warn('Warning in sitemap generation.', { err })
42 } else if (level === 'throw') {
43 logger.error('Error in sitemap generation.', { err })
44
45 throw err
46 }
47 }
48 })
49
50 for (const urlObj of urls) {
51 sitemapStream.write(urlObj)
52 }
53 sitemapStream.end()
54
55 const xml = await streamToPromise(sitemapStream)
56
57 res.header('Content-Type', 'application/xml')
58 res.send(xml)
59}
60
61async function getSitemapVideoChannelUrls () {
62 const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
63
64 return rows.map(channel => ({
65 url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
66 }))
67}
68
69async function getSitemapAccountUrls () {
70 const rows = await AccountModel.listLocalsForSitemap('createdAt')
71
72 return rows.map(channel => ({
73 url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
74 }))
75}
76
77async function getSitemapLocalVideoUrls () {
78 const serverActor = await getServerActor()
79
80 const { data } = await VideoModel.listForApi({
81 start: 0,
82 count: undefined,
83 sort: 'createdAt',
84 displayOnlyForFollower: {
85 actorId: serverActor.id,
86 orLocalVideos: true
87 },
88 isLocal: true,
89 nsfw: buildNSFWFilter(),
90 countVideos: false
91 })
92
93 return data.map(v => ({
94 url: WEBSERVER.URL + v.getWatchStaticPath(),
95 video: [
96 {
97 // Sitemap title should be < 100 characters
98 title: truncate(v.name, { length: 100, omission: '...' }),
99 // Sitemap description should be < 2000 characters
100 description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
101 player_loc: WEBSERVER.URL + v.getEmbedStaticPath(),
102 thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
103 }
104 ]
105 }))
106}
107
108function getSitemapBasicUrls () {
109 const paths = [
110 '/about/instance',
111 '/videos/local'
112 ]
113
114 return paths.map(p => ({ url: WEBSERVER.URL + p }))
115}