How a module delivers notifications by e-mail, and how a subject offers it.
Every notification lands in the recipient's in-app feed. The e-mail channel also mails it. The DMS defines the channel and ships no implementation for it. A mail module registers it, and every subject that offers e-mail then reaches the users who turn it on.
Two parties take part:
- A subject offers e-mail with
emailon its declaration. A subject without it stays in-app only. - A module registers the channel with
NotificationEmailChannel({ deliver }). Without a registration, nothing is mailed and the settings screen shows the Email column marked Soon, as on an installation without a mail module.
Offering e-mail on a subject
import {
NotificationCategory,
NotificationSubject,
} from "@antelopejs/interface-dms/notifications";
const shopCategory = NotificationCategory("shop", {
labelKey: "shop.notifications.categories.shop",
icon: "i-ph-shopping-cart",
});
export const orderShippedSubject = NotificationSubject("order-shipped", {
category: shopCategory,
labelKey: "shop.notifications.subjects.order_shipped",
email: true,
});
export const invoiceSubject = NotificationSubject("invoice", {
category: shopCategory,
labelKey: "shop.notifications.subjects.invoice",
email: { defaultOn: true },
});
Value of email | Meaning |
|---|---|
omitted or false | The subject is not offered by e-mail. Its Email cell shows —. |
true or { defaultOn: false } | Offered, off until the user turns it on. |
{ defaultOn: true } | Offered, on until the user turns it off. |
Registering the channel
import { NotificationEmailChannel } from "@antelopejs/interface-dms/notifications";
NotificationEmailChannel({
deliver: (userId, data, notificationId) => {
// Queue a mail for `userId` about `data` (its title, description,
// params, link and `tenantId`), keyed by `notificationId`.
},
});
deliver(userId, data, notificationId) receives:
| Argument | Description |
|---|---|
userId | The recipient. |
data | The NotificationData sent, with tenantId when the send named a workspace (SendOptions.tenantId). |
notificationId | Id of the in-app notification stored for this recipient. Key the mail on it to send it at most once. |
One module registers the channel. A later registration replaces the earlier one. Unloading the module whose registration is live removes the channel; unloading one that was replaced, as a hot reload does, leaves the live one in place.
When the DMS calls the channel
For each recipient, after the in-app notification is handled:
- Only a new in-app notification is mailed. The DMS calls
deliveronce the in-app row is stored and published. A send the recipient turned off in the app, a repeat inside the 10-second window and a retry of a keyed send (toUsersIdempotently) store no new row, so they mail nothing. E-mail is a copy of an in-app notification: a user who turns a subject off in the app receives no e-mail for it either, and the settings screen disables its Email switch. - The user preference decides. The choice is stored under
categoryId:subjectId@email, next to the in-app key in the same per-user map. A user who never chose gets the subject'sdefaultOn. - A locked subject is mailed whatever is stored. A locked subject that offers e-mail reaches every recipient by e-mail, and the preferences route refuses turning its e-mail off with HTTP 403.
- A failing channel never fails the send. A
deliverthat throws or rejects is logged with[DMS] Notification e-mail channel failed to deliver. The in-app notification stays, and the send resolves. - Sends wait for
deliver. Queue slow work, such as talking to a mail server, instead of doing it insidedeliver.
Preferences are per user and global: one choice per subject covers every workspace the user belongs to, for e-mail as for the app. data.tenantId tells the channel which workspace the notification is about, for example to pick its sender or branding.