From a1eda903a497857017495f37a1fd3593ba7ab23c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 28 May 2021 11:36:33 +0200 Subject: Support '/w/' and '/w/p/' for watch page And use them as default in client --- server/controllers/bots.ts | 2 +- server/controllers/client.ts | 4 +- server/controllers/feeds.ts | 2 +- server/middlewares/validators/oembed.ts | 24 +++- server/models/video/video-playlist.ts | 2 +- server/models/video/video.ts | 2 +- server/tests/api/server/services.ts | 104 +++++++++-------- server/tests/client.ts | 196 ++++++++++++++++++-------------- server/tools/peertube-watch.ts | 2 +- 9 files changed, 191 insertions(+), 147 deletions(-) (limited to 'server') diff --git a/server/controllers/bots.ts b/server/controllers/bots.ts index 8d1fa72f3..9e92063d4 100644 --- a/server/controllers/bots.ts +++ b/server/controllers/bots.ts @@ -75,7 +75,7 @@ async function getSitemapLocalVideoUrls () { }) return data.map(v => ({ - url: WEBSERVER.URL + '/videos/watch/' + v.uuid, + url: WEBSERVER.URL + '/w/' + v.uuid, video: [ { title: v.name, diff --git a/server/controllers/client.ts b/server/controllers/client.ts index 35e5af9d1..fcccc48e0 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts @@ -19,8 +19,8 @@ const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html') // Special route that add OpenGraph and oEmbed tags // Do not use a template engine for a so little thing -clientsRouter.use('/videos/watch/playlist/:id', asyncMiddleware(generateWatchPlaylistHtmlPage)) -clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage)) +clientsRouter.use([ '/w/p/:id', '/videos/watch/playlist/:id' ], asyncMiddleware(generateWatchPlaylistHtmlPage)) +clientsRouter.use([ '/w/:id', '/videos/watch/:id' ], asyncMiddleware(generateWatchHtmlPage)) clientsRouter.use([ '/accounts/:nameWithHost', '/a/:nameWithHost' ], asyncMiddleware(generateAccountHtmlPage)) clientsRouter.use([ '/video-channels/:nameWithHost', '/c/:nameWithHost' ], asyncMiddleware(generateVideoChannelHtmlPage)) clientsRouter.use('/@:nameWithHost', asyncMiddleware(generateActorHtmlPage)) diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts index f0717bbbc..865f5c2a1 100644 --- a/server/controllers/feeds.ts +++ b/server/controllers/feeds.ts @@ -293,7 +293,7 @@ function addVideosToFeed (feed, videos: VideoModel[]) { feed.addItem({ title: video.name, id: video.url, - link: WEBSERVER.URL + '/videos/watch/' + video.uuid, + link: WEBSERVER.URL + '/w/' + video.uuid, description: video.getTruncatedDescription(), content: video.description, author: [ diff --git a/server/middlewares/validators/oembed.ts b/server/middlewares/validators/oembed.ts index 2a7dc257b..165eda6d5 100644 --- a/server/middlewares/validators/oembed.ts +++ b/server/middlewares/validators/oembed.ts @@ -4,15 +4,29 @@ import { join } from 'path' import { fetchVideo } from '@server/helpers/video' import { VideoPlaylistModel } from '@server/models/video/video-playlist' import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' import { isTestInstance } from '../../helpers/core-utils' import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { logger } from '../../helpers/logger' import { WEBSERVER } from '../../initializers/constants' import { areValidationErrors } from './utils' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -const startVideoPlaylistsURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch', 'playlist') + '/' -const startVideosURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch') + '/' +const playlistPaths = [ + join('videos', 'watch', 'playlist'), + join('w', 'p') +] + +const videoPaths = [ + join('videos', 'watch'), + 'w' +] + +function buildUrls (paths: string[]) { + return paths.map(p => WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, p) + '/') +} + +const startPlaylistURLs = buildUrls(playlistPaths) +const startVideoURLs = buildUrls(videoPaths) const watchRegex = /([^/]+)$/ const isURLOptions = { @@ -43,8 +57,8 @@ const oembedValidator = [ const url = req.query.url as string - const isPlaylist = url.startsWith(startVideoPlaylistsURL) - const isVideo = isPlaylist ? false : url.startsWith(startVideosURL) + const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u)) + const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u)) const startIsOk = isVideo || isPlaylist diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index c293287d3..98cea1b64 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -496,7 +496,7 @@ export class VideoPlaylistModel extends Model>> { } getWatchStaticPath () { - return '/videos/watch/' + this.uuid + return '/w/' + this.uuid } getEmbedStaticPath () { diff --git a/server/tests/api/server/services.ts b/server/tests/api/server/services.ts index f0fa91674..ea64e4040 100644 --- a/server/tests/api/server/services.ts +++ b/server/tests/api/server/services.ts @@ -67,61 +67,67 @@ describe('Test services', function () { }) it('Should have a valid oEmbed video response', async function () { - const oembedUrl = 'http://localhost:' + server.port + '/videos/watch/' + video.uuid - - const res = await getOEmbed(server.url, oembedUrl) - const expectedHtml = '' - const expectedThumbnailUrl = 'http://localhost:' + server.port + video.previewPath - - expect(res.body.html).to.equal(expectedHtml) - expect(res.body.title).to.equal(video.name) - expect(res.body.author_name).to.equal(server.videoChannel.displayName) - expect(res.body.width).to.equal(560) - expect(res.body.height).to.equal(315) - expect(res.body.thumbnail_url).to.equal(expectedThumbnailUrl) - expect(res.body.thumbnail_width).to.equal(850) - expect(res.body.thumbnail_height).to.equal(480) + for (const basePath of [ '/videos/watch/', '/w/' ]) { + const oembedUrl = 'http://localhost:' + server.port + basePath + video.uuid + + const res = await getOEmbed(server.url, oembedUrl) + const expectedHtml = '' + const expectedThumbnailUrl = 'http://localhost:' + server.port + video.previewPath + + expect(res.body.html).to.equal(expectedHtml) + expect(res.body.title).to.equal(video.name) + expect(res.body.author_name).to.equal(server.videoChannel.displayName) + expect(res.body.width).to.equal(560) + expect(res.body.height).to.equal(315) + expect(res.body.thumbnail_url).to.equal(expectedThumbnailUrl) + expect(res.body.thumbnail_width).to.equal(850) + expect(res.body.thumbnail_height).to.equal(480) + } }) it('Should have a valid playlist oEmbed response', async function () { - const oembedUrl = 'http://localhost:' + server.port + '/videos/watch/playlist/' + playlistUUID - - const res = await getOEmbed(server.url, oembedUrl) - const expectedHtml = '' - - expect(res.body.html).to.equal(expectedHtml) - expect(res.body.title).to.equal('The Life and Times of Scrooge McDuck') - expect(res.body.author_name).to.equal(server.videoChannel.displayName) - expect(res.body.width).to.equal(560) - expect(res.body.height).to.equal(315) - expect(res.body.thumbnail_url).exist - expect(res.body.thumbnail_width).to.equal(280) - expect(res.body.thumbnail_height).to.equal(157) + for (const basePath of [ '/videos/watch/playlist/', '/w/p/' ]) { + const oembedUrl = 'http://localhost:' + server.port + basePath + playlistUUID + + const res = await getOEmbed(server.url, oembedUrl) + const expectedHtml = '' + + expect(res.body.html).to.equal(expectedHtml) + expect(res.body.title).to.equal('The Life and Times of Scrooge McDuck') + expect(res.body.author_name).to.equal(server.videoChannel.displayName) + expect(res.body.width).to.equal(560) + expect(res.body.height).to.equal(315) + expect(res.body.thumbnail_url).exist + expect(res.body.thumbnail_width).to.equal(280) + expect(res.body.thumbnail_height).to.equal(157) + } }) it('Should have a valid oEmbed response with small max height query', async function () { - const oembedUrl = 'http://localhost:' + server.port + '/videos/watch/' + video.uuid - const format = 'json' - const maxHeight = 50 - const maxWidth = 50 - - const res = await getOEmbed(server.url, oembedUrl, format, maxHeight, maxWidth) - const expectedHtml = '' - - expect(res.body.html).to.equal(expectedHtml) - expect(res.body.title).to.equal(video.name) - expect(res.body.author_name).to.equal(server.videoChannel.displayName) - expect(res.body.height).to.equal(50) - expect(res.body.width).to.equal(50) - expect(res.body).to.not.have.property('thumbnail_url') - expect(res.body).to.not.have.property('thumbnail_width') - expect(res.body).to.not.have.property('thumbnail_height') + for (const basePath of [ '/videos/watch/', '/w/' ]) { + const oembedUrl = 'http://localhost:' + server.port + basePath + video.uuid + const format = 'json' + const maxHeight = 50 + const maxWidth = 50 + + const res = await getOEmbed(server.url, oembedUrl, format, maxHeight, maxWidth) + const expectedHtml = '' + + expect(res.body.html).to.equal(expectedHtml) + expect(res.body.title).to.equal(video.name) + expect(res.body.author_name).to.equal(server.videoChannel.displayName) + expect(res.body.height).to.equal(50) + expect(res.body.width).to.equal(50) + expect(res.body).to.not.have.property('thumbnail_url') + expect(res.body).to.not.have.property('thumbnail_width') + expect(res.body).to.not.have.property('thumbnail_height') + } }) after(async function () { diff --git a/server/tests/client.ts b/server/tests/client.ts index f33e5c1da..253a95624 100644 --- a/server/tests/client.ts +++ b/server/tests/client.ts @@ -54,6 +54,9 @@ describe('Test a client controllers', function () { const channelDescription = 'my super channel description' + const watchVideoBasePaths = [ '/videos/watch/', '/w/' ] + const watchPlaylistBasePaths = [ '/videos/watch/playlist/', '/w/p/' ] + before(async function () { this.timeout(120000) @@ -111,35 +114,40 @@ describe('Test a client controllers', function () { }) describe('oEmbed', function () { + it('Should have valid oEmbed discovery tags for videos', async function () { - const path = '/videos/watch/' + servers[0].video.uuid - const res = await request(servers[0].url) - .get(path) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) + for (const basePath of watchVideoBasePaths) { + const path = basePath + servers[0].video.uuid + const res = await request(servers[0].url) + .get(path) + .set('Accept', 'text/html') + .expect(HttpStatusCode.OK_200) - const port = servers[0].port + const port = servers[0].port - const expectedLink = '` + const expectedLink = '` - expect(res.text).to.contain(expectedLink) + expect(res.text).to.contain(expectedLink) + } }) it('Should have valid oEmbed discovery tags for a playlist', async function () { - const res = await request(servers[0].url) - .get('/videos/watch/playlist/' + playlistUUID) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) + for (const basePath of watchPlaylistBasePaths) { + const res = await request(servers[0].url) + .get(basePath + playlistUUID) + .set('Accept', 'text/html') + .expect(HttpStatusCode.OK_200) - const port = servers[0].port + const port = servers[0].port - const expectedLink = '` + const expectedLink = '` - expect(res.text).to.contain(expectedLink) + expect(res.text).to.contain(expectedLink) + } }) }) @@ -165,6 +173,26 @@ describe('Test a client controllers', function () { expect(text).to.contain(``) } + async function watchVideoPageTest (path: string) { + const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 }) + const text = res.text + + expect(text).to.contain(``) + expect(text).to.contain(``) + expect(text).to.contain('') + expect(text).to.contain(``) + } + + async function watchPlaylistPageTest (path: string) { + const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 }) + const text = res.text + + expect(text).to.contain(``) + expect(text).to.contain(``) + expect(text).to.contain('') + expect(text).to.contain(``) + } + it('Should have valid Open Graph tags on the account page', async function () { await accountPageTest('/accounts/' + servers[0].user.username) await accountPageTest('/a/' + servers[0].user.username) @@ -177,40 +205,16 @@ describe('Test a client controllers', function () { await channelPageTest('/@' + servers[0].videoChannel.name) }) - it('Should have valid Open Graph tags on the watch page with video id', async function () { - const res = await request(servers[0].url) - .get('/videos/watch/' + servers[0].video.id) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) - - expect(res.text).to.contain(``) - expect(res.text).to.contain(``) - expect(res.text).to.contain('') - expect(res.text).to.contain(``) - }) - - it('Should have valid Open Graph tags on the watch page with video uuid', async function () { - const res = await request(servers[0].url) - .get('/videos/watch/' + servers[0].video.uuid) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) - - expect(res.text).to.contain(``) - expect(res.text).to.contain(``) - expect(res.text).to.contain('') - expect(res.text).to.contain(``) + it('Should have valid Open Graph tags on the watch page', async function () { + await watchVideoPageTest('/videos/watch/' + servers[0].video.id) + await watchVideoPageTest('/videos/watch/' + servers[0].video.uuid) + await watchVideoPageTest('/w/' + servers[0].video.uuid) + await watchVideoPageTest('/w/' + servers[0].video.id) }) it('Should have valid Open Graph tags on the watch playlist page', async function () { - const res = await request(servers[0].url) - .get('/videos/watch/playlist/' + playlistUUID) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) - - expect(res.text).to.contain(``) - expect(res.text).to.contain(``) - expect(res.text).to.contain('') - expect(res.text).to.contain(``) + await watchPlaylistPageTest('/videos/watch/playlist/' + playlistUUID) + await watchPlaylistPageTest('/w/p/' + playlistUUID) }) }) @@ -238,28 +242,36 @@ describe('Test a client controllers', function () { expect(text).to.contain(``) } - it('Should have valid twitter card on the watch video page', async function () { - const res = await request(servers[0].url) - .get('/videos/watch/' + servers[0].video.uuid) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) + async function watchVideoPageTest (path: string) { + const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 }) + const text = res.text - expect(res.text).to.contain('') - expect(res.text).to.contain('') - expect(res.text).to.contain(``) - expect(res.text).to.contain(``) + expect(text).to.contain('') + expect(text).to.contain('') + expect(text).to.contain(``) + expect(text).to.contain(``) + } + + async function watchPlaylistPageTest (path: string) { + const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 }) + const text = res.text + + expect(text).to.contain('') + expect(text).to.contain('') + expect(text).to.contain(``) + expect(text).to.contain(``) + } + + it('Should have valid twitter card on the watch video page', async function () { + await watchVideoPageTest('/videos/watch/' + servers[0].video.id) + await watchVideoPageTest('/videos/watch/' + servers[0].video.uuid) + await watchVideoPageTest('/w/' + servers[0].video.uuid) + await watchVideoPageTest('/w/' + servers[0].video.id) }) it('Should have valid twitter card on the watch playlist page', async function () { - const res = await request(servers[0].url) - .get('/videos/watch/playlist/' + playlistUUID) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) - - expect(res.text).to.contain('') - expect(res.text).to.contain('') - expect(res.text).to.contain(``) - expect(res.text).to.contain(``) + await watchPlaylistPageTest('/videos/watch/playlist/' + playlistUUID) + await watchPlaylistPageTest('/w/p/' + playlistUUID) }) it('Should have valid twitter card on the account page', async function () { @@ -304,24 +316,32 @@ describe('Test a client controllers', function () { expect(text).to.contain('') } - it('Should have valid twitter card on the watch video page', async function () { - const res = await request(servers[0].url) - .get('/videos/watch/' + servers[0].video.uuid) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) + async function watchVideoPageTest (path: string) { + const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 }) + const text = res.text - expect(res.text).to.contain('') - expect(res.text).to.contain('') + expect(text).to.contain('') + expect(text).to.contain('') + } + + async function watchPlaylistPageTest (path: string) { + const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', statusCodeExpected: HttpStatusCode.OK_200 }) + const text = res.text + + expect(text).to.contain('') + expect(text).to.contain('') + } + + it('Should have valid twitter card on the watch video page', async function () { + await watchVideoPageTest('/videos/watch/' + servers[0].video.id) + await watchVideoPageTest('/videos/watch/' + servers[0].video.uuid) + await watchVideoPageTest('/w/' + servers[0].video.uuid) + await watchVideoPageTest('/w/' + servers[0].video.id) }) it('Should have valid twitter card on the watch playlist page', async function () { - const res = await request(servers[0].url) - .get('/videos/watch/playlist/' + playlistUUID) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) - - expect(res.text).to.contain('') - expect(res.text).to.contain('') + await watchPlaylistPageTest('/videos/watch/playlist/' + playlistUUID) + await watchPlaylistPageTest('/w/p/' + playlistUUID) }) it('Should have valid twitter card on the account page', async function () { @@ -378,8 +398,10 @@ describe('Test a client controllers', function () { }) it('Should use the original video URL for the canonical tag', async function () { - const res = await makeHTMLRequest(servers[1].url, '/videos/watch/' + servers[0].video.uuid) - expect(res.text).to.contain(``) + for (const basePath of watchVideoBasePaths) { + const res = await makeHTMLRequest(servers[1].url, basePath + servers[0].video.uuid) + expect(res.text).to.contain(``) + } }) it('Should use the original account URL for the canonical tag', async function () { @@ -403,8 +425,10 @@ describe('Test a client controllers', function () { }) it('Should use the original playlist URL for the canonical tag', async function () { - const res = await makeHTMLRequest(servers[1].url, '/videos/watch/playlist/' + playlistUUID) - expect(res.text).to.contain(``) + for (const basePath of watchPlaylistBasePaths) { + const res = await makeHTMLRequest(servers[1].url, basePath + playlistUUID) + expect(res.text).to.contain(``) + } }) }) diff --git a/server/tools/peertube-watch.ts b/server/tools/peertube-watch.ts index 6d9cfa3b7..3ca3e242a 100644 --- a/server/tools/peertube-watch.ts +++ b/server/tools/peertube-watch.ts @@ -30,7 +30,7 @@ function run (url: string, options: program.OptionValues) { const cmd = 'node ' + join(__dirname, 'node_modules', 'webtorrent-hybrid', 'bin', 'cmd.js') const args = ` --${options.gui} ` + - url.replace('videos/watch', 'download/torrents') + + url.replace(/(\/videos\/watch\/)|\/w\//, '/download/torrents/') + `-${options.resolution}.torrent` try { -- cgit v1.2.3