Notifications
A Notification is a status, a title, an optional description and optional links. An action
flashes one as a toast after its write. Sent with Notifications, the same value
is stored for one panel user and listed in the bell of a panel that shows one.
The bell
Panel::notifications shows a bell at the end of the topbar:
#![allow(unused)]
fn main() {
Panel::new("admin").notifications()
}
The bell counts the signed-in user’s unread notifications and opens a sheet that lists them,
newest first, with their links. Once the page loads, it keeps updating over Topcoat’s runtime
connection, so a notification sent while the page is open shows at once, as a toast. The panel
needs auth and the StoredNotification model, which tablo::models! registers.
Panel::notifications has the rules.
Sending
In a request, Notifications::of(cx) sends on the request’s panel, for the tenant the request
acts for. send takes the recipient’s PanelUser::user_id:
#![allow(unused)]
fn main() {
Notifications::of(cx)?
.send(
owner_id,
Notification::success("Your export is ready")
.description("It holds every order of the quarter.")
.action("Download", format!("/exports/{export}.csv")),
)
.await?;
}
send writes the row on its own connection and commits it at once. Inside a write’s
transaction, in a record fn, an action’s run or Resource::before_commit, send through the
transaction instead, so the notification commits with the write or not at all;
Notifications::send_in
has the rules:
#![allow(unused)]
fn main() {
Notifications::of(cx)?
.send_in(ex, owner_id, Notification::success("Your order shipped"))
.await?;
}
A job sends through the panel’s handle, for no tenant until tenant names one:
#![allow(unused)]
fn main() {
admin
.notifications()
.tenant(tenant)
.send(owner_id, Notification::info("The nightly import finished"))
.await?;
}
Who sees what
The bell lists a user’s notifications on its own panel: those sent for the tenant they act for, and those sent for no tenant. A notification for one tenant therefore never shows to a member of another, and one sent for no tenant, such as a job’s, shows whichever tenant the user acts for.
Keeping them
Nothing deletes stored notifications on its own. A job prunes the ones older than the app keeps:
#![allow(unused)]
fn main() {
admin
.notifications()
.prune(Duration::from_hours(24 * 90))
.await?;
}
One process
A send wakes only the bells open on the process that sent it: behind a load balancer, a bell on another process shows the notification on its next page load.