]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/bots.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / controllers / bots.ts
CommitLineData
41fb13c3 1import express from 'express'
15a7eafb 2import { truncate } from 'lodash'
0c9668f7
C
3import { ErrorLevel, SitemapStream, streamToPromise } from 'sitemap'
4import { logger } from '@server/helpers/logger'
5import { getServerActor } from '@server/models/application/application'
15a7eafb
C
6import { buildNSFWFilter } from '../helpers/express-utils'
7import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
8import { asyncMiddleware } from '../middlewares'
9import { cacheRoute } from '../middlewares/cache/cache'
10import { AccountModel } from '../models/account/account'
2feebf3e
C
11import { VideoModel } from '../models/video/video'
12import { VideoChannelModel } from '../models/video/video-channel'
2feebf3e
C
13
14const botsRouter = express.Router()
15
16// Special route that add OpenGraph and oEmbed tags
17// Do not use a template engine for a so little thing
18botsRouter.use('/sitemap.xml',
20bafcb6 19 cacheRoute(ROUTE_CACHE_LIFETIME.SITEMAP),
2feebf3e
C
20 asyncMiddleware(getSitemap)
21)
22
23// ---------------------------------------------------------------------------
24
25export {
26 botsRouter
27}
28
29// ---------------------------------------------------------------------------
30
31async function getSitemap (req: express.Request, res: express.Response) {
32 let urls = getSitemapBasicUrls()
33
34 urls = urls.concat(await getSitemapLocalVideoUrls())
35 urls = urls.concat(await getSitemapVideoChannelUrls())
36 urls = urls.concat(await getSitemapAccountUrls())
37
2b32c5b3
C
38 const sitemapStream = new SitemapStream({
39 hostname: WEBSERVER.URL,
40 errorHandler: (err: Error, level: ErrorLevel) => {
41 if (level === 'warn') {
42 logger.warn('Warning in sitemap generation.', { err })
43 } else if (level === 'throw') {
44 logger.error('Error in sitemap generation.', { err })
45
46 throw err
47 }
48 }
49 })
2feebf3e 50
93708d4e
C
51 for (const urlObj of urls) {
52 sitemapStream.write(urlObj)
d9699428
C
53 }
54 sitemapStream.end()
55
56 const xml = await streamToPromise(sitemapStream)
2feebf3e 57
d5d9b6d7
C
58 res.header('Content-Type', 'application/xml')
59 res.send(xml)
2feebf3e
C
60}
61
62async function getSitemapVideoChannelUrls () {
63 const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
64
65 return rows.map(channel => ({
6dd9de95 66 url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
2feebf3e
C
67 }))
68}
69
70async function getSitemapAccountUrls () {
71 const rows = await AccountModel.listLocalsForSitemap('createdAt')
72
73 return rows.map(channel => ({
6dd9de95 74 url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
2feebf3e
C
75 }))
76}
77
78async function getSitemapLocalVideoUrls () {
2760b454
C
79 const serverActor = await getServerActor()
80
fe987656 81 const { data } = await VideoModel.listForApi({
2feebf3e
C
82 start: 0,
83 count: undefined,
84 sort: 'createdAt',
2760b454
C
85 displayOnlyForFollower: {
86 actorId: serverActor.id,
87 orLocalVideos: true
88 },
89 isLocal: true,
2feebf3e 90 nsfw: buildNSFWFilter(),
fe987656 91 countVideos: false
2feebf3e
C
92 })
93
fe987656 94 return data.map(v => ({
15a7eafb 95 url: WEBSERVER.URL + v.getWatchStaticPath(),
2feebf3e
C
96 video: [
97 {
2b32c5b3
C
98 // Sitemap title should be < 100 characters
99 title: truncate(v.name, { length: 100, omission: '...' }),
2feebf3e
C
100 // Sitemap description should be < 2000 characters
101 description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
15a7eafb 102 player_loc: WEBSERVER.URL + v.getEmbedStaticPath(),
3acc5084 103 thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
2feebf3e
C
104 }
105 ]
106 }))
107}
108
109function getSitemapBasicUrls () {
110 const paths = [
111 '/about/instance',
112 '/videos/local'
113 ]
114
6dd9de95 115 return paths.map(p => ({ url: WEBSERVER.URL + p }))
2feebf3e 116}