From c2102ea42b77b4302ad7d776a9b26bd7a331e535 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Tue, 13 Sep 2022 18:28:18 -0400 Subject: [PATCH] Updated link replacement service to use deps refs https://github.com/TryGhost/Team/issues/1886 --- .../server/services/link-replacement/index.js | 5 +- .../link-replacement/lib/link-replacement.js | 53 ++++++++++++------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/ghost/core/core/server/services/link-replacement/index.js b/ghost/core/core/server/services/link-replacement/index.js index 8fa82b1997..a2a463e510 100644 --- a/ghost/core/core/server/services/link-replacement/index.js +++ b/ghost/core/core/server/services/link-replacement/index.js @@ -9,7 +9,10 @@ class LinkReplacementServiceWrapper { const LinkReplacementService = require('@tryghost/link-replacement'); // Expose the service - this.service = new LinkReplacementService({}); + this.service = new LinkReplacementService({ + linkRedirectService: require('../link-redirection').service, + linkClickTrackingService: require('../link-click-tracking').service + }); } } diff --git a/ghost/link-replacement/lib/link-replacement.js b/ghost/link-replacement/lib/link-replacement.js index 252e52848b..15f3659b82 100644 --- a/ghost/link-replacement/lib/link-replacement.js +++ b/ghost/link-replacement/lib/link-replacement.js @@ -1,22 +1,39 @@ -class LinkReplacementService { - /// Placeholder method - async createRedirect(url) { - return Promise.resolve({ - from: 'https://example.com/r/' + Math.random().toString(36).substring(2, 15), - to: url - }); - } +/** + * @typedef {object} ILinkRedirect + * @prop {URL} to + * @prop {from} to + * @prop {from} to + */ - // Todo: move to different service - addTrackingToRedirect(redirect, memberUuid) { - const newUrl = new URL(redirect.from); - newUrl.searchParams.append('m', memberUuid); - return newUrl; +/** + * @typedef {object} ILinkRedirectService + * @prop {(to: URL) => Promise} addRedirect + */ + +/** + * @typedef {object} ILinkClickTrackingService + * @prop {(link: ILinkRedirect) => Promise} addTrackingToRedirect + */ + +class LinkReplacementService { + /** @type ILinkRedirectService */ + #linkRedirectService; + /** @type ILinkClickTrackingService */ + #linkClickTrackingService; + + /** + * @param {object} deps + * @param {ILinkRedirectService} deps.linkRedirectService + * @param {ILinkClickTrackingService} deps.linkClickTrackingService + */ + constructor(deps) { + this.#linkRedirectService = deps.linkRedirectService; + this.#linkClickTrackingService = deps.linkClickTrackingService; } async replaceLink(url, newsletter, post) { // Can probably happen in one call to the MemberAttributionService (but just to make clear what happens here) - + // 1. Add attribution // TODO: this should move the the attribution service in the future url.searchParams.append('rel', newsletter.get('slug') + '-newsletter'); @@ -24,10 +41,11 @@ class LinkReplacementService { url.searchParams.append('attribution_type', 'post'); // 2. Add redirect for link click tracking - const redirect = await this.createRedirect(url.toString()); + const redirect = await this.#linkRedirectService.addRedirect(url); // 3. Add member tracking - return this.addTrackingToRedirect(redirect, '--uuid--'); + const result = await this.#linkClickTrackingService.addTrackingToRedirect(redirect, '--uuid--'); + return result; } /** @@ -58,7 +76,6 @@ class LinkReplacementService { } return Promise.resolve($.html()); - } + } } module.exports = LinkReplacementService; -