bots.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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'
  12. const botsRouter = express.Router()
  13. // Special route that add OpenGraph and oEmbed tags
  14. // Do not use a template engine for a so little thing
  15. botsRouter.use('/sitemap.xml',
  16. asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SITEMAP)),
  17. asyncMiddleware(getSitemap)
  18. )
  19. // ---------------------------------------------------------------------------
  20. export {
  21. botsRouter
  22. }
  23. // ---------------------------------------------------------------------------
  24. async function getSitemap (req: express.Request, res: express.Response) {
  25. let urls = getSitemapBasicUrls()
  26. urls = urls.concat(await getSitemapLocalVideoUrls())
  27. urls = urls.concat(await getSitemapVideoChannelUrls())
  28. urls = urls.concat(await getSitemapAccountUrls())
  29. const sitemap = sitemapModule.createSitemap({
  30. hostname: WEBSERVER.URL,
  31. urls: urls
  32. })
  33. sitemap.toXML((err, xml) => {
  34. if (err) {
  35. logger.error('Cannot generate sitemap.', { err })
  36. return res.sendStatus(500)
  37. }
  38. res.header('Content-Type', 'application/xml')
  39. res.send(xml)
  40. })
  41. }
  42. async function getSitemapVideoChannelUrls () {
  43. const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
  44. return rows.map(channel => ({
  45. url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
  46. }))
  47. }
  48. async function getSitemapAccountUrls () {
  49. const rows = await AccountModel.listLocalsForSitemap('createdAt')
  50. return rows.map(channel => ({
  51. url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
  52. }))
  53. }
  54. async function getSitemapLocalVideoUrls () {
  55. const resultList = await VideoModel.listForApi({
  56. start: 0,
  57. count: undefined,
  58. sort: 'createdAt',
  59. includeLocalVideos: true,
  60. nsfw: buildNSFWFilter(),
  61. filter: 'local',
  62. withFiles: false
  63. })
  64. return resultList.data.map(v => ({
  65. url: WEBSERVER.URL + '/videos/watch/' + v.uuid,
  66. video: [
  67. {
  68. title: v.name,
  69. // Sitemap description should be < 2000 characters
  70. description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
  71. player_loc: WEBSERVER.URL + '/videos/embed/' + v.uuid,
  72. thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
  73. }
  74. ]
  75. }))
  76. }
  77. function getSitemapBasicUrls () {
  78. const paths = [
  79. '/about/instance',
  80. '/videos/local'
  81. ]
  82. return paths.map(p => ({ url: WEBSERVER.URL + p }))
  83. }