| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { ConfigService } |
| 4 | import { MailerService } |
| 5 | import { render } |
| 6 | import { sql } |
| 7 | import { createElement, type |
| 8 | |
| 9 | import { DATABASE } |
| 10 | import ThreadAssigned from './templates/thread-assigned.js |
| 11 | import ThreadComment from './templates/thread-comment.js |
| 12 | import ThreadOpened from './templates/thread-opened.js |
| 13 | import ThreadReview from './templates/thread-review.js |
| 14 | import ThreadState from './templates/thread-state.js |
| 15 | |
| 16 | export type ThreadTemplate |
| 17 | | 'thread-opened' |
| 18 | | 'thread-comment' |
| 19 | | 'thread-review' |
| 20 | | 'thread-assigned' |
| 21 | | 'thread-state' |
| 22 | |
| 23 | // The context is built per template by NotifierService, the same untyped hand-off the mailer's template adapter gets. |
| 24 | const THREAD_TEMPLATES = { |
| 25 | 'thread-opened': ThreadOpened, |
| 26 | 'thread-comment': ThreadComment, |
| 27 | 'thread-review': ThreadReview, |
| 28 | 'thread-assigned': ThreadAssigned, |
| 29 | 'thread-state': ThreadState, |
| 30 | } as unknown as |
| 31 | ThreadTemplate, |
| 32 | FunctionComponent<Record |
| 33 | >; |
| 34 | |
| 35 | @Injectable() |
| 36 | export class MailService |
| 37 | private readonly logger |
| 38 | /** Auth forms live in the web app, so every link in an email points there. */ |
| 39 | private readonly appUrl |
| 40 | /** `DELIVERY_EMAIL=queue` hands thread emails to apps/delivery, which retries them; otherwise they are sent inline, best effort. */ |
| 41 | private readonly queue |
| 42 | |
| 43 | constructor( |
| 44 | private readonly mailer |
| 45 | config: ConfigService |
| 46 | @Inject(DATABASE) |
| 47 | ) { |
| 48 | this.appUrl = |
| 49 | this.queue = |
| 50 | } |
| 51 | |
| 52 | async sendVerificationEmail( |
| 53 | to: string, |
| 54 | context: { |
| 55 | ): Promise<void |
| 56 | await this.send |
| 57 | } |
| 58 | |
| 59 | async sendVerifyAliasEmail( |
| 60 | to: string, |
| 61 | context: { |
| 62 | ): Promise<void |
| 63 | await this.send |
| 64 | } |
| 65 | |
| 66 | async sendChangeEmailEmail( |
| 67 | to: string, |
| 68 | context: { |
| 69 | ): Promise<void |
| 70 | await this.send |
| 71 | to, |
| 72 | 'Approve your new email address', |
| 73 | 'change-email', |
| 74 | context, |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | async sendResetPasswordEmail( |
| 79 | to: string, |
| 80 | context: { |
| 81 | ): Promise<void |
| 82 | await this.send |
| 83 | } |
| 84 | |
| 85 | async sendOrganizationInvitationEmail( |
| 86 | to: string, |
| 87 | context: { |
| 88 | inviter: string |
| 89 | organization: string |
| 90 | role: string |
| 91 | acceptUrl: string |
| 92 | }, |
| 93 | ): Promise<void |
| 94 | await this.send |
| 95 | to, |
| 96 | `${context.inviter} |
| 97 | 'organization-invitation', |
| 98 | context, |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | async sendRepositoryInvitationEmail( |
| 103 | to: string, |
| 104 | context: { |
| 105 | name?: string |
| 106 | inviter: string |
| 107 | repository: string |
| 108 | role: string |
| 109 | }, |
| 110 | ): Promise<void |
| 111 | await this.send |
| 112 | to, |
| 113 | `${context.inviter} |
| 114 | 'repository-invitation', |
| 115 | { ...context, |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** `owner` is `owner/repo` or an organization's slug, which is also where its settings live. */ |
| 120 | async sendWebhookDisabledEmail( |
| 121 | to: string, |
| 122 | context: { |
| 123 | name: string |
| 124 | owner: string |
| 125 | url: string |
| 126 | reason: string |
| 127 | webhookId: string |
| 128 | }, |
| 129 | ): Promise<void |
| 130 | await this.send |
| 131 | to, |
| 132 | `A webhook on ${context.owner |
| 133 | 'webhook-disabled', |
| 134 | { |
| 135 | ...context, |
| 136 | settingsUrl: `${this. |
| 137 | }, |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** Every email about one issue or pull request names the same thread in `References`, so mail clients file them as one conversation. */ |
| 142 | async sendThreadEmail( |
| 143 | to: string, |
| 144 | { |
| 145 | template, |
| 146 | threadId, |
| 147 | subject, |
| 148 | context, |
| 149 | idempotencyKey, |
| 150 | }: { |
| 151 | template: ThreadTemplate |
| 152 | threadId: string |
| 153 | subject: string |
| 154 | context: Record |
| 155 | /** The same key enqueues once, so a retried outbox event sends no duplicate. */ |
| 156 | idempotencyKey: string |
| 157 | }, |
| 158 | ): Promise<void |
| 159 | if (this.queue |
| 160 | await this.enqueueThreadEmail |
| 161 | to, |
| 162 | template, |
| 163 | subject, |
| 164 | context, |
| 165 | idempotencyKey, |
| 166 | ); |
| 167 | return; |
| 168 | } |
| 169 | await this.send |
| 170 | references: `<${threadId |
| 171 | }); |
| 172 | } |
| 173 | |
| 174 | // ponytail: the HTTP relay carries no headers, so queued mail loses `References` as relayed mail already does; add it to the payload when delivery speaks SMTP. |
| 175 | private async enqueueThreadEmail |
| 176 | to: string, |
| 177 | template: ThreadTemplate |
| 178 | subject: string, |
| 179 | context: Record |
| 180 | idempotencyKey: string, |
| 181 | ) { |
| 182 | const element = |
| 183 | ...context, |
| 184 | appUrl: this.appUrl |
| 185 | }); |
| 186 | const [html, |
| 187 | render(element), |
| 188 | render(element, { |
| 189 | ]); |
| 190 | await this.db |
| 191 | .insert(schema.deliveryJob |
| 192 | .values({ |
| 193 | kind: 'email', |
| 194 | idempotencyKey: `email:${idempotencyKey |
| 195 | payload: { to, |
| 196 | }) |
| 197 | .onConflictDoNothing(); |
| 198 | await this.db |
| 199 | this.logger.log |
| 200 | } |
| 201 | |
| 202 | private async send |
| 203 | to: string, |
| 204 | subject: string, |
| 205 | template: string, |
| 206 | context: Record |
| 207 | options: { |
| 208 | ): Promise<void |
| 209 | await this.mailer |
| 210 | to, |
| 211 | subject, |
| 212 | template, |
| 213 | context: { ...context |
| 214 | ...options, |
| 215 | }); |
| 216 | this.logger.log |
| 217 | } |
| 218 | } |