diff options
Diffstat (limited to 'server/controllers/sitemap.ts')
-rw-r--r-- | server/controllers/sitemap.ts | 115 |
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 @@ | |||
1 | import express from 'express' | ||
2 | import { truncate } from 'lodash' | ||
3 | import { ErrorLevel, SitemapStream, streamToPromise } from 'sitemap' | ||
4 | import { logger } from '@server/helpers/logger' | ||
5 | import { getServerActor } from '@server/models/application/application' | ||
6 | import { buildNSFWFilter } from '../helpers/express-utils' | ||
7 | import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' | ||
8 | import { apiRateLimiter, asyncMiddleware } from '../middlewares' | ||
9 | import { cacheRoute } from '../middlewares/cache/cache' | ||
10 | import { AccountModel } from '../models/account/account' | ||
11 | import { VideoModel } from '../models/video/video' | ||
12 | import { VideoChannelModel } from '../models/video/video-channel' | ||
13 | |||
14 | const sitemapRouter = express.Router() | ||
15 | |||
16 | sitemapRouter.use('/sitemap.xml', | ||
17 | apiRateLimiter, | ||
18 | cacheRoute(ROUTE_CACHE_LIFETIME.SITEMAP), | ||
19 | asyncMiddleware(getSitemap) | ||
20 | ) | ||
21 | |||
22 | // --------------------------------------------------------------------------- | ||
23 | |||
24 | export { | ||
25 | sitemapRouter | ||
26 | } | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | async 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 | |||
61 | async 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 | |||
69 | async 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 | |||
77 | async 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 | |||
108 | function getSitemapBasicUrls () { | ||
109 | const paths = [ | ||
110 | '/about/instance', | ||
111 | '/videos/local' | ||
112 | ] | ||
113 | |||
114 | return paths.map(p => ({ url: WEBSERVER.URL + p })) | ||
115 | } | ||