| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { parseReferences } |
| 3 | import { Inject, Injectable |
| 4 | import { and, eq |
| 5 | |
| 6 | import { DATABASE } |
| 7 | import type { |
| 8 | EventType, |
| 9 | RepositoryEvent, |
| 10 | StoredEvent, |
| 11 | } from '../../lib/events/events.js'; |
| 12 | import { repositoryFullNameOf } |
| 13 | import { teamSlug } |
| 14 | import { MailService, type |
| 15 | import type { NotificationReason |
| 16 | import { RepositoryAccessService } |
| 17 | import { excluded } |
| 18 | |
| 19 | // The rest reach webhooks only. |
| 20 | const notifyingEvents = [ |
| 21 | 'issue.opened', |
| 22 | 'issue.closed', |
| 23 | 'issue.reopened', |
| 24 | 'issue.assigned', |
| 25 | 'issue.commented', |
| 26 | 'pull_request.merged', |
| 27 | 'pull_request.reviewed', |
| 28 | 'pull_request.review_commented', |
| 29 | ] as const satisfies |
| 30 | |
| 31 | type ThreadEvent |
| 32 | RepositoryEvent, |
| 33 | { type: |
| 34 | >; |
| 35 | |
| 36 | function notifies(event: |
| 37 | return (notifyingEvents as |
| 38 | } |
| 39 | |
| 40 | // Long enough to read the point of a comment in the email, short enough that nobody reads a whole essay there. |
| 41 | const EXCERPT_LENGTH = 1000 |
| 42 | |
| 43 | // GitHub's limit too: one comment cannot page a whole user base. |
| 44 | const MAX_MENTIONS = 50 |
| 45 | |
| 46 | const STATES = { |
| 47 | 'issue.closed': 'closed', |
| 48 | 'issue.reopened': 'reopened', |
| 49 | 'pull_request.merged': 'merged', |
| 50 | } as const; |
| 51 | |
| 52 | type Thread |
| 53 | |
| 54 | /** What an event says, as far as notifications care. */ |
| 55 | type Activity |
| 56 | /** Text whose `@user` and `@org/team` mentions notify, and subscribe, whoever they name. */ |
| 57 | text: string |
| 58 | assigneeIds: string |
| 59 | /** Whether the actor joined the conversation, and so subscribes to it. */ |
| 60 | participates: boolean |
| 61 | template: ThreadTemplate |
| 62 | context: Record |
| 63 | }; |
| 64 | |
| 65 | /** Turns repository events into inbox rows and emails. */ |
| 66 | @Injectable() |
| 67 | export class NotifierService |
| 68 | private readonly logger |
| 69 | |
| 70 | constructor( |
| 71 | @Inject(DATABASE) |
| 72 | private readonly access |
| 73 | private readonly mail |
| 74 | ) {} |
| 75 | |
| 76 | async handle(event |
| 77 | if (!notifies(event |
| 78 | const thread = |
| 79 | if (!thread) |
| 80 | const activity = |
| 81 | if (!activity) |
| 82 | |
| 83 | const recipients = |
| 84 | if (recipients.size |
| 85 | |
| 86 | await this.db |
| 87 | .insert(schema.notification |
| 88 | .values( |
| 89 | [...recipients].map |
| 90 | userId, |
| 91 | issueId: thread.id |
| 92 | reason, |
| 93 | eventType: event.type |
| 94 | actorId: event.actorId |
| 95 | })), |
| 96 | ) |
| 97 | .onConflictDoUpdate({ |
| 98 | target: [schema.notification |
| 99 | set: { |
| 100 | reason: excluded(schema |
| 101 | eventType: excluded(schema |
| 102 | actorId: excluded(schema |
| 103 | unread: true, |
| 104 | updatedAt: sql`now()` |
| 105 | }, |
| 106 | }); |
| 107 | |
| 108 | await this.email |
| 109 | } |
| 110 | |
| 111 | private async loadThread |
| 112 | const [thread] |
| 113 | .select({ |
| 114 | id: schema.issue |
| 115 | number: schema.issue |
| 116 | title: schema.issue |
| 117 | body: schema.issue |
| 118 | isPullRequest: schema.issue |
| 119 | authorId: schema.issue |
| 120 | repositoryId: schema.issue |
| 121 | organizationId: schema.repository |
| 122 | repository: repositoryFullNameOf( |
| 123 | schema.user, |
| 124 | schema.organization, |
| 125 | schema.repository, |
| 126 | ), |
| 127 | }) |
| 128 | .from(schema.issue |
| 129 | .innerJoin( |
| 130 | schema.repository, |
| 131 | eq(schema.repository |
| 132 | ) |
| 133 | .innerJoin(schema.user |
| 134 | .leftJoin( |
| 135 | schema.organization, |
| 136 | eq(schema.organization |
| 137 | ) |
| 138 | .where(eq(schema |
| 139 | return thread; |
| 140 | } |
| 141 | |
| 142 | /** Null when what the event points at is gone, such as a comment deleted before its event was handled. */ |
| 143 | private async activityOf |
| 144 | event: ThreadEvent |
| 145 | thread: Thread |
| 146 | ): Promise<Activity |
| 147 | switch (event.type |
| 148 | case 'issue.opened': { |
| 149 | const assignees = |
| 150 | .select({ userId: |
| 151 | .from(schema.issueAssignee |
| 152 | .where(eq(schema |
| 153 | return { |
| 154 | text: thread.body |
| 155 | assigneeIds: assignees.map |
| 156 | participates: true, |
| 157 | template: 'thread-opened', |
| 158 | context: { body: thread |
| 159 | }; |
| 160 | } |
| 161 | case 'issue.commented': { |
| 162 | const [comment] |
| 163 | .select({ body: |
| 164 | .from(schema.issueComment |
| 165 | .where(eq(schema |
| 166 | if (!comment) |
| 167 | return { |
| 168 | text: comment.body |
| 169 | assigneeIds: [], |
| 170 | participates: true, |
| 171 | template: 'thread-comment', |
| 172 | context: { body: excerpt |
| 173 | }; |
| 174 | } |
| 175 | case 'pull_request.review_commented': { |
| 176 | const [comment] |
| 177 | .select({ |
| 178 | body: schema.pullRequestReviewComment |
| 179 | path: schema.pullRequestReviewComment |
| 180 | }) |
| 181 | .from(schema.pullRequestReviewComment |
| 182 | .where( |
| 183 | eq(schema.pullRequestReviewComment |
| 184 | ); |
| 185 | if (!comment) |
| 186 | return { |
| 187 | text: comment.body |
| 188 | assigneeIds: [], |
| 189 | participates: true, |
| 190 | template: 'thread-comment', |
| 191 | context: { body: excerpt |
| 192 | }; |
| 193 | } |
| 194 | case 'pull_request.reviewed': { |
| 195 | const [review] |
| 196 | .select({ |
| 197 | state: schema.pullRequestReview |
| 198 | body: schema.pullRequestReview |
| 199 | }) |
| 200 | .from(schema.pullRequestReview |
| 201 | .where(eq(schema |
| 202 | if (!review) |
| 203 | const comments = |
| 204 | .select({ body: |
| 205 | .from(schema.pullRequestReviewComment |
| 206 | .where( |
| 207 | eq( |
| 208 | schema.pullRequestReviewComment.reviewId |
| 209 | event.payload.reviewId |
| 210 | ), |
| 211 | ); |
| 212 | return { |
| 213 | text: [review.body |
| 214 | '\n\n', |
| 215 | ), |
| 216 | assigneeIds: [], |
| 217 | participates: true, |
| 218 | template: 'thread-review', |
| 219 | context: { |
| 220 | state: review.state |
| 221 | body: review.body |
| 222 | commentCount: comments.length, |
| 223 | }, |
| 224 | }; |
| 225 | } |
| 226 | case 'issue.assigned': { |
| 227 | const [assignee] |
| 228 | .select({ username: |
| 229 | .from(schema.user |
| 230 | .where(eq(schema |
| 231 | if (!assignee) |
| 232 | return { |
| 233 | text: null, |
| 234 | assigneeIds: [event.payload |
| 235 | participates: false, |
| 236 | template: 'thread-assigned', |
| 237 | context: { assignee: assignee |
| 238 | }; |
| 239 | } |
| 240 | case 'issue.closed': |
| 241 | case 'issue.reopened': |
| 242 | case 'pull_request.merged': |
| 243 | return { |
| 244 | text: null, |
| 245 | assigneeIds: [], |
| 246 | participates: false, |
| 247 | template: 'thread-state', |
| 248 | context: { state: STATES |
| 249 | }; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** Everyone to notify, each with the most specific reason that applies. Nobody hears about their own activity, or about a repository they ignore or can no longer read. */ |
| 254 | private async recipientsOf |
| 255 | event: ThreadEvent |
| 256 | thread: Thread |
| 257 | activity: Activity |
| 258 | ) { |
| 259 | const direct = |
| 260 | for (const userId |
| 261 | const mentions = |
| 262 | activity.text, |
| 263 | thread, |
| 264 | event.actorId, |
| 265 | ); |
| 266 | for (const userId |
| 267 | if (!direct.has |
| 268 | } |
| 269 | for (const userId |
| 270 | if (!direct.has |
| 271 | } |
| 272 | // mentioning someone who cannot read the thread must neither notify nor subscribe them |
| 273 | for (const userId |
| 274 | if (!(await |
| 275 | direct.delete(userId |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | const joining = |
| 280 | if (activity.participates |
| 281 | if (joining.length |
| 282 | await this.db |
| 283 | .insert(schema.issueSubscription |
| 284 | .values( |
| 285 | joining.map((userId |
| 286 | userId, |
| 287 | issueId: thread.id |
| 288 | subscribed: true, |
| 289 | })), |
| 290 | ) |
| 291 | .onConflictDoNothing(); |
| 292 | } |
| 293 | |
| 294 | const [subscriptions, |
| 295 | this.db |
| 296 | .select({ |
| 297 | userId: schema.issueSubscription |
| 298 | subscribed: schema.issueSubscription |
| 299 | }) |
| 300 | .from(schema.issueSubscription |
| 301 | .where(eq(schema |
| 302 | this.db |
| 303 | .select({ |
| 304 | userId: schema.repositoryWatch |
| 305 | level: schema.repositoryWatch |
| 306 | }) |
| 307 | .from(schema.repositoryWatch |
| 308 | .where(eq(schema |
| 309 | ]); |
| 310 | |
| 311 | const subscribed = |
| 312 | subscriptions.map((row |
| 313 | ); |
| 314 | const ignoring = |
| 315 | watches.filter((row |
| 316 | ); |
| 317 | const indirect = |
| 318 | for (const [userId |
| 319 | if (isSubscribed) { |
| 320 | indirect.set( |
| 321 | userId, |
| 322 | userId === thread. |
| 323 | ); |
| 324 | } |
| 325 | } |
| 326 | for (const { |
| 327 | // an explicit unsubscribe silences watching too |
| 328 | if (level === |
| 329 | indirect.set(userId |
| 330 | } |
| 331 | |
| 332 | const recipients = |
| 333 | // ponytail: one access query per indirect recipient, batch them if threads with hundreds of subscribers show up |
| 334 | for (const [userId |
| 335 | if ( |
| 336 | !recipients.has(userId |
| 337 | (await this.access |
| 338 | ) { |
| 339 | recipients.set(userId |
| 340 | } |
| 341 | } |
| 342 | for (const userId |
| 343 | if (event.actorId |
| 344 | return recipients; |
| 345 | } |
| 346 | |
| 347 | /** `@org/team` counts only for the repository's own organization, and only from one of its members, so nobody can page a whole team from outside. */ |
| 348 | private async mentionsIn |
| 349 | text: string |
| 350 | thread: Thread |
| 351 | actorId: string |
| 352 | ) { |
| 353 | const references = |
| 354 | const usernames = |
| 355 | .flatMap((reference) |
| 356 | reference.kind === |
| 357 | ) |
| 358 | .slice(0, MAX_MENTIONS |
| 359 | const teamNames = |
| 360 | .flatMap((reference) |
| 361 | .slice(0, MAX_MENTIONS |
| 362 | |
| 363 | const users = |
| 364 | ? await this. |
| 365 | .select({ id: |
| 366 | .from(schema.user |
| 367 | .where(inArray(schema |
| 368 | : []; |
| 369 | |
| 370 | let teams: |
| 371 | if (teamNames.length |
| 372 | const [organization] |
| 373 | .select({ slug: |
| 374 | .from(schema.organization |
| 375 | .innerJoin( |
| 376 | schema.member, |
| 377 | and( |
| 378 | eq(schema.member |
| 379 | eq(schema.member |
| 380 | ), |
| 381 | ) |
| 382 | .where(eq(schema |
| 383 | const wanted = |
| 384 | teamNames |
| 385 | .filter((team) |
| 386 | .map((team) |
| 387 | ); |
| 388 | if (wanted.size |
| 389 | const members = |
| 390 | .select({ userId: |
| 391 | .from(schema.team |
| 392 | .innerJoin( |
| 393 | schema.teamMember, |
| 394 | eq(schema.teamMember |
| 395 | ) |
| 396 | .where(eq(schema |
| 397 | teams = members |
| 398 | .filter((member) |
| 399 | .map((member) |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return { users: |
| 404 | } |
| 405 | |
| 406 | /** Only to verified addresses, so nobody can sign up with someone else's address and have Ghost mail them. Best effort: a failed send is logged and skipped. */ |
| 407 | private async email |
| 408 | event: ThreadEvent |
| 409 | thread: Thread |
| 410 | activity: Activity |
| 411 | recipients: Map |
| 412 | ) { |
| 413 | const [people, |
| 414 | this.db |
| 415 | .select({ id: |
| 416 | .from(schema.user |
| 417 | .where( |
| 418 | and( |
| 419 | inArray(schema.user |
| 420 | eq(schema.user |
| 421 | ), |
| 422 | ), |
| 423 | event.actorId |
| 424 | ? this.db |
| 425 | .select({ username: |
| 426 | .from(schema.user |
| 427 | .where(eq(schema |
| 428 | .then(([row |
| 429 | : undefined, |
| 430 | ]); |
| 431 | |
| 432 | const kind = |
| 433 | for (const person |
| 434 | try { |
| 435 | await this.mail |
| 436 | template: activity.template |
| 437 | threadId: thread.id |
| 438 | idempotencyKey: `${event. |
| 439 | // a title is user input, and a line break in a header would start a new one |
| 440 | subject: `[${thread |
| 441 | context: { |
| 442 | ...activity.context |
| 443 | repository: thread.repository |
| 444 | number: thread.number |
| 445 | title: thread.title |
| 446 | isPullRequest: thread.isPullRequest |
| 447 | path: `/${thread |
| 448 | reason: recipients.get |
| 449 | actor: actor ?? |
| 450 | }, |
| 451 | }); |
| 452 | } catch (error) |
| 453 | this.logger.warn |
| 454 | `Emailing ${person.id |
| 455 | ); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | function excerpt(text: |
| 462 | const trimmed = |
| 463 | return trimmed.length |
| 464 | ? `${trimmed.slice |
| 465 | : trimmed; |
| 466 | } |