1 import { getServerActor } from '@server/models/application/application'
2 import express from 'express'
3 import { truncate } from 'lodash'
4 import { SitemapStream, streamToPromise } from 'sitemap'
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'
10 import { VideoModel } from '../models/video/video'
11 import { VideoChannelModel } from '../models/video/video-channel'
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 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 sitemapStream = new SitemapStream({ hostname: WEBSERVER.URL })
39 for (const urlObj of urls) {
40 sitemapStream.write(urlObj)
44 const xml = await streamToPromise(sitemapStream)
46 res.header('Content-Type', 'application/xml')
50 async function getSitemapVideoChannelUrls () {
51 const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
53 return rows.map(channel => ({
54 url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
58 async function getSitemapAccountUrls () {
59 const rows = await AccountModel.listLocalsForSitemap('createdAt')
61 return rows.map(channel => ({
62 url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
66 async function getSitemapLocalVideoUrls () {
67 const serverActor = await getServerActor()
69 const { data } = await VideoModel.listForApi({
73 displayOnlyForFollower: {
74 actorId: serverActor.id,
78 nsfw: buildNSFWFilter(),
82 return data.map(v => ({
83 url: WEBSERVER.URL + v.getWatchStaticPath(),
87 // Sitemap description should be < 2000 characters
88 description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
89 player_loc: WEBSERVER.URL + v.getEmbedStaticPath(),
90 thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
96 function getSitemapBasicUrls () {
102 return paths.map(p => ({ url: WEBSERVER.URL + p }))