Notify
How a module tells a person something, at once or at a set time, such as the appointments module's booking reminders: where it lives, how it is checked and how it is tested. A message reaches a person through the notification methods they chose; email is the first notification method, and the only one so far.
How it works
notify(ctx, message, { key, at }):messageis{ to: { email }, subject, text, replyTo?, attachments? }; attachments only with a message sent at once (a planned one is text only, VALIDATION otherwise). Withoutat, or with anatthat has passed, it goes now. With a lateratit is kept in Postgres under the module's ownkey(reminder:<appointment id>; 1 to 200 letters, digits and:._-), and the same key again moves it.cancelNotification(ctx, key)calls a planned one off (cancelled); one already sent stays sent.- Plan and call off in the transaction of the change they are about (
withDb(ctx, tx)): a booking that rolls back plans nothing. - A planned notification has a job on the queue (
notify.send) with its time and the key<plugin>:<key>: moving the notification moves its job, calling it off takes the job back. When it is due the job claims it (scheduled→sending) and sends it, so it goes once, and only then is itsent. A send cut off midway (the worker stopped) is taken over by a later try after 5 minutes; a database failure puts it back toscheduledfor the job's next try. A module that is off, or no longer has the capability, gets itskipped; one the notification method refuses (email not set up) isfailedwith the reason. Email that fails to arrive is the email service's to try again. - Only from the context SHADE gave the module's code (FORBIDDEN otherwise), with the
notifycapability (notify: { methods: ['email'] }). The capability covers its notification methods: a module needs noemailcapability for it.
Notification methods
- A notification method names the provider behind it (email's is the email settings); it is used only while that provider is set
up and on. The capability's
methodslists the ones a module may use, by name (lowercase letters, digits and-); a notification method no provider gives yet is never used. WhatsApp and SMS come with their providers' first modules. - Which notification methods a message takes is decided when it goes, for a planned one when it is due: the notification methods the person chose (on their account account, found by the address the message is to), among the ones the module declares and that work now. A person who chose nothing, or has no account, gets email.
- Choosing a notification method is the person's opt-in. Every notification method but email needs it: SHADE never sends through one the
person did not choose, whatever the module declares. Files (
attachments) go by email only. - When none of the person's chosen notification methods can take a message, it falls back to email, and a planned one says so
(its
methodsand a note). When email can't take it either,notifyrefuses with CONFLICT ("No notification method can reach this person now") and a planned one isfailedwith that reason: never skipped quietly. - A module lets a signed-in account see and change their choice in its account routes (experimental, until a second module uses it):
// GET: [{ name: 'email', chosen: true, ready: true }]
handler: async (ctx, request) => ({ methods: await notifyChoices(ctx, request.account) })
// PUT: their own choice among this module's notification methods; another module's stay as chosen.
handler: async (ctx, request) => ({ methods: await chooseNotificationMethods(ctx, request.account, request.body.methods) })
Both work only for the signed-in account of the request the route serves (FORBIDDEN for another account's id, in a schedule or a public route), and only for notification methods the module declares (FORBIDDEN otherwise). The appointments module's My reminders routes are the first account.
Kept data
A person's notification method choices are kept with their account account: found, downloaded and erased with them
(Privacy, kind notification-methods), and deleted with the account.
Planned notifications hold whom they are for and what they say:
- found and erased with that person (Privacy, kind
notifications), planned ones too, so nothing more goes to them; - purged 30 days after they were due, once sent, called off or skipped (retention
notifications, at least 7 days); - deleted with the module when it is uninstalled.
Messages sent at once are not kept here: the email log has them.
Where it is in the code, and its tests: shade/features/notify/README.md in the shade repository.