]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/bots.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / bots.ts
CommitLineData
2feebf3e
C
1import * as express from 'express'
2import { asyncMiddleware } from '../middlewares'
74dc3bca 3import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
2feebf3e 4import * as sitemapModule from 'sitemap'
2feebf3e
C
5import { VideoModel } from '../models/video/video'
6import { VideoChannelModel } from '../models/video/video-channel'
7import { AccountModel } from '../models/account/account'
8import { cacheRoute } from '../middlewares/cache'
9import { buildNSFWFilter } from '../helpers/express-utils'
10import { truncate } from 'lodash'
11
12const 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
16botsRouter.use('/sitemap.xml',
f2f0eda5 17 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.SITEMAP)),
2feebf3e
C
18 asyncMiddleware(getSitemap)
19)
20
21// ---------------------------------------------------------------------------
22
23export {
24 botsRouter
25}
26
27// ---------------------------------------------------------------------------
28
29async 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({
6dd9de95 37 hostname: WEBSERVER.URL,
2feebf3e
C
38 urls: urls
39 })
40
d5d9b6d7 41 const xml = sitemap.toXML()
2feebf3e 42
d5d9b6d7
C
43 res.header('Content-Type', 'application/xml')
44 res.send(xml)
2feebf3e
C
45}
46
47async function getSitemapVideoChannelUrls () {
48 const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
49
50 return rows.map(channel => ({
6dd9de95 51 url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
2feebf3e
C
52 }))
53}
54
55async function getSitemapAccountUrls () {
56 const rows = await AccountModel.listLocalsForSitemap('createdAt')
57
58 return rows.map(channel => ({
6dd9de95 59 url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
2feebf3e
C
60 }))
61}
62
63async function getSitemapLocalVideoUrls () {
fe987656 64 const { data } = await VideoModel.listForApi({
2feebf3e
C
65 start: 0,
66 count: undefined,
67 sort: 'createdAt',
68 includeLocalVideos: true,
69 nsfw: buildNSFWFilter(),
70 filter: 'local',
fe987656
C
71 withFiles: false,
72 countVideos: false
2feebf3e
C
73 })
74
fe987656 75 return data.map(v => ({
6dd9de95 76 url: WEBSERVER.URL + '/videos/watch/' + v.uuid,
2feebf3e
C
77 video: [
78 {
79 title: v.name,
80 // Sitemap description should be < 2000 characters
81 description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
6dd9de95 82 player_loc: WEBSERVER.URL + '/videos/embed/' + v.uuid,
3acc5084 83 thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
2feebf3e
C
84 }
85 ]
86 }))
87}
88
89function getSitemapBasicUrls () {
90 const paths = [
91 '/about/instance',
92 '/videos/local'
93 ]
94
6dd9de95 95 return paths.map(p => ({ url: WEBSERVER.URL + p }))
2feebf3e 96}