From d0d45d45bc68128d72a234fe61703e1e48248e94 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Wed, 18 Jan 2023 14:38:08 +0700 Subject: [PATCH] Refactored OEmbed service into a proper service This allows us to share the implementation with other parts of the codebase, the specific usecase here being fetching the metadata from webmention sources, for display in the mentions UI, which will be borrowing a lot of stuff from the bookmark card. --- .../core/core/server/api/endpoints/oembed.js | 23 +----------------- .../core/core/server/services/oembed/index.js | 1 + .../services/{ => oembed}/nft-oembed.js | 0 .../core/server/services/oembed/service.js | 24 +++++++++++++++++++ .../services/{ => oembed}/twitter-embed.js | 0 .../services/{ => oembed}/nft-oembed.test.js | 2 +- 6 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 ghost/core/core/server/services/oembed/index.js rename ghost/core/core/server/services/{ => oembed}/nft-oembed.js (100%) create mode 100644 ghost/core/core/server/services/oembed/service.js rename ghost/core/core/server/services/{ => oembed}/twitter-embed.js (100%) rename ghost/core/test/unit/server/services/{ => oembed}/nft-oembed.test.js (86%) diff --git a/ghost/core/core/server/api/endpoints/oembed.js b/ghost/core/core/server/api/endpoints/oembed.js index f9ac28de36..1031ba9e84 100644 --- a/ghost/core/core/server/api/endpoints/oembed.js +++ b/ghost/core/core/server/api/endpoints/oembed.js @@ -1,25 +1,4 @@ -const config = require('../../../shared/config'); -const externalRequest = require('../../lib/request-external'); - -const OEmbed = require('@tryghost/oembed-service'); -const oembed = new OEmbed({config, externalRequest}); - -const NFT = require('../../services/nft-oembed'); -const nft = new NFT({ - config: { - apiKey: config.get('opensea').privateReadOnlyApiKey - } -}); - -const Twitter = require('../../services/twitter-embed'); -const twitter = new Twitter({ - config: { - bearerToken: config.get('twitter').privateReadOnlyToken - } -}); - -oembed.registerProvider(nft); -oembed.registerProvider(twitter); +const oembed = require('../../services/oembed'); module.exports = { docName: 'oembed', diff --git a/ghost/core/core/server/services/oembed/index.js b/ghost/core/core/server/services/oembed/index.js new file mode 100644 index 0000000000..102ef66d4f --- /dev/null +++ b/ghost/core/core/server/services/oembed/index.js @@ -0,0 +1 @@ +module.exports = require('./service'); diff --git a/ghost/core/core/server/services/nft-oembed.js b/ghost/core/core/server/services/oembed/nft-oembed.js similarity index 100% rename from ghost/core/core/server/services/nft-oembed.js rename to ghost/core/core/server/services/oembed/nft-oembed.js diff --git a/ghost/core/core/server/services/oembed/service.js b/ghost/core/core/server/services/oembed/service.js new file mode 100644 index 0000000000..6e44bf9344 --- /dev/null +++ b/ghost/core/core/server/services/oembed/service.js @@ -0,0 +1,24 @@ +const config = require('../../../shared/config'); +const externalRequest = require('../../lib/request-external'); + +const OEmbed = require('@tryghost/oembed-service'); +const oembed = new OEmbed({config, externalRequest}); + +const NFT = require('./nft-oembed'); +const nft = new NFT({ + config: { + apiKey: config.get('opensea').privateReadOnlyApiKey + } +}); + +const Twitter = require('./twitter-embed'); +const twitter = new Twitter({ + config: { + bearerToken: config.get('twitter').privateReadOnlyToken + } +}); + +oembed.registerProvider(nft); +oembed.registerProvider(twitter); + +module.exports = oembed; diff --git a/ghost/core/core/server/services/twitter-embed.js b/ghost/core/core/server/services/oembed/twitter-embed.js similarity index 100% rename from ghost/core/core/server/services/twitter-embed.js rename to ghost/core/core/server/services/oembed/twitter-embed.js diff --git a/ghost/core/test/unit/server/services/nft-oembed.test.js b/ghost/core/test/unit/server/services/oembed/nft-oembed.test.js similarity index 86% rename from ghost/core/test/unit/server/services/nft-oembed.test.js rename to ghost/core/test/unit/server/services/oembed/nft-oembed.test.js index 1c68242a90..1ccf6d0088 100644 --- a/ghost/core/test/unit/server/services/nft-oembed.test.js +++ b/ghost/core/test/unit/server/services/oembed/nft-oembed.test.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const NFTOembedProvider = require('../../../../core/server/services/nft-oembed'); +const NFTOembedProvider = require('../../../../../core/server/services/oembed/nft-oembed'); describe('NFTOEmbedProvider', function () { it('Can support requests for OpenSea Ethereum NTFs', async function () {