ActivityPub-federated video streaming platform using P2P directly in your web browser
Fix mention redirection
Chocobozzzafd913530c1f
diff --git a/client/src/app/core/renderer/linkifier.service.ts b/client/src/app/core/renderer/linkifier.service.tsindex 6f7deb969d..3581ad9284 100644--- a/client/src/app/core/renderer/linkifier.service.ts+++ b/client/src/app/core/renderer/linkifier.service.ts@@ -19,7 +19,7 @@ export class LinkifierService {},formatHref: {mention: (href: string) => {- return getAPIUrl() + '/services/redirect/accounts/' + href.substring(1)+ return getAPIUrl() + '/services/redirect/actors/' + href.substring(1)}}}diff --git a/packages/server-commands/src/videos/services-command.ts b/packages/server-commands/src/videos/services-command.tsindex ade10cd3af..4487a52412 100644--- a/packages/server-commands/src/videos/services-command.ts+++ b/packages/server-commands/src/videos/services-command.ts@@ -2,13 +2,14 @@ import { HttpStatusCode } from '@peertube/peertube-models'import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js'export class ServicesCommand extends AbstractCommand {-- getOEmbed (options: OverrideCommandOptions & {- oembedUrl: string- format?: string- maxHeight?: number- maxWidth?: number- }) {+ getOEmbed (+ options: OverrideCommandOptions & {+ oembedUrl: string+ format?: string+ maxHeight?: number+ maxWidth?: number+ }+ ) {const path = '/services/oembed'const query = {url: options.oembedUrl,@@ -26,4 +27,27 @@ export class ServicesCommand extends AbstractCommand {defaultExpectedStatus: HttpStatusCode.OK_200})}++ async getActorRedirection (+ options: OverrideCommandOptions & {+ handle: string+ type: 'actors' | 'accounts'+ }+ ) {+ const path = `/services/redirect/${options.type}/${encodeURIComponent(options.handle)}`++ const res = await this.getRequest({+ ...options,++ path,+ implicitToken: false,+ defaultExpectedStatus: HttpStatusCode.FOUND_302+ })++ if (options.expectedStatus !== HttpStatusCode.NOT_FOUND_404) {+ return res.headers['location']+ }++ return undefined+ }}diff --git a/packages/tests/src/api/check-params/services.ts b/packages/tests/src/api/check-params/services.tsindex 0b0466d848..d317a7ef52 100644--- a/packages/tests/src/api/check-params/services.ts+++ b/packages/tests/src/api/check-params/services.ts@@ -71,7 +71,6 @@ describe('Test services API validators', function () {})describe('Test oEmbed API validators', function () {-it('Should fail with an invalid url', async function () {const embedUrl = 'hello.com'await checkParamEmbed(server, embedUrl)diff --git a/packages/tests/src/api/server/services.ts b/packages/tests/src/api/server/services.tsindex bf8d3e2eef..c577a3695f 100644--- a/packages/tests/src/api/server/services.ts+++ b/packages/tests/src/api/server/services.ts@@ -1,159 +1,202 @@/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */-import { expect } from 'chai'-import { Video, VideoPlaylistPrivacy } from '@peertube/peertube-models'+import { HttpStatusCode, Video, VideoPlaylistPrivacy } from '@peertube/peertube-models'import {cleanupTests,- createSingleServer,+ createMultipleServers,PeerTubeServer,setAccessTokensToServers,setDefaultVideoChannel} from '@peertube/peertube-server-commands'+import { expect } from 'chai'describe('Test services', function () {- let server: PeerTubeServer = null-- let playlistShortUUID: string- let playlistUUID: string- let playlistDisplayName: string-- let video: Video-- const urlSuffixes = [- {- input: '',- output: ''- },- {- input: '?param=1',- output: ''- },- {- input: '?muted=1&warningTitle=0&toto=1',- output: '?muted=1&warningTitle=0'- }- ]+ let servers: PeerTubeServer[]before(async function () {this.timeout(120000)- server = await createSingleServer(1)+ servers = await createMultipleServers(2)- await setAccessTokensToServers([ server ])- await setDefaultVideoChannel([ server ])-- {- const attributes = { name: 'my super name' }- await server.videos.upload({ attributes })+ await setAccessTokensToServers(servers)+ await setDefaultVideoChannel(servers)+ })- const { data } = await server.videos.list()- video = data[0]- }+ describe('OEmbed', function () {+ let playlistShortUUID: string+ let playlistUUID: string+ let playlistDisplayName: string++ let video: Video++ const urlSuffixes = [+ {+ input: '',+ output: ''+ },+ {+ input: '?param=1',+ output: ''+ },+ {+ input: '?muted=1&warningTitle=0&toto=1',+ output: '?muted=1&warningTitle=0'+ }+ ]- {- const created = await server.playlists.create({- attributes: {- displayName: 'The Life and Times of Scrooge McDuck',- privacy: VideoPlaylistPrivacy.PUBLIC,- videoChannelId: server.store.channel.id- }- })+ before(async function () {+ {+ const attributes = { name: 'my super name' }+ await servers[0].videos.upload({ attributes })- playlistUUID = created.uuid- playlistShortUUID = created.shortUUID- playlistDisplayName = 'The Life and Times of Scrooge McDuck'+ const { data } = await servers[0].videos.list()+ video = data[0]+ }- await server.playlists.addElement({- playlistId: created.id,- attributes: {- videoId: video.id+ {+ const created = await servers[0].playlists.create({+ attributes: {+ displayName: 'The Life and Times of Scrooge McDuck',+ privacy: VideoPlaylistPrivacy.PUBLIC,+ videoChannelId: servers[0].store.channel.id+ }+ })++ playlistUUID = created.uuid+ playlistShortUUID = created.shortUUID+ playlistDisplayName = 'The Life and Times of Scrooge McDuck'++ await servers[0].playlists.addElement({+ playlistId: created.id,+ attributes: {+ videoId: video.id+ }+ })+ }+ })++ it('Should have a valid oEmbed video response', async function () {+ for (const basePath of [ '/videos/watch/', '/w/' ]) {+ for (const suffix of urlSuffixes) {+ const oembedUrl = servers[0].url + basePath + video.uuid + suffix.input++ const res = await servers[0].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://${servers[0].host}/videos/embed/${video.shortUUID}${suffix.output}" ` ++ 'style="border: none" allow="fullscreen"></iframe>'++ const expectedThumbnailUrl = 'http://' + servers[0].host + video.previewPath++ expect(res.body.html).to.equal(expectedHtml)+ expect(res.body.title).to.equal(video.name)+ expect(res.body.author_name).to.equal(servers[0].store.channel.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 () {+ for (const basePath of [ '/videos/watch/playlist/', '/w/p/' ]) {+ for (const suffix of urlSuffixes) {+ const oembedUrl = servers[0].url + basePath + playlistUUID + suffix.input++ const res = await servers[0].services.getOEmbed({ oembedUrl })+ const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" ' ++ `title="${playlistDisplayName}" src="http://${servers[0].host}/video-playlists/embed/${playlistShortUUID}${suffix.output}" ` ++ 'style="border: none" allow="fullscreen"></iframe>'++ 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(servers[0].store.channel.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 video response', async function () {- for (const basePath of [ '/videos/watch/', '/w/' ]) {- for (const suffix of urlSuffixes) {- const oembedUrl = server.url + basePath + video.uuid + suffix.input+ it('Should have a oEmbed response with query params', async function () {+ const query = '?start=1m2s&stop=2'+ const oembedUrl = `http://${servers[0].host}/w/${video.uuid}${query}&unknown=3`+ const res = await servers[0].services.getOEmbed({ oembedUrl })- 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.shortUUID}${suffix.output}" ` +- 'style="border: none" allow="fullscreen"></iframe>'+ const expectedHtml = '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" ' ++ `title="${video.name}" src="http://${servers[0].host}/videos/embed/${video.shortUUID}${query}" ` ++ 'style="border: none" allow="fullscreen"></iframe>'- const expectedThumbnailUrl = 'http://' + server.host + video.previewPath+ expect(res.body.html).to.equal(expectedHtml)+ })++ it('Should have a valid oEmbed response with small max height query', async function () {+ for (const basePath of [ '/videos/watch/', '/w/' ]) {+ const oembedUrl = 'http://' + servers[0].host + basePath + video.uuid+ const format = 'json'+ const maxHeight = 50+ const maxWidth = 50++ const res = await servers[0].services.getOEmbed({ oembedUrl, format, maxHeight, maxWidth })+ const expectedHtml = '<iframe width="50" height="50" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" ' ++ `title="${video.name}" src="http://${servers[0].host}/videos/embed/${video.shortUUID}" ` ++ 'style="border: none" allow="fullscreen"></iframe>'expect(res.body.html).to.equal(expectedHtml)expect(res.body.title).to.equal(video.name)- expect(res.body.author_name).to.equal(server.store.channel.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)+ expect(res.body.author_name).to.equal(servers[0].store.channel.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')}- }+ })})- it('Should have a valid playlist oEmbed response', async function () {- for (const basePath of [ '/videos/watch/playlist/', '/w/p/' ]) {- for (const suffix of urlSuffixes) {- const oembedUrl = server.url + basePath + playlistUUID + suffix.input+ describe('Actor redirection', function () {+ it('Should redirect to actor url from handle', async function () {+ for (const type of [ 'actors' as const, 'accounts' as const ]) {+ {+ const url = await servers[1].services.getActorRedirection({ handle: `root`, type })+ expect(url).to.equal(`${servers[1].url}/accounts/root`)+ }- 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="${playlistDisplayName}" src="http://${server.host}/video-playlists/embed/${playlistShortUUID}${suffix.output}" ` +- 'style="border: none" allow="fullscreen"></iframe>'+ {+ await servers[1].services.getActorRedirection({ handle: `unknown`, type, expectedStatus: HttpStatusCode.NOT_FOUND_404 })- 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.store.channel.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)+ await servers[1].services.getActorRedirection({+ handle: `unknown@${servers[0].host}`,+ type,+ expectedStatus: HttpStatusCode.NOT_FOUND_404+ })+ }}- }- })-- 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.shortUUID}${query}" ` +- 'style="border: none" allow="fullscreen"></iframe>'- expect(res.body.html).to.equal(expectedHtml)- })-- it('Should have a valid oEmbed response with small max height query', async function () {- for (const basePath of [ '/videos/watch/', '/w/' ]) {- const oembedUrl = 'http://' + server.host + basePath + video.uuid- const format = 'json'- const maxHeight = 50- const maxWidth = 50+ {+ await servers[1].services.getActorRedirection({+ handle: `root@${servers[0].host}`,+ type: 'accounts',+ expectedStatus: HttpStatusCode.NOT_FOUND_404+ })+ }- const res = await server.services.getOEmbed({ oembedUrl, format, maxHeight, maxWidth })- const expectedHtml = '<iframe width="50" height="50" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" ' +- `title="${video.name}" src="http://${server.host}/videos/embed/${video.shortUUID}" ` +- 'style="border: none" allow="fullscreen"></iframe>'+ {+ const url = await servers[1].services.getActorRedirection({ handle: `root@${servers[0].host}`, type: 'actors' })+ expect(url).to.equal(`${servers[0].url}/accounts/root`)+ }- expect(res.body.html).to.equal(expectedHtml)- expect(res.body.title).to.equal(video.name)- expect(res.body.author_name).to.equal(server.store.channel.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')- }+ {+ const url = await servers[1].services.getActorRedirection({ handle: `@root_channel@${servers[0].host}`, type: 'actors' })+ expect(url).to.equal(`${servers[0].url}/video-channels/root_channel`)+ }+ })})after(async function () {- await cleanupTests([ server ])+ await cleanupTests(servers)})})diff --git a/server/core/controllers/services.ts b/server/core/controllers/services.tsindex c2a7b9a761..28985467fa 100644--- a/server/core/controllers/services.ts+++ b/server/core/controllers/services.ts@@ -1,14 +1,22 @@-import express from 'express'-import cors from 'cors'import { escapeHTML, forceNumber } from '@peertube/peertube-core-utils'+import { HttpStatusCode } from '@peertube/peertube-models'+import { getOrCreateAPActor } from '@server/lib/activitypub/actors/get.js'+import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors/webfinger.js'+import { AccountModel } from '@server/models/account/account.js'+import { VideoChannelModel } from '@server/models/video/video-channel.js'import { MChannelSummary } from '@server/types/models/index.js'+import cors from 'cors'+import express from 'express'import { EMBED_SIZE, PREVIEWS_SIZE, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants.js'import { apiRateLimiter, asyncMiddleware, oembedValidator } from '../middlewares/index.js'import { accountHandleGetValidatorFactory } from '../middlewares/validators/index.js'+import { logger } from '@server/helpers/logger.js'const servicesRouter = express.Router()servicesRouter.use('/oembed', cors(), apiRateLimiter, asyncMiddleware(oembedValidator), generateOEmbed)++// TODO: deprecated, remove in PeerTube 8.1servicesRouter.use('/redirect/accounts/:handle',apiRateLimiter,@@ -16,6 +24,12 @@ servicesRouter.use(redirectToAccountUrl)+servicesRouter.use(+ '/redirect/actors/:handle',+ apiRateLimiter,+ asyncMiddleware(redirectToActorUrl)+)+// ---------------------------------------------------------------------------export {@@ -161,6 +175,32 @@ function buildOEmbed (options: {return json}+// ---------------------------------------------------------------------------function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {return res.redirect(res.locals.account.Actor.url)}++async function redirectToActorUrl (req: express.Request, res: express.Response, next: express.NextFunction) {+ const handle = req.params.handle++ if (handle.includes('@')) {+ try {+ const actorUrl = await loadActorUrlOrGetFromWebfinger(handle)+ const actor = await getOrCreateAPActor(actorUrl, 'all')++ if (actor) return res.redirect(actor.url)+ } catch (error) {+ logger.info('Cannot redirect to actor URL from handle.', { handle, error })++ return res.sendStatus(HttpStatusCode.NOT_FOUND_404)+ }+ } else {+ const account = await AccountModel.loadByHandle(handle)+ if (account) return res.redirect(account.Actor.url)++ const channel = await VideoChannelModel.loadByHandleAndPopulateAccount(handle)+ if (channel) return res.redirect(channel.Actor.url)+ }++ return res.sendStatus(HttpStatusCode.NOT_FOUND_404)+}diff --git a/server/core/lib/activitypub/actors/get.ts b/server/core/lib/activitypub/actors/get.tsindex 6ba67873a5..73f071100d 100644--- a/server/core/lib/activitypub/actors/get.ts+++ b/server/core/lib/activitypub/actors/get.ts@@ -16,6 +16,7 @@ import { checkUrlsSameHost } from '../url.js'import { refreshActorIfNeeded } from './refresh.js'import { APActorCreator, fetchRemoteActor } from './shared/index.js'+// FIXME: use an object for paramsfunction getOrCreateAPActor (activityActor: string | ActivityPubActor,fetchType: 'all',diff --git a/server/core/middlewares/validators/shared/accounts.ts b/server/core/middlewares/validators/shared/accounts.tsindex 67837045a8..20db233101 100644--- a/server/core/middlewares/validators/shared/accounts.ts+++ b/server/core/middlewares/validators/shared/accounts.ts@@ -49,7 +49,7 @@ function doesAccountExist (options: {if (!account) {res.fail({status: HttpStatusCode.NOT_FOUND_404,- message: 'Account not found'+ message: req.t('Account not found')})return false}@@ -65,7 +65,7 @@ function doesAccountExist (options: {if (checkIsLocal && account.Actor.isLocal() === false) {res.fail({status: HttpStatusCode.FORBIDDEN_403,- message: 'This account is not owned.'+ message: req.t('This account is not owned by the platform')})return falsediff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yamlindex 2084967adf..eea78fb408 100644--- a/support/doc/api/openapi.yaml+++ b/support/doc/api/openapi.yaml@@ -1,7 +1,7 @@openapi: 3.0.3info:title: PeerTube- version: 7.1.0+ version: 7..0contact:name: PeerTube Communityurl: https://joinpeertube.org