aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/sitemap.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/sitemap.ts')
-rw-r--r--server/controllers/sitemap.ts115
1 files changed, 115 insertions, 0 deletions
diff --git a/server/controllers/sitemap.ts b/server/controllers/sitemap.ts
new file mode 100644
index 000000000..07f4c554e
--- /dev/null
+++ b/server/controllers/sitemap.ts
@@ -0,0 +1,115 @@
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}