0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

🐛 Fixed email analytics crashing when processing unsubscribe/complaint events (#14883)

refs https://github.com/TryGhost/Team/issues/1649

The event handlers for `unsubscribed`/`complained` events originating from Mailgun were still using the now-deleted `member.subscribed` field resulting in errors and aborted attempts at processing the Mailgun event log.

- `member.subscribed` no longer exists, replaced that part of the query with a delete of member<->newsletter association rows from the `members_newsletters` pivot table
- kept the member `updated_at` bump so we have some timestamp record of an update
- added creation of `member_subscribe_event` records for the newsletter unsubscribes to keep stats and history in check
  - the unsubscribed/complained events do or at least should originate from a member action so "member" is more appropriate than "system" even though the event is being handled by an internal system
This commit is contained in:
Kevin Ansfield 2022-05-24 12:38:42 +01:00 committed by GitHub
parent cdb365c29d
commit c46303cb2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
const {EventProcessor} = require('@tryghost/email-analytics-service');
const {default: ObjectID} = require('bson-objectid');
const moment = require('moment-timezone');
class GhostEventProcessor extends EventProcessor {
@ -130,33 +131,43 @@ class GhostEventProcessor extends EventProcessor {
}
async handleUnsubscribed(event) {
const memberId = await this.getMemberId(event);
if (!memberId) {
return false;
}
const updateResult = await this.db.knex('members')
.where('id', '=', memberId)
.update({
subscribed: false,
updated_at: moment.utc().toDate()
});
return updateResult > 0;
return this._unsubscribeFromNewsletters(event);
}
async handleComplained(event) {
return this._unsubscribeFromNewsletters(event);
}
async _unsubscribeFromNewsletters(event) {
const memberId = await this.getMemberId(event);
if (!memberId) {
return false;
}
const subscribedNewsletterIds = await this.db.knex('members_newsletters')
.where('member_id', '=', memberId)
.pluck('newsletter_id');
await this.db.knex('members_newsletters')
.where('member_id', '=', memberId)
.del();
const nowUTC = moment.utc().toDate();
for (const newsletterId of subscribedNewsletterIds) {
await this.db.knex('members_subscribe_events').insert({
id: ObjectID().toHexString(),
member_id: memberId,
newsletter_id: newsletterId,
subscribed: false,
created_at: nowUTC,
source: 'member'
});
}
const updateResult = await this.db.knex('members')
.where('id', '=', memberId)
.update({
subscribed: false,
updated_at: moment.utc().toDate()
});