AshDispatch.Helpers.NotificationLoader (AshDispatch v0.5.0)

View Source

Helper for loading and managing notifications.

Automatically discovers the notification resource from AshDispatch.Resources.Notification and provides common operations.

Usage

alias AshDispatch.Helpers.NotificationLoader

# Load recent notifications
notifications = NotificationLoader.load_recent(user_id)
#=> [%{id: "...", title: "...", ...}]

# Mark as read
NotificationLoader.mark_as_read(notification_id, actor: user)

# Mark all as read
NotificationLoader.mark_all_as_read(user_id, actor: user)

Custom Serialization

Apps can provide custom serializers:

NotificationLoader.load_recent(user_id,
  serializer: &MyApp.serialize_notification/1
)

Summary

Functions

Load recent notifications for a user.

Mark all notifications as read for a user.

Mark a notification as read.

Serialize a notification for JSON/frontend consumption.

Functions

load_recent(user_id, opts \\ [])

Load recent notifications for a user.

Returns serialized notifications ready for JSON/frontend consumption.

Options

  • :limit - Number of notifications to load (default: 50)
  • :serializer - Custom serialization function (default: built-in serializer)

Examples

# Default serialization
NotificationLoader.load_recent("user-123")
#=> [%{id: "...", title: "...", read: false, ...}]

# Custom limit
NotificationLoader.load_recent("user-123", limit: 10)

# Custom serializer
NotificationLoader.load_recent("user-123",
  serializer: &MyApp.serialize_notification/1
)

mark_all_as_read(user_id, opts \\ [])

Mark all notifications as read for a user.

Uses the mark_all_as_read action which properly triggers counter broadcasts.

Options

  • :actor - User performing the action (for authorization)

Examples

NotificationLoader.mark_all_as_read(user_id, actor: current_user)
#=> {:ok, %{marked_count: 5}}

Returns

  • {:ok, %{marked_count: integer}} - Successfully marked notifications as read
  • {:error, reason} - Failed to mark notifications

mark_as_read(notification_id, opts \\ [])

Mark a notification as read.

Uses Ash policies to verify ownership.

Options

  • :actor - User performing the action (for authorization)

Examples

NotificationLoader.mark_as_read(notification_id, actor: current_user)
#=> {:ok, updated_notification}

Returns

  • {:ok, notification} - Successfully marked as read
  • {:error, reason} - Failed (unauthorized, not found, etc.)

serialize(notification)

Serialize a notification for JSON/frontend consumption.

Uses camelCase keys for JavaScript compatibility. Apps can provide custom serializers via options.

Examples

NotificationLoader.serialize(notification)
#=> %{id: "...", title: "...", actionLabel: "View", ...}