1 import * as express from 'express'
2 import { truncate } from 'lodash'
3 import { SitemapStream, streamToPromise } from 'sitemap'
4 import { buildNSFWFilter } from '../helpers/express-utils'
5 import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
6 import { asyncMiddleware } from '../middlewares'
7 import { cacheRoute } from '../middlewares/cache/cache'
8 import { AccountModel } from '../models/account/account'
9 import { VideoModel } from '../models/video/video'
10 import { VideoChannelModel } from '../models/video/video-channel'
12 const botsRouter = express.Router()
14 // Special route that add OpenGraph and oEmbed tags
15 // Do not use a template engine for a so little thing
16 botsRouter.use('/sitemap.xml',
17 cacheRoute(ROUTE_CACHE_LIFETIME.SITEMAP),
18 asyncMiddleware(getSitemap)
21 // ---------------------------------------------------------------------------
27 // ---------------------------------------------------------------------------
29 async function getSitemap (req: express.Request, res: express.Response) {
30 let urls = getSitemapBasicUrls()
32 urls = urls.concat(await getSitemapLocalVideoUrls())
33 urls = urls.concat(await getSitemapVideoChannelUrls())
34 urls = urls.concat(await getSitemapAccountUrls())
36 const sitemapStream = new SitemapStream({ hostname: WEBSERVER.URL })
38 for (const urlObj of urls) {
39 sitemapStream.write(urlObj)
43 const xml = await streamToPromise(sitemapStream)
45 res.header('Content-Type', 'application/xml')
49 async function getSitemapVideoChannelUrls () {
50 const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
52 return rows.map(channel => ({
53 url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
57 async function getSitemapAccountUrls () {
58 const rows = await AccountModel.listLocalsForSitemap('createdAt')
60 return rows.map(channel => ({
61 url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
65 async function getSitemapLocalVideoUrls () {
66 const { data } = await VideoModel.listForApi({
70 includeLocalVideos: true,
71 nsfw: buildNSFWFilter(),
77 return data.map(v => ({
78 url: WEBSERVER.URL + v.getWatchStaticPath(),
82 // Sitemap description should be < 2000 characters
83 description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
84 player_loc: WEBSERVER.URL + v.getEmbedStaticPath(),
85 thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
91 function getSitemapBasicUrls () {
97 return paths.map(p => ({ url: WEBSERVER.URL + p }))