Send video comment comments to followers/origin
[oweals/peertube.git] / server / controllers / client.ts
1 import * as express from 'express'
2 import { join } from 'path'
3 import * as validator from 'validator'
4 import * as Bluebird from 'bluebird'
5 import {
6   CONFIG,
7   STATIC_PATHS,
8   STATIC_MAX_AGE,
9   OPENGRAPH_AND_OEMBED_COMMENT,
10   EMBED_SIZE
11 } from '../initializers'
12 import { root, readFileBufferPromise, escapeHTML } from '../helpers'
13 import { asyncMiddleware } from '../middlewares'
14 import { VideoModel } from '../models/video/video'
15
16 const clientsRouter = express.Router()
17
18 const distPath = join(root(), 'client', 'dist')
19 const assetsImagesPath = join(root(), 'client', 'dist', 'client', 'assets', 'images')
20 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
21 const indexPath = join(distPath, 'index.html')
22
23 // Special route that add OpenGraph and oEmbed tags
24 // Do not use a template engine for a so little thing
25 clientsRouter.use('/videos/watch/:id',
26   asyncMiddleware(generateWatchHtmlPage)
27 )
28
29 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
30   res.sendFile(embedPath)
31 })
32
33 // Static HTML/CSS/JS client files
34 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
35 clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
36
37 // 404 for static files not found
38 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
39   res.sendStatus(404)
40 })
41
42 // ---------------------------------------------------------------------------
43
44 export {
45   clientsRouter
46 }
47
48 // ---------------------------------------------------------------------------
49
50 function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
51   const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
52   const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
53
54   const videoNameEscaped = escapeHTML(video.name)
55   const videoDescriptionEscaped = escapeHTML(video.description)
56   const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
57
58   const openGraphMetaTags = {
59     'og:type': 'video',
60     'og:title': videoNameEscaped,
61     'og:image': previewUrl,
62     'og:url': videoUrl,
63     'og:description': videoDescriptionEscaped,
64
65     'og:video:url': embedUrl,
66     'og:video:secure_url': embedUrl,
67     'og:video:type': 'text/html',
68     'og:video:width': EMBED_SIZE.width,
69     'og:video:height': EMBED_SIZE.height,
70
71     'name': videoNameEscaped,
72     'description': videoDescriptionEscaped,
73     'image': previewUrl,
74
75     'twitter:card': 'summary_large_image',
76     'twitter:site': '@Chocobozzz',
77     'twitter:title': videoNameEscaped,
78     'twitter:description': videoDescriptionEscaped,
79     'twitter:image': previewUrl,
80     'twitter:player': embedUrl,
81     'twitter:player:width': EMBED_SIZE.width,
82     'twitter:player:height': EMBED_SIZE.height
83   }
84
85   const oembedLinkTags = [
86     {
87       type: 'application/json+oembed',
88       href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
89       title: videoNameEscaped
90     }
91   ]
92
93   let tagsString = ''
94   Object.keys(openGraphMetaTags).forEach(tagName => {
95     const tagValue = openGraphMetaTags[tagName]
96
97     tagsString += `<meta property="${tagName}" content="${tagValue}" />`
98   })
99
100   for (const oembedLinkTag of oembedLinkTags) {
101     tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
102   }
103
104   return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
105 }
106
107 async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
108   const videoId = '' + req.params.id
109   let videoPromise: Bluebird<VideoModel>
110
111   // Let Angular application handle errors
112   if (validator.isUUID(videoId, 4)) {
113     videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
114   } else if (validator.isInt(videoId)) {
115     videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
116   } else {
117     return res.sendFile(indexPath)
118   }
119
120   let [ file, video ] = await Promise.all([
121     readFileBufferPromise(indexPath),
122     videoPromise
123   ])
124
125   const html = file.toString()
126
127   // Let Angular application handle errors
128   if (!video) return res.sendFile(indexPath)
129
130   const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
131   res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
132 }