2022-09-19 18:12:54 +03:00
|
|
|
const {RedirectEvent} = require('@tryghost/link-redirects');
|
2023-05-02 23:43:47 +03:00
|
|
|
const LinkClick = require('./ClickEvent');
|
2022-09-19 18:12:54 +03:00
|
|
|
const PostLink = require('./PostLink');
|
|
|
|
const ObjectID = require('bson-objectid').default;
|
2022-10-18 13:03:27 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const nql = require('@tryghost/nql');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const tpl = require('@tryghost/tpl');
|
2022-10-21 14:21:24 +03:00
|
|
|
const moment = require('moment');
|
2022-09-19 18:12:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} ILinkClickRepository
|
|
|
|
* @prop {(event: LinkClick) => Promise<void>} save
|
2022-09-22 14:39:52 +03:00
|
|
|
* @prop {({filter: string}) => Promise<LinkClick[]>} getAll
|
2022-09-19 18:12:54 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} ILinkRedirect
|
|
|
|
* @prop {ObjectID} link_id
|
|
|
|
* @prop {URL} to
|
|
|
|
* @prop {URL} from
|
|
|
|
*/
|
|
|
|
|
2022-09-22 14:39:52 +03:00
|
|
|
/**
|
|
|
|
* @typedef {import('./FullPostLink')} FullPostLink
|
|
|
|
*/
|
|
|
|
|
2022-09-19 18:12:54 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} ILinkRedirectService
|
|
|
|
* @prop {(to: URL, slug: string) => Promise<ILinkRedirect>} addRedirect
|
|
|
|
* @prop {() => Promise<string>} getSlug
|
2022-09-22 14:39:52 +03:00
|
|
|
* @prop {({filter: string}) => Promise<ILinkRedirect[]>} getAll
|
2022-10-18 13:03:27 +03:00
|
|
|
* @prop {({filter: string}) => Promise<string[]>} getFilteredIds
|
2022-09-19 18:12:54 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} IPostLinkRepository
|
|
|
|
* @prop {(postLink: PostLink) => Promise<void>} save
|
2022-09-22 14:39:52 +03:00
|
|
|
* @prop {({filter: string}) => Promise<FullPostLink[]>} getAll
|
2022-10-18 13:03:27 +03:00
|
|
|
* @prop {(linkIds: array, data, options) => Promise<FullPostLink[]>} updateLinks
|
2022-09-19 18:12:54 +03:00
|
|
|
*/
|
|
|
|
|
2022-10-18 13:03:27 +03:00
|
|
|
const messages = {
|
|
|
|
invalidFilter: 'Invalid filter value received',
|
|
|
|
unsupportedBulkAction: 'Unsupported bulk action',
|
|
|
|
invalidRedirectUrl: 'Invalid redirect URL value'
|
|
|
|
};
|
|
|
|
|
2022-09-19 18:12:54 +03:00
|
|
|
class LinkClickTrackingService {
|
|
|
|
#initialised = false;
|
|
|
|
|
|
|
|
/** @type ILinkClickRepository */
|
|
|
|
#linkClickRepository;
|
|
|
|
/** @type ILinkRedirectService */
|
|
|
|
#linkRedirectService;
|
|
|
|
/** @type IPostLinkRepository */
|
|
|
|
#postLinkRepository;
|
2022-09-23 17:19:16 +03:00
|
|
|
/** @type DomainEvents */
|
|
|
|
#DomainEvents;
|
2022-10-18 13:03:27 +03:00
|
|
|
/** @type {Object} */
|
|
|
|
#LinkRedirect;
|
|
|
|
/** @type {Object} */
|
|
|
|
#urlUtils;
|
2022-09-19 18:12:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {ILinkClickRepository} deps.linkClickRepository
|
|
|
|
* @param {ILinkRedirectService} deps.linkRedirectService
|
|
|
|
* @param {IPostLinkRepository} deps.postLinkRepository
|
2022-09-23 17:19:16 +03:00
|
|
|
* @param {DomainEvents} deps.DomainEvents
|
2022-10-18 13:03:27 +03:00
|
|
|
* @param {urlUtils} deps.urlUtils
|
2022-09-19 18:12:54 +03:00
|
|
|
*/
|
|
|
|
constructor(deps) {
|
|
|
|
this.#linkClickRepository = deps.linkClickRepository;
|
|
|
|
this.#linkRedirectService = deps.linkRedirectService;
|
|
|
|
this.#postLinkRepository = deps.postLinkRepository;
|
2022-09-23 17:19:16 +03:00
|
|
|
this.#DomainEvents = deps.DomainEvents;
|
2022-10-18 13:03:27 +03:00
|
|
|
this.#urlUtils = deps.urlUtils;
|
2022-09-19 18:12:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
if (this.#initialised) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.subscribe();
|
|
|
|
this.#initialised = true;
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:39:52 +03:00
|
|
|
/**
|
|
|
|
* @param {object} options
|
|
|
|
* @param {string} options.filter
|
|
|
|
* @return {Promise<FullPostLink[]>}
|
|
|
|
*/
|
|
|
|
async getLinks(options) {
|
|
|
|
return await this.#postLinkRepository.getAll({
|
|
|
|
filter: options.filter
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-18 13:03:27 +03:00
|
|
|
/**
|
|
|
|
* validate and manage the new redirect url in filter
|
|
|
|
* `to` url needs decoding and transformation to relative url for comparision
|
|
|
|
* @param {string} filter
|
|
|
|
* @returns {Object} parsed filter
|
|
|
|
* @throws {errors.BadRequestError}
|
|
|
|
*/
|
|
|
|
#parseLinkFilter(filter) {
|
|
|
|
try {
|
|
|
|
const filterJson = nql(filter).parse();
|
|
|
|
const postId = filterJson?.$and?.[0]?.post_id;
|
|
|
|
const redirectUrl = new URL(filterJson?.$and?.[1]?.to);
|
|
|
|
if (!postId || !redirectUrl) {
|
|
|
|
throw new errors.BadRequestError({
|
|
|
|
message: tpl(messages.invalidFilter)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
postId,
|
|
|
|
redirectUrl
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
throw new errors.BadRequestError({
|
|
|
|
message: tpl(messages.invalidFilter),
|
|
|
|
context: e.message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#getRedirectLinkWithAttribution({newLink, oldLink, postId}) {
|
|
|
|
const newUrl = new URL(newLink);
|
|
|
|
const oldUrl = new URL(oldLink);
|
|
|
|
// append newsletter ref query param from oldUrl to newUrl
|
|
|
|
if (oldUrl.searchParams.has('ref')) {
|
|
|
|
newUrl.searchParams.set('ref', oldUrl.searchParams.get('ref'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// append post attribution to site urls
|
|
|
|
const isSite = this.#urlUtils.isSiteUrl(newUrl);
|
|
|
|
if (isSite) {
|
|
|
|
newUrl.searchParams.set('attribution_type', 'post');
|
|
|
|
newUrl.searchParams.set('attribution_id', postId);
|
|
|
|
}
|
|
|
|
return newUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
async #updateLinks(data, options) {
|
|
|
|
const filterOptions = _.pick(options, ['transacting', 'context', 'filter']);
|
|
|
|
|
|
|
|
// decode and parse filter to manage new redirect url
|
|
|
|
const {postId, redirectUrl} = this.#parseLinkFilter(filterOptions.filter);
|
|
|
|
|
|
|
|
// manages transformation of current url to relative for comparision
|
|
|
|
const transformedOldUrl = this.#urlUtils.absoluteToTransformReady(redirectUrl.href);
|
2023-11-13 14:00:20 +03:00
|
|
|
const filterQuery = `post_id:'${postId}'+to:'${transformedOldUrl}'`;
|
2022-10-18 13:03:27 +03:00
|
|
|
|
|
|
|
const updatedFilterOptions = {
|
|
|
|
...filterOptions,
|
|
|
|
filter: filterQuery
|
|
|
|
};
|
|
|
|
|
|
|
|
// get new redirect link with proper attribution
|
|
|
|
const newRedirectUrl = this.#getRedirectLinkWithAttribution({
|
|
|
|
newLink: data.meta?.link?.to,
|
|
|
|
oldLink: redirectUrl.href,
|
|
|
|
postId
|
|
|
|
});
|
|
|
|
const linkIds = await this.#linkRedirectService.getFilteredIds(updatedFilterOptions);
|
|
|
|
|
|
|
|
const bulkUpdateOptions = _.pick(options, ['transacting']);
|
|
|
|
const updateData = {
|
|
|
|
to: this.#urlUtils.absoluteToTransformReady(newRedirectUrl.href),
|
2022-10-21 14:21:24 +03:00
|
|
|
updated_at: moment().format('YYYY-MM-DD HH:mm:ss')
|
2022-10-18 13:03:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return await this.#postLinkRepository.updateLinks(linkIds, updateData, bulkUpdateOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
async bulkEdit(data, options) {
|
|
|
|
if (data.action === 'updateLink') {
|
|
|
|
return await this.#updateLinks(data, options);
|
|
|
|
}
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: tpl(messages.unsupportedBulkAction)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-27 19:20:34 +03:00
|
|
|
* @private (not using # to allow tests)
|
2022-09-19 18:12:54 +03:00
|
|
|
* Replace URL with a redirect that redirects to the original URL, and link that redirect with the given post
|
|
|
|
*/
|
|
|
|
async addRedirectToUrl(url, post) {
|
|
|
|
// Generate a unique redirect slug
|
2022-09-23 14:32:44 +03:00
|
|
|
const slugUrl = await this.#linkRedirectService.getSlugUrl();
|
2022-09-19 18:12:54 +03:00
|
|
|
|
|
|
|
// Add redirect for link click tracking
|
2022-09-23 14:32:44 +03:00
|
|
|
const redirect = await this.#linkRedirectService.addRedirect(slugUrl, url);
|
2022-09-19 18:12:54 +03:00
|
|
|
|
|
|
|
// Store a reference of the link against the post
|
|
|
|
const postLink = new PostLink({
|
|
|
|
link_id: redirect.link_id,
|
|
|
|
post_id: ObjectID.createFromHexString(post.id)
|
|
|
|
});
|
|
|
|
await this.#postLinkRepository.save(postLink);
|
|
|
|
|
|
|
|
return redirect.from;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add tracking to a URL and returns a new URL (if link click tracking is enabled)
|
|
|
|
* @param {URL} url
|
|
|
|
* @param {Post} post
|
|
|
|
* @param {string} memberUuid
|
|
|
|
* @return {Promise<URL>}
|
|
|
|
*/
|
|
|
|
async addTrackingToUrl(url, post, memberUuid) {
|
|
|
|
url = await this.addRedirectToUrl(url, post);
|
|
|
|
url.searchParams.set('m', memberUuid);
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe() {
|
2022-09-23 17:19:16 +03:00
|
|
|
this.#DomainEvents.subscribe(RedirectEvent, async (event) => {
|
2022-09-19 18:12:54 +03:00
|
|
|
const uuid = event.data.url.searchParams.get('m');
|
|
|
|
if (!uuid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const click = new LinkClick({
|
|
|
|
member_uuid: uuid,
|
|
|
|
link_id: event.data.link.link_id
|
|
|
|
});
|
|
|
|
await this.#linkClickRepository.save(click);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = LinkClickTrackingService;
|