Send video comment comments to followers/origin
[oweals/peertube.git] / server / controllers / services.ts
1 import * as express from 'express'
2 import { CONFIG, EMBED_SIZE, PREVIEWS_SIZE } from '../initializers'
3 import { asyncMiddleware, oembedValidator } from '../middlewares'
4 import { VideoModel } from '../models/video/video'
5
6 const servicesRouter = express.Router()
7
8 servicesRouter.use('/oembed',
9   asyncMiddleware(oembedValidator),
10   generateOEmbed
11 )
12
13 // ---------------------------------------------------------------------------
14
15 export {
16   servicesRouter
17 }
18
19 // ---------------------------------------------------------------------------
20
21 function generateOEmbed (req: express.Request, res: express.Response, next: express.NextFunction) {
22   const video = res.locals.video as VideoModel
23   const webserverUrl = CONFIG.WEBSERVER.URL
24   const maxHeight = parseInt(req.query.maxheight, 10)
25   const maxWidth = parseInt(req.query.maxwidth, 10)
26
27   const embedUrl = webserverUrl + video.getEmbedPath()
28   let thumbnailUrl = webserverUrl + video.getPreviewPath()
29   let embedWidth = EMBED_SIZE.width
30   let embedHeight = EMBED_SIZE.height
31
32   if (maxHeight < embedHeight) embedHeight = maxHeight
33   if (maxWidth < embedWidth) embedWidth = maxWidth
34
35   // Our thumbnail is too big for the consumer
36   if (
37     (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) ||
38     (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width)
39   ) {
40     thumbnailUrl = undefined
41   }
42
43   const html = `<iframe width="${embedWidth}" height="${embedHeight}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
44
45   const json: any = {
46     type: 'video',
47     version: '1.0',
48     html,
49     width: embedWidth,
50     height: embedHeight,
51     title: video.name,
52     author_name: video.VideoChannel.Account.name,
53     provider_name: 'PeerTube',
54     provider_url: webserverUrl
55   }
56
57   if (thumbnailUrl !== undefined) {
58     json.thumbnail_url = thumbnailUrl
59     json.thumbnail_width = PREVIEWS_SIZE.width
60     json.thumbnail_height = PREVIEWS_SIZE.height
61   }
62
63   return res.json(json)
64 }