]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/client.ts
Add oembed endpoint
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
index d913f81b808faaed80e76f0fce76a334168fd580..e3c9620588f3ae368b9a8965ad8b037edf325646 100644 (file)
@@ -6,10 +6,9 @@ import * as Promise from 'bluebird'
 import { database as db } from '../initializers/database'
 import {
   CONFIG,
-  REMOTE_SCHEME,
   STATIC_PATHS,
   STATIC_MAX_AGE,
-  OPENGRAPH_COMMENT
+  OPENGRAPH_AND_OEMBED_COMMENT
 } from '../initializers'
 import { root, readFileBufferPromise } from '../helpers'
 import { VideoInstance } from '../models'
@@ -20,7 +19,7 @@ const distPath = join(root(), 'client', 'dist')
 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
 const indexPath = join(distPath, 'index.html')
 
-// Special route that add OpenGraph tags
+// Special route that add OpenGraph and oEmbed tags
 // Do not use a template engine for a so little thing
 clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
 
@@ -44,22 +43,11 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
-  let basePreviewUrlHttp
+function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
+  const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
+  const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
 
-  if (video.isOwned()) {
-    basePreviewUrlHttp = CONFIG.WEBSERVER.URL
-  } else {
-    basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
-  }
-
-  // We fetch the remote preview (bigger than the thumbnail)
-  // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
-  // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
-  const previewUrl = basePreviewUrlHttp + STATIC_PATHS.PREVIEWS + video.getPreviewName()
-  const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
-
-  const metaTags = {
+  const openGraphMetaTags = {
     'og:type': 'video',
     'og:title': video.name,
     'og:image': previewUrl,
@@ -77,14 +65,26 @@ function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
     'twitter:image': previewUrl
   }
 
+  const oembedLinkTags = [
+    {
+      type: 'application/json+oembed',
+      href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
+      title: video.name
+    }
+  ]
+
   let tagsString = ''
-  Object.keys(metaTags).forEach(tagName => {
-    const tagValue = metaTags[tagName]
+  Object.keys(openGraphMetaTags).forEach(tagName => {
+    const tagValue = openGraphMetaTags[tagName]
 
-    tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
+    tagsString += `<meta property="${tagName}" content="${tagValue}" />`
   })
 
-  return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)
+  for (const oembedLinkTag of oembedLinkTags) {
+    tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
+  }
+
+  return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
 }
 
 function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
@@ -113,7 +113,7 @@ function generateWatchHtmlPage (req: express.Request, res: express.Response, nex
     // Let Angular application handle errors
     if (!video) return res.sendFile(indexPath)
 
-    const htmlStringPageWithTags = addOpenGraphTags(html, video)
+    const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
     res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
   })
   .catch(err => next(err))