diff --git a/.changeset/discord-notification-thread-target-docs.md b/.changeset/discord-notification-thread-target-docs.md new file mode 100644 index 000000000..857667d2e --- /dev/null +++ b/.changeset/discord-notification-thread-target-docs.md @@ -0,0 +1,5 @@ +--- +"@fluojs/discord": patch +--- + +Correct the Discord notification routing documentation: one non-blank recipient takes precedence over the configured default thread, multiple non-blank recipients are rejected, and a valid message with neither a recipient nor a default thread is passed to the transport without a thread ID. Runtime behavior is unchanged. diff --git a/book/02-fluoshop/ch18-notification-channels.ko.md b/book/02-fluoshop/ch18-notification-channels.ko.md index 2b09e3f9d..abcce8267 100644 --- a/book/02-fluoshop/ch18-notification-channels.ko.md +++ b/book/02-fluoshop/ch18-notification-channels.ko.md @@ -18,7 +18,9 @@ FluoBlog에서 글을 구독하던 독자는 티셔츠를 주문한 뒤 이메 `@fluojs/notifications`는 `channel`, `id`, `recipients`, `subject`, `template`, `payload`, `metadata` 등을 가진 요청을 받아 등록된 채널로 보낸다. 이메일이나 Slack 구현을 스스로 발견하거나 설치하지 않는다. `NotificationChannel` 값을 `channels`에 명시적으로 넣어야 한다. 이 장에서는 직접 채널 인터페이스를 흉내 내지 않고 `@fluojs/email`의 `EMAIL_CHANNEL`, `@fluojs/slack`의 `SLACK_CHANNEL`, `@fluojs/discord`의 `DISCORD_CHANNEL`을 사용한다. 그래야 부분 이메일 수락, recipient 해석, 전송 취소 같은 실제 패키지 동작을 함께 검증할 수 있다. -세 채널의 대상 지정은 서로 다르다. 이메일 recipient는 이메일 주소다. Slack의 하나의 dispatch는 단일 recipient에서 하나의 대상 채널을 해석하고, 없으면 기본 채널로 폴백한다. Discord recipient는 이메일 주소나 사용자 ID가 아니라 이 통합에서의 thread 경로이며, 단일 recipient에서 해석하고 없으면 기본 thread로 폴백한다. 여러 Slack 채널이나 Discord thread에 보내려면 요청을 각각 만들거나 해당 서비스의 `sendMany`를 사용한다. 하나의 요청에 여러 recipient를 넣는 것이 세 시스템에서 같은 의미라고 가정하지 않는다. +세 채널의 대상 지정은 서로 다르다. 이메일 recipient는 이메일 주소다. Slack의 하나의 dispatch는 단일 recipient에서 하나의 대상 채널을 해석하고, 없으면 기본 채널로 폴백한다. Discord recipient는 이메일 주소나 사용자 ID가 아니라 thread ID다. Discord는 `recipients` 각 항목의 앞뒤 공백을 제거하고 빈 항목을 제외한다. 남은 항목이 둘 이상이면 `DiscordMessageValidationError`로 거부하고, 하나만 남으면 그 값이 `defaultThreadId`보다 우선한다. 아무것도 남지 않으면 설정된 `defaultThreadId`를 사용한다. + +남은 수신자와 기본 thread가 모두 없으면 유효한 메시지는 `threadId`가 지정되지 않은 상태로 설정된 transport에 전달된다. 내장 webhook transport는 이 메시지에 `thread_id` 쿼리 파라미터를 새로 추가하지 않는다. 하나의 dispatch가 여러 Discord thread로 자동 fan-out되지는 않는다. 여러 Slack 채널이나 Discord thread에 보내려면 요청을 각각 만들거나 해당 서비스의 `sendMany`를 사용한다. 하나의 요청에 여러 recipient를 넣는 것이 세 시스템에서 같은 의미라고 가정하지 않는다. 아래 `src/notifications/order-notifications.ts`는 **완전한 애플리케이션 파일**이다. DB 조회나 전송을 하지 않고, 확인된 결제 사건과 기존 계정에서 얻은 수신자 정보로 세 요청을 만든다. 이 장의 `PaidNotice`는 알림용 스냅샷이며 저장소의 주문 모델을 대체하지 않는다. `totalMinor`는 JSON 경계를 넘은 십진 문자열이다. 통화와 금액은 이미 결제 처리 경계에서 검증된 값이며, 코드에서 소수로 다시 계산하지 않는다. 현재 `ProductVariant.priceMinor`를 조회해 과거 결제 금액을 다시 산출하지도 않는다. diff --git a/book/02-fluoshop/ch18-notification-channels.md b/book/02-fluoshop/ch18-notification-channels.md index 6e5c5b5cd..0a5d887be 100644 --- a/book/02-fluoshop/ch18-notification-channels.md +++ b/book/02-fluoshop/ch18-notification-channels.md @@ -18,7 +18,9 @@ At first, one `EmailService.send` call was enough. We can continue using the del `@fluojs/notifications` accepts requests with fields such as `channel`, `id`, `recipients`, `subject`, `template`, `payload`, and `metadata`, and sends them through registered channels. It does not discover or install email or Slack implementations itself. Explicitly place `NotificationChannel` values in `channels`. Rather than imitating channel interfaces ourselves, this chapter uses `EMAIL_CHANNEL` from `@fluojs/email`, `SLACK_CHANNEL` from `@fluojs/slack`, and `DISCORD_CHANNEL` from `@fluojs/discord`. That lets us verify actual package behavior, including partial email acceptance, recipient interpretation, and delivery cancellation. -The three channels address recipients differently. An email recipient is an email address. One Slack dispatch resolves to one destination channel from a single recipient, then falls back to the default channel. A Discord recipient is neither an email address nor a user ID; in this integration, it is a thread path resolved from a single recipient, then the default thread. To send to multiple Slack channels or Discord threads, create separate requests or use that service's `sendMany`. Do not assume that multiple recipients in one request have the same meaning in all three systems. +The three channels address recipients differently. An email recipient is an email address. One Slack dispatch resolves to one destination channel from a single recipient, then falls back to the default channel. A Discord recipient is a thread ID, not an email address or a user ID. The Discord integration trims leading and trailing whitespace from each entry in `recipients` and drops empty entries. It rejects two or more remaining entries with `DiscordMessageValidationError`. One remaining entry takes precedence over `defaultThreadId`; if none remain, the integration uses `defaultThreadId` when configured. + +If neither a recipient nor a default thread is available, a valid message reaches the configured transport with `threadId` unset. The built-in webhook transport does not add a `thread_id` query parameter for that message. A single dispatch does not automatically fan out to multiple Discord threads. To send to multiple Slack channels or Discord threads, create separate requests or use that service's `sendMany`. Do not assume that multiple recipients in one request have the same meaning in all three systems. The following `src/notifications/order-notifications.ts` is a **complete application file**. Without querying the database or sending anything, it creates three requests from a confirmed payment event and recipient information obtained from the existing account. `PaidNotice` in this chapter is a notification snapshot, not a replacement for the stored order model. `totalMinor` is a decimal string that has crossed the JSON boundary. Currency and amount were already validated at the payment processing boundary; the code does not recalculate them as fractional numbers. Nor does it query the current `ProductVariant.priceMinor` to recalculate a past payment amount. diff --git a/packages/discord/README.ko.md b/packages/discord/README.ko.md index 7a82138df..07414960c 100644 --- a/packages/discord/README.ko.md +++ b/packages/discord/README.ko.md @@ -180,8 +180,8 @@ export class AppModule {} Behavioral contract 메모: -- 하나의 notification dispatch는 `recipients`의 단일 항목으로 정확히 하나의 Discord thread 경로에 매핑됩니다. -- `recipients`가 없으면 `DiscordService.sendNotification(...)`는 `defaultThreadId`로 폴백합니다. +- `DiscordService.sendNotification(...)`은 `recipients` 각 항목의 앞뒤 공백을 제거하고 빈 항목을 제외합니다. 남은 항목이 둘 이상이면 `DiscordMessageValidationError`로 거부합니다. 하나만 남으면 그 값이 `defaultThreadId`보다 우선하며, 아무것도 남지 않으면 설정된 `defaultThreadId`를 사용합니다. +- 남은 수신자와 기본 thread가 모두 없으면 유효한 메시지는 `threadId`가 지정되지 않은 상태로 설정된 transport에 전달됩니다. 내장 webhook transport는 이 메시지에 `thread_id` 쿼리 파라미터를 새로 추가하지 않습니다. - notification metadata는 payload metadata, dispatch metadata, template/subject marker를 합쳐 구성됩니다. 중복 key에서는 dispatch metadata가 payload metadata를 덮어쓰고, 최종 `subject` / `template` marker가 둘 모두를 덮어씁니다. `template`은 renderer가 구성된 경우에만 렌더링됩니다. - Template rendering은 서비스가 ready일 때만 시작합니다. Render input에는 `signal`이 포함되므로 renderer는 transport delivery 전에 caller-cancelled 작업을 중단할 수 있습니다. - 여러 Discord thread로 fan-out이 필요한 notification workflow라면 thread별 concrete Discord message를 만들어 `DiscordService.sendMany(...)`로 보내거나 별도 notification dispatch를 실행해야 합니다. 하나의 notification dispatch는 multi-recipient fan-out을 암묵적으로 확장하지 않습니다. diff --git a/packages/discord/README.md b/packages/discord/README.md index 2c4c77add..40bee3bca 100644 --- a/packages/discord/README.md +++ b/packages/discord/README.md @@ -180,8 +180,8 @@ Supported notification payload fields: Behavioral contract notes: -- One notification dispatch maps to exactly one Discord thread route from a single entry in `recipients`. -- If `recipients` is omitted, `DiscordService.sendNotification(...)` falls back to `defaultThreadId`. +- `DiscordService.sendNotification(...)` trims leading and trailing whitespace from each entry in `recipients` and drops empty entries. It rejects two or more remaining entries with `DiscordMessageValidationError`. One remaining entry takes precedence over `defaultThreadId`; if none remain, the service uses `defaultThreadId` when configured. +- If neither a recipient nor a default thread is available, a valid message reaches the configured transport with `threadId` unset. The built-in webhook transport does not add a `thread_id` query parameter for that message. - Notification metadata is merged from payload metadata, dispatch metadata, and template/subject markers. On duplicate keys, dispatch metadata overrides payload metadata, and final `subject` / `template` markers override both. `template` is rendered only when a renderer is configured. - Template rendering starts only while the service is ready. The render input includes `signal`, so renderers can stop caller-cancelled work before transport delivery. - If a notification workflow needs fan-out across multiple Discord threads, create one concrete Discord message per thread with `DiscordService.sendMany(...)` or issue separate notification dispatches; a single notification dispatch never expands multi-recipient fan-out implicitly. diff --git a/packages/discord/src/service.ts b/packages/discord/src/service.ts index c1887dc7c..110a2f7db 100644 --- a/packages/discord/src/service.ts +++ b/packages/discord/src/service.ts @@ -328,7 +328,7 @@ export class DiscordService implements OnModuleInit, OnApplicationShutdown { * @param notification Shared notification envelope interpreted by the Discord package. * @param options Optional abort signal propagated to rendering and transport work. * @returns A normalized delivery receipt for the resulting Discord message. - * @throws {DiscordMessageValidationError} When the notification cannot resolve one target thread or any Discord-visible content. + * @throws {DiscordMessageValidationError} When more than one recipient remains after trimming and dropping empty entries, or when the resolved message has no Discord-visible content. * * @example * ```ts