Skip to main content

Crate larust_notifications

Crate larust_notifications 

Source
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§

PendingNotification
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, or N: MailNotification, U: MailNotifiable) is known, erasing it to a plain BoxedDelivery<'a> before storing it - which is what lets .send() itself need no bound on U/N at all, regardless of how many or which channels were actually selected.
StoredNotification
One stored row, as read back by notifications_for. data stays a raw serde_json::Value rather than a concrete type - a single query reads rows for many different Notification types at once; the app is expected to match on notification_type to interpret data if it needs more than generic display.

Traits§

MailNotifiable
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.
MailNotification
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 + 'static isn’t this trait being stricter than Mailable itself needs - it’s what lets PendingNotification::mail box the delivery future as dyn Future <...> + Send regardless of what a specific Mailable impl happens to capture.
Notification
A durable fact worth recording against a notifiable - Laravel’s Notification class, narrowed to just its database-channel shape. Serializing Self is the stored data payload; there’s no separate render step the way [Notification::to_database] would imply if it existed. NOTIFICATION_TYPE is the same explicit, stable, app-chosen tag convention larust_queue::Job::JOB_TYPE already establishes - deliberately not std::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 are NotFound, while another user’s row is FORBIDDEN.
mark_all_as_read
Marks every one of notifiable’s own notifications read. No ownership check needed the way mark_as_read needs 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 notifiable first - the same “does this row belong to the acting user?” question larust_auth::Policy<U>::update/delete already answer, so this reuses larust_auth::authorize rather than reinventing the check: a mismatched owner is a loud AppError::Http{FORBIDDEN, ..}, matching how e.g. updating someone else’s post already responds today, not a silent no-op (silently collapsing “not yours” into Ok(()) 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 is AppError::NotFound, kept distinct from the mismatched-owner case.
notifications_for
Reads notifiable’s own notifications, newest first, capped at limit rows. limit is caller-supplied rather than a framework-picked constant - the same real precedent larust_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_id pagination in v1, the same documented gap paginate itself carries.
notify
Records notification against notifiable - 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 notify and mail().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, via tokio::try_join!. Fails with whichever error occurs first if either side fails - the other side is not rolled back, matching this crate’s own notify’s “one plain INSERT, no transaction” posture (a partially-delivered notification, e.g. recorded but not emailed, is treated the same accepted-tradeoff way larust-mail’s own .queue() already treats a similar gap).
to
Starts building a multi-channel delivery for notification, aimed at notifiable - see PendingNotification’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.