Fix error log when updating redundancy
[oweals/peertube.git] / server / controllers / bots.ts
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 { VideoModel } from '../models/video/video'
6 import { VideoChannelModel } from '../models/video/video-channel'
7 import { AccountModel } from '../models/account/account'
8 import { cacheRoute } from '../middlewares/cache'
9 import { buildNSFWFilter } from '../helpers/express-utils'
10 import { truncate } from 'lodash'
11
12 const botsRouter = express.Router()
13
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   asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SITEMAP)),
18   asyncMiddleware(getSitemap)
19 )
20
21 // ---------------------------------------------------------------------------
22
23 export {
24   botsRouter
25 }
26
27 // ---------------------------------------------------------------------------
28
29 async function getSitemap (req: express.Request, res: express.Response) {
30   let urls = getSitemapBasicUrls()
31
32   urls = urls.concat(await getSitemapLocalVideoUrls())
33   urls = urls.concat(await getSitemapVideoChannelUrls())
34   urls = urls.concat(await getSitemapAccountUrls())
35
36   const sitemap = sitemapModule.createSitemap({
37     hostname: WEBSERVER.URL,
38     urls: urls
39   })
40
41   const xml = sitemap.toXML()
42
43   res.header('Content-Type', 'application/xml')
44   res.send(xml)
45 }
46
47 async function getSitemapVideoChannelUrls () {
48   const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
49
50   return rows.map(channel => ({
51     url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
52   }))
53 }
54
55 async function getSitemapAccountUrls () {
56   const rows = await AccountModel.listLocalsForSitemap('createdAt')
57
58   return rows.map(channel => ({
59     url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
60   }))
61 }
62
63 async function getSitemapLocalVideoUrls () {
64   const resultList = await VideoModel.listForApi({
65     start: 0,
66     count: undefined,
67     sort: 'createdAt',
68     includeLocalVideos: true,
69     nsfw: buildNSFWFilter(),
70     filter: 'local',
71     withFiles: false
72   })
73
74   return resultList.data.map(v => ({
75     url: WEBSERVER.URL + '/videos/watch/' + v.uuid,
76     video: [
77       {
78         title: v.name,
79         // Sitemap description should be < 2000 characters
80         description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
81         player_loc: WEBSERVER.URL + '/videos/embed/' + v.uuid,
82         thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
83       }
84     ]
85   }))
86 }
87
88 function getSitemapBasicUrls () {
89   const paths = [
90     '/about/instance',
91     '/videos/local'
92   ]
93
94   return paths.map(p => ({ url: WEBSERVER.URL + p }))
95 }