From f7a58ecafcf80d55fbaff5613054870ef48f036e Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Tue, 6 Sep 2022 11:29:35 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20OpenSea=20NFT=20OEmbeds?= =?UTF-8?q?=20(#15372)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://github.com/TryGhost/Team/issues/1879 OpenSea updated their URL format for NFTs after adding support for Solana which broke our regex, this updates to support the new format. --- ghost/core/core/server/services/nft-oembed.js | 8 +++++--- .../unit/server/services/nft-oembed.test.js | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 ghost/core/test/unit/server/services/nft-oembed.test.js diff --git a/ghost/core/core/server/services/nft-oembed.js b/ghost/core/core/server/services/nft-oembed.js index 48e9d6d322..bc2a293e72 100644 --- a/ghost/core/core/server/services/nft-oembed.js +++ b/ghost/core/core/server/services/nft-oembed.js @@ -3,7 +3,7 @@ * @typedef {import('./oembed').IExternalRequest} IExternalRequest */ -const OPENSEA_PATH_REGEX = /^\/assets\/(0x[a-f0-9]+)\/(\d+)/; +const OPENSEA_ETH_PATH_REGEX = /^\/assets\/ethereum\/(0x[a-f0-9]+)\/(\d+)/; /** * @implements ICustomProvider @@ -11,6 +11,8 @@ const OPENSEA_PATH_REGEX = /^\/assets\/(0x[a-f0-9]+)\/(\d+)/; class NFTOEmbedProvider { /** * @param {object} dependencies + * @param {object} dependencies.config + * @param {string} [dependencies.config.apiKey] - An OpenSea API key */ constructor(dependencies) { this.dependencies = dependencies; @@ -21,7 +23,7 @@ class NFTOEmbedProvider { * @returns {Promise} */ async canSupportRequest(url) { - return url.host === 'opensea.io' && OPENSEA_PATH_REGEX.test(url.pathname); + return url.host === 'opensea.io' && OPENSEA_ETH_PATH_REGEX.test(url.pathname); } /** @@ -31,7 +33,7 @@ class NFTOEmbedProvider { * @returns {Promise} */ async getOEmbedData(url, externalRequest) { - const [match, transaction, asset] = url.pathname.match(OPENSEA_PATH_REGEX); + const [match, transaction, asset] = url.pathname.match(OPENSEA_ETH_PATH_REGEX); if (!match) { return null; } diff --git a/ghost/core/test/unit/server/services/nft-oembed.test.js b/ghost/core/test/unit/server/services/nft-oembed.test.js new file mode 100644 index 0000000000..1c68242a90 --- /dev/null +++ b/ghost/core/test/unit/server/services/nft-oembed.test.js @@ -0,0 +1,20 @@ +const assert = require('assert'); +const NFTOembedProvider = require('../../../../core/server/services/nft-oembed'); + +describe('NFTOEmbedProvider', function () { + it('Can support requests for OpenSea Ethereum NTFs', async function () { + const provider = new NFTOembedProvider({ + config: { + apiKey: 'fake-api-key' + } + }); + + const ethereumNFTURL = new URL( + 'https://opensea.io/assets/ethereum/0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb/9998' + ); + + const supportsRequest = await provider.canSupportRequest(ethereumNFTURL); + + assert(supportsRequest, 'Should support ethereum NFT URL'); + }); +});