Skip to content

Commit

Permalink
Add email unsubscribe header (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris authored Aug 23, 2024
1 parent b4bf9d5 commit bd5f6fc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions apps/platform/src/providers/email/Email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface Email {
text: string
html: string
headers?: Record<string, any>
list?: { unsubscribe: string }
}
8 changes: 8 additions & 0 deletions apps/platform/src/providers/email/EmailChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import App from '../../app'
import { Variables, Wrap } from '../../render'
import { EmailTemplate } from '../../render/Template'
import { unsubscribeEmailLink } from '../../subscriptions/SubscriptionService'
import { encodeHashid, pick } from '../../utilities'
import { Email } from './Email'
import EmailProvider from './EmailProvider'
Expand Down Expand Up @@ -34,6 +35,13 @@ export default class EmailChannel {
'X-Campaign-Id': encodeHashid(variables.context.campaign_id),
'X-Subscription-Id': encodeHashid(variables.context.subscription_id),
},
list: {
unsubscribe: unsubscribeEmailLink({
userId: variables.user.id,
campaignId: variables.context.campaign_id,
referenceId: variables.context.reference_id,
}),
},
}
const result = await this.provider.send(email)
return {
Expand Down
9 changes: 1 addition & 8 deletions apps/platform/src/providers/email/EmailProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ export default abstract class EmailProvider extends Provider {
static group = 'email' as ProviderGroup

async send(message: Email): Promise<any> {
const list = this.unsubscribe
? { unsubscribe: [this.unsubscribe] }
: undefined

return await this.transport?.sendMail({
...message,
list,
})
return await this.transport?.sendMail(message)
}

async verify(): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion apps/platform/src/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const Render = (template: string, { user, event, journey, context }: Vari
context,
unsubscribeEmailUrl: new Handlebars.SafeString(unsubscribeEmailLink({
userId: user.id,
campaignId: context?.campaign_id,
campaignId: context.campaign_id,
referenceId: context.reference_id,
})),
preferencesUrl: new Handlebars.SafeString(preferencesLink(user.id)),
Expand Down
10 changes: 10 additions & 0 deletions apps/platform/src/subscriptions/SubscriptionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ publicRouter.get('/email', async ctx => {
ctx.body = '<html><body><h3>You have been unsubscribed!</h3></body></html>'
})

publicRouter.post('/email', async ctx => {
const { user, campaign } = await encodedLinkToParts(ctx.URL)

if (!user) throw new RequestError(SubscriptionError.UnsubscribeInvalidUser)
if (!campaign) throw new RequestError(SubscriptionError.UnsubscribeInvalidCampaign)

await unsubscribe(user.id, campaign.subscription_id)
ctx.status = 200
})

/**
***
* User-facing subscription preferences page
Expand Down

0 comments on commit bd5f6fc

Please sign in to comment.