Support fowarding query params to oembed
For now only `start` for videos and `playlistPosition` for playlists are supported
This commit is contained in:
parent
0edade2226
commit
546265e9ae
7 changed files with 118 additions and 18 deletions
|
@ -19,7 +19,7 @@ function removeQueryParams (url: string) {
|
||||||
return objUrl.toString()
|
return objUrl.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryParamsToObject (entries: any) {
|
function queryParamsToObject (entries: URLSearchParams) {
|
||||||
const result: { [ id: string ]: string | number | boolean } = {}
|
const result: { [ id: string ]: string | number | boolean } = {}
|
||||||
|
|
||||||
for (const [ key, value ] of entries) {
|
for (const [ key, value ] of entries) {
|
||||||
|
|
|
@ -114,6 +114,18 @@ describe('Test services', function () {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Should have a oEmbed response with query params', async function () {
|
||||||
|
const query = '?start=1m2s&stop=2'
|
||||||
|
const oembedUrl = `http://${server.host}/w/${video.uuid}${query}&unknown=3`
|
||||||
|
const res = await server.services.getOEmbed({ oembedUrl })
|
||||||
|
|
||||||
|
const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" ' +
|
||||||
|
`title="${video.name}" src="http://${server.host}/videos/embed/${video.uuid}${query}" ` +
|
||||||
|
'frameborder="0" allowfullscreen></iframe>'
|
||||||
|
|
||||||
|
expect(res.body.html).to.equal(expectedHtml)
|
||||||
|
})
|
||||||
|
|
||||||
it('Should have a valid oEmbed response with small max height query', async function () {
|
it('Should have a valid oEmbed response with small max height query', async function () {
|
||||||
for (const basePath of [ '/videos/watch/', '/w/' ]) {
|
for (const basePath of [ '/videos/watch/', '/w/' ]) {
|
||||||
const oembedUrl = 'http://' + server.host + basePath + video.uuid
|
const oembedUrl = 'http://' + server.host + basePath + video.uuid
|
||||||
|
|
|
@ -58,6 +58,44 @@ describe('Test oEmbed HTML tags', function () {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Should forward query params to video oEmbed discrovery URL', async function () {
|
||||||
|
const res = await makeGetRequest({
|
||||||
|
url: servers[0].url,
|
||||||
|
path: '/w/' + videoIds[0],
|
||||||
|
query: {
|
||||||
|
toto: 'hello',
|
||||||
|
start: '1m2s'
|
||||||
|
},
|
||||||
|
accept: 'text/html',
|
||||||
|
expectedStatus: HttpStatusCode.OK_200
|
||||||
|
})
|
||||||
|
|
||||||
|
const expectedLink = `<link rel="alternate" type="application/json+oembed" href="${servers[0].url}/services/oembed?` +
|
||||||
|
`url=http%3A%2F%2F${servers[0].hostname}%3A${servers[0].port}%2Fw%2F${servers[0].store.video.shortUUID}%3Fstart%3D1m2s" ` +
|
||||||
|
`title="${servers[0].store.video.name}" />`
|
||||||
|
|
||||||
|
expect(res.text).to.contain(expectedLink)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should forward query params to playlist oEmbed discrovery URL', async function () {
|
||||||
|
const res = await makeGetRequest({
|
||||||
|
url: servers[0].url,
|
||||||
|
path: '/w/p/' + playlistIds[0],
|
||||||
|
query: {
|
||||||
|
toto: 'hello',
|
||||||
|
playlistPosition: '415'
|
||||||
|
},
|
||||||
|
accept: 'text/html',
|
||||||
|
expectedStatus: HttpStatusCode.OK_200
|
||||||
|
})
|
||||||
|
|
||||||
|
const expectedLink = `<link rel="alternate" type="application/json+oembed" href="${servers[0].url}/services/oembed?` +
|
||||||
|
`url=http%3A%2F%2F${servers[0].hostname}%3A${servers[0].port}%2Fw%2Fp%2F${playlist.shortUUID}%3FplaylistPosition%3D415" ` +
|
||||||
|
`title="${playlistName}" />`
|
||||||
|
|
||||||
|
expect(res.text).to.contain(expectedLink)
|
||||||
|
})
|
||||||
|
|
||||||
after(async function () {
|
after(async function () {
|
||||||
await cleanupTests(servers)
|
await cleanupTests(servers)
|
||||||
})
|
})
|
||||||
|
|
|
@ -79,7 +79,11 @@ function buildPlayerURLQuery (inputQueryUrl: string) {
|
||||||
'subtitle',
|
'subtitle',
|
||||||
'bigPlayBackgroundColor',
|
'bigPlayBackgroundColor',
|
||||||
'mode',
|
'mode',
|
||||||
'foregroundColor'
|
'foregroundColor',
|
||||||
|
'playbackRate',
|
||||||
|
'api',
|
||||||
|
'waitPasswordFromEmbedAPI',
|
||||||
|
'playlistPosition'
|
||||||
])
|
])
|
||||||
|
|
||||||
const params = new URLSearchParams()
|
const params = new URLSearchParams()
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
import { escapeHTML } from '@peertube/peertube-core-utils'
|
import { addQueryParams, escapeHTML } from '@peertube/peertube-core-utils'
|
||||||
import { HttpStatusCode, VideoPlaylistPrivacy } from '@peertube/peertube-models'
|
import { HttpStatusCode, VideoPlaylistPrivacy } from '@peertube/peertube-models'
|
||||||
import { toCompleteUUID } from '@server/helpers/custom-validators/misc.js'
|
import { toCompleteUUID } from '@server/helpers/custom-validators/misc.js'
|
||||||
|
import { Memoize } from '@server/helpers/memoize.js'
|
||||||
|
import { VideoPlaylistModel } from '@server/models/video/video-playlist.js'
|
||||||
|
import { MVideoPlaylist, MVideoPlaylistFull } from '@server/types/models/index.js'
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import validator from 'validator'
|
import validator from 'validator'
|
||||||
import { CONFIG } from '../../../initializers/config.js'
|
import { CONFIG } from '../../../initializers/config.js'
|
||||||
import { MEMOIZE_TTL, WEBSERVER } from '../../../initializers/constants.js'
|
import { MEMOIZE_TTL, WEBSERVER } from '../../../initializers/constants.js'
|
||||||
import { Memoize } from '@server/helpers/memoize.js'
|
|
||||||
import { VideoPlaylistModel } from '@server/models/video/video-playlist.js'
|
|
||||||
import { MVideoPlaylistFull } from '@server/types/models/index.js'
|
|
||||||
import { TagsHtml } from './tags-html.js'
|
|
||||||
import { PageHtml } from './page-html.js'
|
|
||||||
import { CommonEmbedHtml } from './common-embed-html.js'
|
import { CommonEmbedHtml } from './common-embed-html.js'
|
||||||
|
import { PageHtml } from './page-html.js'
|
||||||
|
import { TagsHtml } from './tags-html.js'
|
||||||
|
|
||||||
export class PlaylistHtml {
|
export class PlaylistHtml {
|
||||||
|
|
||||||
|
@ -39,7 +39,9 @@ export class PlaylistHtml {
|
||||||
playlist: videoPlaylist,
|
playlist: videoPlaylist,
|
||||||
addEmbedInfo: true,
|
addEmbedInfo: true,
|
||||||
addOG: true,
|
addOG: true,
|
||||||
addTwitterCard: true
|
addTwitterCard: true,
|
||||||
|
|
||||||
|
currentQuery: req.query
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +64,10 @@ export class PlaylistHtml {
|
||||||
playlist,
|
playlist,
|
||||||
addEmbedInfo: true,
|
addEmbedInfo: true,
|
||||||
addOG: false,
|
addOG: false,
|
||||||
addTwitterCard: false
|
addTwitterCard: false,
|
||||||
|
|
||||||
|
// TODO: Implement it so we can send query params to oembed service
|
||||||
|
currentQuery: {}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,8 +82,10 @@ export class PlaylistHtml {
|
||||||
addOG: boolean
|
addOG: boolean
|
||||||
addTwitterCard: boolean
|
addTwitterCard: boolean
|
||||||
addEmbedInfo: boolean
|
addEmbedInfo: boolean
|
||||||
|
|
||||||
|
currentQuery: Record<string, string>
|
||||||
}) {
|
}) {
|
||||||
const { html, playlist, addEmbedInfo, addOG, addTwitterCard } = options
|
const { html, playlist, addEmbedInfo, addOG, addTwitterCard, currentQuery = {} } = options
|
||||||
const escapedTruncatedDescription = TagsHtml.buildEscapedTruncatedDescription(playlist.description)
|
const escapedTruncatedDescription = TagsHtml.buildEscapedTruncatedDescription(playlist.description)
|
||||||
|
|
||||||
let htmlResult = TagsHtml.addTitleTag(html, playlist.name)
|
let htmlResult = TagsHtml.addTitleTag(html, playlist.name)
|
||||||
|
@ -117,7 +124,22 @@ export class PlaylistHtml {
|
||||||
schemaType,
|
schemaType,
|
||||||
ogType,
|
ogType,
|
||||||
twitterCard,
|
twitterCard,
|
||||||
embed
|
|
||||||
|
embed,
|
||||||
|
oembedUrl: this.getOEmbedUrl(playlist, currentQuery)
|
||||||
}, { playlist })
|
}, { playlist })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static getOEmbedUrl (playlist: MVideoPlaylist, currentQuery: Record<string, string>) {
|
||||||
|
const base = WEBSERVER.URL + playlist.getWatchStaticPath()
|
||||||
|
|
||||||
|
const additionalQuery: Record<string, string> = {}
|
||||||
|
const allowedParams = new Set([ 'playlistPosition' ])
|
||||||
|
|
||||||
|
for (const [ key, value ] of Object.entries(currentQuery)) {
|
||||||
|
if (allowedParams.has(key)) additionalQuery[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return addQueryParams(base, additionalQuery)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,8 @@ type Tags = {
|
||||||
duration?: string
|
duration?: string
|
||||||
views?: number
|
views?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oembedUrl?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HookContext = {
|
type HookContext = {
|
||||||
|
@ -74,14 +76,14 @@ export class TagsHtml {
|
||||||
const twitterCardMetaTags = this.generateTwitterCardMetaTagsOptions(tagsValues)
|
const twitterCardMetaTags = this.generateTwitterCardMetaTagsOptions(tagsValues)
|
||||||
const schemaTags = await this.generateSchemaTagsOptions(tagsValues, context)
|
const schemaTags = await this.generateSchemaTagsOptions(tagsValues, context)
|
||||||
|
|
||||||
const { url, escapedTitle, embed, indexationPolicy } = tagsValues
|
const { url, escapedTitle, oembedUrl, indexationPolicy } = tagsValues
|
||||||
|
|
||||||
const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = []
|
const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = []
|
||||||
|
|
||||||
if (embed) {
|
if (oembedUrl) {
|
||||||
oembedLinkTags.push({
|
oembedLinkTags.push({
|
||||||
type: 'application/json+oembed',
|
type: 'application/json+oembed',
|
||||||
href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
|
href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(oembedUrl),
|
||||||
escapedTitle
|
escapedTitle
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { escapeHTML } from '@peertube/peertube-core-utils'
|
import { addQueryParams, escapeHTML } from '@peertube/peertube-core-utils'
|
||||||
import { HttpStatusCode, VideoPrivacy } from '@peertube/peertube-models'
|
import { HttpStatusCode, VideoPrivacy } from '@peertube/peertube-models'
|
||||||
import { toCompleteUUID } from '@server/helpers/custom-validators/misc.js'
|
import { toCompleteUUID } from '@server/helpers/custom-validators/misc.js'
|
||||||
import { Memoize } from '@server/helpers/memoize.js'
|
import { Memoize } from '@server/helpers/memoize.js'
|
||||||
|
@ -39,6 +39,7 @@ export class VideoHtml {
|
||||||
return this.buildVideoHTML({
|
return this.buildVideoHTML({
|
||||||
html,
|
html,
|
||||||
video,
|
video,
|
||||||
|
currentQuery: req.query,
|
||||||
addEmbedInfo: true,
|
addEmbedInfo: true,
|
||||||
addOG: true,
|
addOG: true,
|
||||||
addTwitterCard: true
|
addTwitterCard: true
|
||||||
|
@ -64,7 +65,10 @@ export class VideoHtml {
|
||||||
video,
|
video,
|
||||||
addEmbedInfo: true,
|
addEmbedInfo: true,
|
||||||
addOG: false,
|
addOG: false,
|
||||||
addTwitterCard: false
|
addTwitterCard: false,
|
||||||
|
|
||||||
|
// TODO: Implement it so we can send query params to oembed service
|
||||||
|
currentQuery: {}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +83,10 @@ export class VideoHtml {
|
||||||
addOG: boolean
|
addOG: boolean
|
||||||
addTwitterCard: boolean
|
addTwitterCard: boolean
|
||||||
addEmbedInfo: boolean
|
addEmbedInfo: boolean
|
||||||
|
|
||||||
|
currentQuery: Record<string, string>
|
||||||
}) {
|
}) {
|
||||||
const { html, video, addEmbedInfo, addOG, addTwitterCard } = options
|
const { html, video, addEmbedInfo, addOG, addTwitterCard, currentQuery = {} } = options
|
||||||
const escapedTruncatedDescription = TagsHtml.buildEscapedTruncatedDescription(video.description)
|
const escapedTruncatedDescription = TagsHtml.buildEscapedTruncatedDescription(video.description)
|
||||||
|
|
||||||
let customHTML = TagsHtml.addTitleTag(html, video.name)
|
let customHTML = TagsHtml.addTitleTag(html, video.name)
|
||||||
|
@ -107,6 +113,7 @@ export class VideoHtml {
|
||||||
|
|
||||||
return TagsHtml.addTags(customHTML, {
|
return TagsHtml.addTags(customHTML, {
|
||||||
url: WEBSERVER.URL + video.getWatchStaticPath(),
|
url: WEBSERVER.URL + video.getWatchStaticPath(),
|
||||||
|
|
||||||
escapedSiteName: escapeHTML(CONFIG.INSTANCE.NAME),
|
escapedSiteName: escapeHTML(CONFIG.INSTANCE.NAME),
|
||||||
escapedTitle: escapeHTML(video.name),
|
escapedTitle: escapeHTML(video.name),
|
||||||
escapedTruncatedDescription,
|
escapedTruncatedDescription,
|
||||||
|
@ -118,9 +125,24 @@ export class VideoHtml {
|
||||||
image: { url: WEBSERVER.URL + video.getPreviewStaticPath() },
|
image: { url: WEBSERVER.URL + video.getPreviewStaticPath() },
|
||||||
|
|
||||||
embed,
|
embed,
|
||||||
|
oembedUrl: this.getOEmbedUrl(video, currentQuery),
|
||||||
|
|
||||||
ogType,
|
ogType,
|
||||||
twitterCard,
|
twitterCard,
|
||||||
schemaType
|
schemaType
|
||||||
}, { video })
|
}, { video })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static getOEmbedUrl (video: MVideo, currentQuery: Record<string, string>) {
|
||||||
|
const base = WEBSERVER.URL + video.getWatchStaticPath()
|
||||||
|
|
||||||
|
const additionalQuery: Record<string, string> = {}
|
||||||
|
const allowedParams = new Set([ 'start' ])
|
||||||
|
|
||||||
|
for (const [ key, value ] of Object.entries(currentQuery)) {
|
||||||
|
if (allowedParams.has(key)) additionalQuery[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return addQueryParams(base, additionalQuery)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue