]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/bots.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / controllers / bots.ts
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'
12
13 const botsRouter = express.Router()
14
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)
20 )
21
22 // ---------------------------------------------------------------------------
23
24 export {
25 botsRouter
26 }
27
28 // ---------------------------------------------------------------------------
29
30 async function getSitemap (req: express.Request, res: express.Response) {
31 let urls = getSitemapBasicUrls()
32
33 urls = urls.concat(await getSitemapLocalVideoUrls())
34 urls = urls.concat(await getSitemapVideoChannelUrls())
35 urls = urls.concat(await getSitemapAccountUrls())
36
37 const sitemapStream = new SitemapStream({ hostname: WEBSERVER.URL })
38
39 for (const urlObj of urls) {
40 sitemapStream.write(urlObj)
41 }
42 sitemapStream.end()
43
44 const xml = await streamToPromise(sitemapStream)
45
46 res.header('Content-Type', 'application/xml')
47 res.send(xml)
48 }
49
50 async function getSitemapVideoChannelUrls () {
51 const rows = await VideoChannelModel.listLocalsForSitemap('createdAt')
52
53 return rows.map(channel => ({
54 url: WEBSERVER.URL + '/video-channels/' + channel.Actor.preferredUsername
55 }))
56 }
57
58 async function getSitemapAccountUrls () {
59 const rows = await AccountModel.listLocalsForSitemap('createdAt')
60
61 return rows.map(channel => ({
62 url: WEBSERVER.URL + '/accounts/' + channel.Actor.preferredUsername
63 }))
64 }
65
66 async function getSitemapLocalVideoUrls () {
67 const serverActor = await getServerActor()
68
69 const { data } = await VideoModel.listForApi({
70 start: 0,
71 count: undefined,
72 sort: 'createdAt',
73 displayOnlyForFollower: {
74 actorId: serverActor.id,
75 orLocalVideos: true
76 },
77 isLocal: true,
78 nsfw: buildNSFWFilter(),
79 countVideos: false
80 })
81
82 return data.map(v => ({
83 url: WEBSERVER.URL + v.getWatchStaticPath(),
84 video: [
85 {
86 title: v.name,
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()
91 }
92 ]
93 }))
94 }
95
96 function getSitemapBasicUrls () {
97 const paths = [
98 '/about/instance',
99 '/videos/local'
100 ]
101
102 return paths.map(p => ({ url: WEBSERVER.URL + p }))
103 }