Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added job for email link click processing #21845

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions ghost/core/core/server/services/link-redirection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const LinkRedirectRepository = require('./LinkRedirectRepository');
const adapterManager = require('../adapter-manager');
const config = require('../../../shared/config');
const EventRegistry = require('../../lib/common/events');
const JobManager = require('../jobs');
const path = require('path');
const settingsCache = require('../../../shared/settings-cache');

class LinkRedirectsServiceWrapper {
async init() {
Expand All @@ -26,11 +29,30 @@ class LinkRedirectsServiceWrapper {
// Expose the service
this.service = new LinkRedirectsService({
linkRedirectRepository: this.linkRedirectRepository,
config: {
urlConfig: {
baseURL: new URL(urlUtils.getSiteUrl())
}
},
submitHandleRedirectJob,
config
});
}
}

const submitHandleRedirectJob = async ({uuid, linkId, timestamp}) => {
console.log(`Submitting redirect job to queue for ${uuid} with linkId ${linkId} and timestamp ${timestamp}`);
await JobManager.addQueuedJob({
name: `link-redirect-event-${uuid}-${linkId}-${timestamp}`,
metadata: {
job: path.resolve(__dirname, path.join('jobs', 'link-redirect-event')),
name: 'link-redirect-event',
data: {
uuid,
linkId,
timestamp,
timezone: settingsCache.get('timezone') || 'Etc/UTC'
}
}
});
};

module.exports = new LinkRedirectsServiceWrapper();
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const db = require('../../../../data/db');
const errors = require('@tryghost/errors');
const moment = require('moment-timezone');
const ObjectID = require('bson-objectid').default;

module.exports = async function handleRedirect(data) {
console.log('handleRedirect', data);
const {uuid, linkId, timestamp, timezone} = data;
const member = await db.knex.select('id').from('members').where('uuid', uuid).first();
if (!member) {
return; // throwing error causes the job to fail and be retried
// throw new errors.NotFoundError({message: `Member with uuid ${uuid} not found`});
}
// const formattedTimestamp = new Date(timestamp).toISOString(); // for sqlite support
try {
await handleMemberLinkClick({memberId: member.id, linkId, timestamp});
await handleLastSeenAtUpdate({memberId: member.id, timestamp, timezone});
} catch (error) {
console.error(error);
}
};

const handleMemberLinkClick = async ({memberId, linkId, timestamp}) => {
// Create a Date object from the timestamp
const date = new Date(timestamp);

// Format the date to 'YYYY-MM-DD HH:MM:SS' in UTC
const formattedTimestamp = date.getUTCFullYear() + '-' +
String(date.getUTCMonth() + 1).padStart(2, '0') + '-' +
String(date.getUTCDate()).padStart(2, '0') + ' ' +
String(date.getUTCHours()).padStart(2, '0') + ':' +
String(date.getUTCMinutes()).padStart(2, '0') + ':' +
String(date.getUTCSeconds()).padStart(2, '0');

return await db.knex('members_click_events')
.insert({
id: new ObjectID().toHexString(),
created_at: formattedTimestamp,
member_id: memberId,
redirect_id: linkId
});
};

const handleLastSeenAtUpdate = async (data) => {
const result = await db.knex.transaction(async (trx) => {
// To avoid a race condition, we lock the member row for update, then the last_seen_at field again to prevent simultaneous updates
const currentMember = await trx('members')
.where({id: data.memberId})
.forUpdate()
.first();

if (!currentMember) {
throw new errors.NotFoundError({message: `Member with id ${data.memberId} not found`});
}

const currentMemberLastSeenAt = currentMember.last_seen_at;
if (currentMemberLastSeenAt === null || moment(moment.utc(data.timestamp).tz(data.timezone).startOf('day')).isAfter(currentMemberLastSeenAt)) {
await trx('members')
.where({id: data.memberId})
.update({
last_seen_at: moment.utc(data.timestamp).format('YYYY-MM-DD HH:mm:ss')
});

// Fetch the updated member data
const updatedMember = await trx('members')
.where({id: data.memberId})
.first();

// Return data for the job manager to emit the event
const eventData = {
nodeEvents: [{
name: 'member.edited',
data: updatedMember
}]
};
return {result: updatedMember, eventData};
}
return undefined;
});
return result;
};
3 changes: 2 additions & 1 deletion ghost/core/core/shared/config/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@
"linkClickTrackingCacheMemberUuid": false,
"backgroundJobs": {
"emailAnalytics": true,
"clickTrackingLastSeenAtUpdater": true
"clickTrackingLastSeenAtUpdater": true,
"clickTracking": false
},
"portal": {
"url": "https://cdn.jsdelivr.net/ghost/portal@~{version}/umd/portal.min.js",
Expand Down
3 changes: 2 additions & 1 deletion ghost/core/core/shared/sentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ const ALLOWED_HTTP_TRANSACTIONS = [
'/members/api', // any Members API call
'/:slug', // any frontend post/page
'/author', // any frontend author page
'/tag' // any frontend tag page
'/tag', // any frontend tag page
'/r' // any email redirect
].map((path) => {
// Sentry names HTTP transactions like: "<HTTP_METHOD> <PATH>" i.e. "GET /ghost/api/content/settings"
// Match any of the paths above with any HTTP method, and also the homepage "GET /"
Expand Down
1 change: 1 addition & 0 deletions ghost/job-manager/lib/JobQueueManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class JobQueueManager {
}

async handleJobError(job, jobMetadata, error) {
logging.error('Error in job queue:', error);
let errorMessage;
if (error instanceof Error) {
errorMessage = error.message;
Expand Down
36 changes: 25 additions & 11 deletions ghost/link-redirects/lib/LinkRedirectsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,31 @@ class LinkRedirectsService {
#linkRedirectRepository;
/** @type URL */
#baseURL;

/** @type object */
#config;
/** @type String */
#redirectURLPrefix = 'r/';
/** @type Function */
#submitHandleRedirectJob;

/**
* @param {object} deps
* @param {ILinkRedirectRepository} deps.linkRedirectRepository
* @param {object} deps.urlConfig
* @param {URL} deps.urlConfig.baseURL
* @param {object} deps.config
* @param {URL} deps.config.baseURL
* @param {Function} deps.submitHandleRedirectJob
*/
constructor(deps) {
this.#linkRedirectRepository = deps.linkRedirectRepository;
if (!deps.config.baseURL.pathname.endsWith('/')) {
this.#baseURL = new URL(deps.config.baseURL);
if (!deps.urlConfig.baseURL.pathname.endsWith('/')) {
this.#baseURL = new URL(deps.urlConfig.baseURL);
this.#baseURL.pathname += '/';
} else {
this.#baseURL = deps.config.baseURL;
this.#baseURL = deps.urlConfig.baseURL;
}
this.#config = deps.config;
this.#submitHandleRedirectJob = deps.submitHandleRedirectJob;
this.handleRequest = this.handleRequest.bind(this);
}

Expand Down Expand Up @@ -105,12 +112,19 @@ class LinkRedirectsService {
return next();
}

const event = RedirectEvent.create({
url,
link
});

DomainEvents.dispatch(event);
if (this.#config?.get('services:jobs:queue:enabled') && this.#config?.get('backgroundJobs:clickTracking')) {
const uuid = url.searchParams.get('m');
if (uuid) {
await this.#submitHandleRedirectJob({uuid, linkId: link.link_id, timestamp: Date.now()});
}
} else {
const event = RedirectEvent.create({
url,
link
});

DomainEvents.dispatch(event);
}

res.setHeader('X-Robots-Tag', 'noindex, nofollow');
return res.redirect(link.to.href);
Expand Down
58 changes: 51 additions & 7 deletions ghost/link-redirects/test/LinkRedirectsService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('LinkRedirectsService', function () {
linkRedirectRepository: {
getByURL: () => Promise.resolve(undefined)
},
config: {
urlConfig: {
baseURL: new URL('https://localhost:2368/')
}
});
Expand All @@ -38,7 +38,7 @@ describe('LinkRedirectsService', function () {
return Promise.resolve(undefined);
}
},
config: {
urlConfig: {
baseURL: new URL('https://localhost:2368/')
}
});
Expand All @@ -58,7 +58,7 @@ describe('LinkRedirectsService', function () {
};
const instance = new LinkRedirectsService({
linkRedirectRepository,
config: {
urlConfig: {
baseURL: new URL('https://localhost:2368/')
}
});
Expand All @@ -83,7 +83,7 @@ describe('LinkRedirectsService', function () {
};
const instance = new LinkRedirectsService({
linkRedirectRepository,
config: {
urlConfig: {
baseURL: new URL('https://localhost:2368/')
}
});
Expand All @@ -106,7 +106,7 @@ describe('LinkRedirectsService', function () {
};
const instance = new LinkRedirectsService({
linkRedirectRepository,
config: {
urlConfig: {
baseURL: new URL('https://localhost:2368/')
}
});
Expand All @@ -121,7 +121,7 @@ describe('LinkRedirectsService', function () {

it('does not redirect if url does not contain a redirect prefix on site with no subdir', async function () {
const instance = new LinkRedirectsService({
config: {
urlConfig: {
baseURL: new URL('https://localhost:2368/')
}
});
Expand All @@ -138,7 +138,7 @@ describe('LinkRedirectsService', function () {

it('does not redirect if url does not contain a redirect prefix on site with subdir', async function () {
const instance = new LinkRedirectsService({
config: {
urlConfig: {
baseURL: new URL('https://localhost:2368/blog')
}
});
Expand All @@ -152,5 +152,49 @@ describe('LinkRedirectsService', function () {

assert.equal(next.callCount, 1);
});

it('submits a job to the queue to process the click if enabled', async function () {
const jobStub = sinon.stub().resolves();
const linkRedirectRepository = {
getByURL: (url) => {
if (url.href === 'https://localhost:2368/r/a?m=12345678') {
return Promise.resolve({
to: new URL('https://localhost:2368/b')
});
}
return Promise.resolve(undefined);
}
};
const instance = new LinkRedirectsService({
linkRedirectRepository,
urlConfig: {
baseURL: new URL('https://localhost:2368/')
},
config: {
get: (key) => {
if (key === 'services:jobs:queue:enabled' || key === 'backgroundJobs:clickTracking') {
return true;
}
return false;
}
},
submitHandleRedirectJob: jobStub
});

const req = {
originalUrl: '/r/a?m=12345678'
};
const res = {
redirect: sinon.fake(),
setHeader: sinon.fake()
};
const next = sinon.fake();

await instance.handleRequest(req, res, next);

assert.equal(jobStub.callCount, 1);
// redirect is still served
assert.equal(res.redirect.callCount, 1);
});
});
});
Loading