Expand description
Durable, per-notifiable, read-tracked notifications - Laravel’s
database notification channel - plus a compile-time-checked,
multi-channel delivery builder (to) covering the database and
mail channels together. Not Laravel’s full Notification/via()/
toMail()/toDatabase()/toBroadcast() shape, and deliberately so.
Laravel’s Notification has optional per-channel render methods,
decided at runtime by via($notifiable). There’s no clean way to
express “this trait method is conditionally required based on another
method’s runtime return value” in Rust without Option-returning
defaults - and this codebase’s closest sibling traits (Mailable,
larust_queue::Job, larust_auth::Authenticatable) are all
zero-default-method traits, deliberately, specifically to force a
compile error on a real gap rather than a silently-never-implemented
one. Building Laravel’s full shape here would be the first trait in
this codebase to break that convention - so to’s builder doesn’t
decide channels at runtime from the notification itself either. Each
channel is opted into explicitly, at the call site
(.database().mail()), and each of those methods carries its own
real trait bound (N: Notification for .database(); N: MailNotification, U: MailNotifiable for .mail()) - asking for a
channel a notification/notifiable doesn’t actually implement is a
compile error pointing at the missing impl, not a channel silently
skipped at runtime:
notification::to(&user, &InvoiceSent { invoice_id })
.database()
.mail()
.send()
.await?;No broadcast channel yet - larust-live::push (push::broadcast( channel, html)) already fully solves “push a live update”
independently, and it’s already synchronous and infallible (see its
own doc comment), so there’s nothing this builder would add by
wrapping it; call it alongside, same as before:
notification::to(&user, &InvoiceSent { invoice_id }).database().mail().send().await?;
push::broadcast(&format!("notifications.{}", user.auth_id()), ...);notify and notify_and_mail (the original, narrower two-channel
facade - notify and mail()...send() run concurrently via
tokio::try_join!, unlike to’s own sequential-per-channel
send()) both still exist unchanged, for existing callers.
Re-exported through larust_support::notification (see
crates/larust-support/src/lib.rs) so generated apps depend only on
larust-support, never on this crate directly.
Structs§
- Pending
Notification - A multi-channel notification being assembled - build with
to, add channels with.database()/.mail(), run them with.send(). Each channel method builds and immediately boxes its own delivery future while its own bound (N: Notification, orN: MailNotification, U: MailNotifiable) is known, erasing it to a plainBoxedDelivery<'a>before storing it - which is what lets.send()itself need no bound onU/Nat all, regardless of how many or which channels were actually selected. - Stored
Notification - One stored row, as read back by
notifications_for.datastays a rawserde_json::Valuerather than a concrete type - a single query reads rows for many differentNotificationtypes at once; the app is expected to match onnotification_typeto interpretdataif it needs more than generic display.
Traits§
- Mail
Notifiable - A notifiable that knows its own mail address - Laravel’s
routeNotificationForMail(), narrowed to the common case (one, always-available address) rather than Laravel’s full per-notification override mechanism. - Mail
Notification - A notification that also knows how to render itself as mail -
Laravel’s
Notification::toMail(), as a real trait method rather than a runtime-dispatched one (see this crate’s own module doc comment for why).Mail: Send + 'staticisn’t this trait being stricter thanMailableitself needs - it’s what letsPendingNotification::mailbox the delivery future asdyn Future <...> + Sendregardless of what a specificMailableimpl happens to capture. - Notification
- A durable fact worth recording against a notifiable - Laravel’s
Notificationclass, narrowed to just its database-channel shape. SerializingSelfis the storeddatapayload; there’s no separate render step the way [Notification::to_database] would imply if it existed.NOTIFICATION_TYPEis the same explicit, stable, app-chosen tag conventionlarust_queue::Job::JOB_TYPEalready establishes - deliberately notstd::any::type_name::<Self>(), since that string isn’t stable across a rename and an already-stored row would silently stop being attributable to its type.
Functions§
- clear_
notifications - Permanently removes every notification owned by
notifiable. - delete_
notification - Permanently removes one notification, after verifying it belongs to the
acting notifiable. This mirrors
mark_as_read’s ownership behavior: missing rows areNotFound, while another user’s row isFORBIDDEN. - mark_
all_ as_ read - Marks every one of
notifiable’s own notifications read. No ownership check needed the waymark_as_readneeds one -WHERE notifiable_id = ?already makes touching another notifiable’s rows structurally impossible. - mark_
as_ read - Marks one notification read, verifying it actually belongs to
notifiablefirst - the same “does this row belong to the acting user?” questionlarust_auth::Policy<U>::update/deletealready answer, so this reuseslarust_auth::authorizerather than reinventing the check: a mismatched owner is a loudAppError::Http{FORBIDDEN, ..}, matching how e.g. updating someone else’s post already responds today, not a silent no-op (silently collapsing “not yours” intoOk(())is the right call for an authentication-state ambiguity like “am I logged in at all” -larust_auth::guard’s own precedent - but this is an authorization question about a specific id someone is deliberately trying to act on, a different question with a different, louder, established answer in this codebase). A nonexistent id isAppError::NotFound, kept distinct from the mismatched-owner case. - notifications_
for - Reads
notifiable’s own notifications, newest first, capped atlimitrows.limitis caller-supplied rather than a framework-picked constant - the same real precedentlarust_orm::QueryBuilder:: paginate(per_page)already sets in this crate family - making an unbounded query structurally impossible rather than merely policy-discouraged. No cursor/before_idpagination in v1, the same documented gappaginateitself carries. - notify
- Records
notificationagainstnotifiable- Laravel’s$user->notify(new InvoiceSent($invoice)), narrowed to the database channel (see this crate’s own doc comment for why mail/broadcast aren’t wrapped here). - notify_
and_ mail - Runs
notifyandmail().to(email).send(mailable)concurrently - sugar for the common “record it and email it” case, not a new dispatch mechanism (see this crate’s own module doc comment). Both calls still exist, still fully typed, still independently callable on their own; this only removes the sequencing cost of awaiting them one after the other, viatokio::try_join!. Fails with whichever error occurs first if either side fails - the other side is not rolled back, matching this crate’s ownnotify’s “one plainINSERT, no transaction” posture (a partially-delivered notification, e.g. recorded but not emailed, is treated the same accepted-tradeoff waylarust-mail’s own.queue()already treats a similar gap). - to
- Starts building a multi-channel delivery for
notification, aimed atnotifiable- seePendingNotification’s own doc comment. - unread_
count - The count of
notifiable’s own notifications that have never been marked read - the number a notification-bell UI would badge with.