mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-28 21:33:24 +03:00
Updated link replacement service to use deps
refs https://github.com/TryGhost/Team/issues/1886
This commit is contained in:
parent
b564e2bd66
commit
c2102ea42b
@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,17 +1,34 @@
|
||||
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<ILinkRedirect>} addRedirect
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} ILinkClickTrackingService
|
||||
* @prop {(link: ILinkRedirect) => Promise<URL>} 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) {
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,4 +79,3 @@ class LinkReplacementService {
|
||||
}
|
||||
}
|
||||
module.exports = LinkReplacementService;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user