]>
Commit | Line | Data |
---|---|---|
2760b454 | 1 | import { getServerActor } from '@server/models/application/application' |
41fb13c3 | 2 | import express from 'express' |
15a7eafb | 3 | import { truncate } from 'lodash' |
d9699428 | 4 | import { SitemapStream, streamToPromise } from 'sitemap' |
15a7eafb C |
5 | import { buildNSFWFilter } from '../helpers/express-utils' |
6 | import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' | |
7 | import { asyncMiddleware } from '../middlewares' | |
8 | import { cacheRoute } from '../middlewares/cache/cache' | |
9 | import { AccountModel } from '../models/account/account' | |
2feebf3e C |
10 | import { VideoModel } from '../models/video/video' |
11 | import { VideoChannelModel } from '../models/video/video-channel' | |
2feebf3e C |
12 | |
13 | const botsRouter = express.Router() | |
14 | ||
15 | // Special route that add OpenGraph and oEmbed tags | |
16 | // Do not use a template engine for a so little thing | |
17 | botsRouter.use('/sitemap.xml', | |
20bafcb6 | 18 | cacheRoute(ROUTE_CACHE_LIFETIME.SITEMAP), |
2feebf3e C |
19 | asyncMiddleware(getSitemap) |
20 | ) | |
21 | ||
22 | // --------------------------------------------------------------------------- | |
23 | ||
24 | export { | |
25 | botsRouter | |
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 | ||
d9699428 | 37 | const sitemapStream = new SitemapStream({ hostname: WEBSERVER.URL }) |
2feebf3e | 38 | |
93708d4e C |
39 | for (const urlObj of urls) { |
40 | sitemapStream.write(urlObj) | |
d9699428 C |
41 | } |
42 | sitemapStream.end() | |
43 | ||
44 | const xml = await streamToPromise(sitemapStream) | |
2feebf3e | 45 | |
d5d9b6d7 C |
46 | res.header('Content-Type', 'application/xml') |
47 | res.send(xml) | |
2feebf3e C |
48 | } |
49 | ||
50 | async function getSitemapVideoChannelUrls () { | |
51 | const rows = await VideoChannelModel.listLocalsForSitemap('createdAt') | |
52 | ||
53 | return rows.map(channel => ({ | |
6dd9de95 | 54 | url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername |
2feebf3e C |
55 | })) |
56 | } | |
57 | ||
58 | async function getSitemapAccountUrls () { | |
59 | const rows = await AccountModel.listLocalsForSitemap('createdAt') | |
60 | ||
61 | return rows.map(channel => ({ | |
6dd9de95 | 62 | url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername |
2feebf3e C |
63 | })) |
64 | } | |
65 | ||
66 | async function getSitemapLocalVideoUrls () { | |
2760b454 C |
67 | const serverActor = await getServerActor() |
68 | ||
fe987656 | 69 | const { data } = await VideoModel.listForApi({ |
2feebf3e C |
70 | start: 0, |
71 | count: undefined, | |
72 | sort: 'createdAt', | |
2760b454 C |
73 | displayOnlyForFollower: { |
74 | actorId: serverActor.id, | |
75 | orLocalVideos: true | |
76 | }, | |
77 | isLocal: true, | |
2feebf3e | 78 | nsfw: buildNSFWFilter(), |
fe987656 | 79 | countVideos: false |
2feebf3e C |
80 | }) |
81 | ||
fe987656 | 82 | return data.map(v => ({ |
15a7eafb | 83 | url: WEBSERVER.URL + v.getWatchStaticPath(), |
2feebf3e C |
84 | video: [ |
85 | { | |
86 | title: v.name, | |
87 | // Sitemap description should be < 2000 characters | |
88 | description: truncate(v.description || v.name, { length: 2000, omission: '...' }), | |
15a7eafb | 89 | player_loc: WEBSERVER.URL + v.getEmbedStaticPath(), |
3acc5084 | 90 | thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath() |
2feebf3e C |
91 | } |
92 | ] | |
93 | })) | |
94 | } | |
95 | ||
96 | function getSitemapBasicUrls () { | |
97 | const paths = [ | |
98 | '/about/instance', | |
99 | '/videos/local' | |
100 | ] | |
101 | ||
6dd9de95 | 102 | return paths.map(p => ({ url: WEBSERVER.URL + p })) |
2feebf3e | 103 | } |