1 import * as express from 'express'
2 import { asyncMiddleware } from '../middlewares'
3 import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
4 import * as sitemapModule from 'sitemap'
5 import { logger } from '../helpers/logger'
6 import { VideoModel } from '../models/video/video'
7 import { VideoChannelModel } from '../models/video/video-channel'
8 import { AccountModel } from '../models/account/account'
9 import { cacheRoute } from '../middlewares/cache'
10 import { buildNSFWFilter } from '../helpers/express-utils'
11 import { truncate } from 'lodash'
13 const botsRouter = express.Router()
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',
18 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SITEMAP)),
19 asyncMiddleware(getSitemap)
22 // ---------------------------------------------------------------------------
28 // ---------------------------------------------------------------------------
30 async function getSitemap (req: express.Request, res: express.Response) {
31 let urls = getSitemapBasicUrls()
33 urls = urls.concat(await getSitemapLocalVideoUrls())
34 urls = urls.concat(await getSitemapVideoChannelUrls())
35 urls = urls.concat(await getSitemapAccountUrls())
37 const sitemap = sitemapModule.createSitemap({
38 hostname: WEBSERVER.URL,
42 sitemap.toXML((err, xml) => {
44 logger.error('Cannot generate sitemap.', { err })
45 return res.sendStatus(500)
48 res.header('Content-Type', 'application/xml')
53 async function getSitemapVideoChannelUrls () {
54 const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
56 return rows.map(channel => ({
57 url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
61 async function getSitemapAccountUrls () {
62 const rows = await AccountModel.listLocalsForSitemap('createdAt')
64 return rows.map(channel => ({
65 url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
69 async function getSitemapLocalVideoUrls () {
70 const resultList = await VideoModel.listForApi({
74 includeLocalVideos: true,
75 nsfw: buildNSFWFilter(),
80 return resultList.data.map(v => ({
81 url: WEBSERVER.URL + '/videos/watch/' + v.uuid,
85 // Sitemap description should be < 2000 characters
86 description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
87 player_loc: WEBSERVER.URL + '/videos/embed/' + v.uuid,
88 thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
94 function getSitemapBasicUrls () {
100 return paths.map(p => ({ url: WEBSERVER.URL + p }))