PhoenixKit.Notifications (phoenix_kit v1.7.102)

Copy Markdown View Source

Per-user notifications driven by PhoenixKit.Activity.

When an activity is logged with a target_uuid that differs from the actor_uuid, a row is inserted into phoenix_kit_notifications for the target user. The user sees it in the bell dropdown (count_unread/1, recent_for_user/2) and in the inbox at /notifications (list_for_user/2). Each row carries its own seen_at and dismissed_at — the same activity can be "seen but not dismissed" for one user and "unseen" for another.

The whole feature is gated by the global notifications_enabled setting (default "true"); when "false", maybe_create_from_activity/1 is a no-op.

Summary

Functions

Counts undismissed, unseen notifications for a user. Drives the badge.

Dismisses a single notification. Idempotent.

Bulk-dismisses all undismissed notifications. Returns {count, nil}.

Is the notifications feature enabled? Default true.

Fetches one notification scoped to the recipient. Returns nil if missing.

Returns {notifications, total_count} for the given user, newest first.

Bulk-marks all unseen notifications as seen. Returns {count, nil}.

Marks a single notification as seen. Idempotent — already-seen rows return {:ok, notification} unchanged.

Inserts a notification for the activity's target user, if the rules allow it.

Deletes notifications whose underlying activity is older than days.

Returns the N most-recent undismissed notifications for a user.

Retention period in days. Falls back to activity retention if unset.

Functions

count_unread(user_uuid)

Counts undismissed, unseen notifications for a user. Drives the badge.

dismiss(user_uuid, uuid)

Dismisses a single notification. Idempotent.

dismiss_all(user_uuid)

Bulk-dismisses all undismissed notifications. Returns {count, nil}.

enabled?()

Is the notifications feature enabled? Default true.

get_notification(user_uuid, uuid)

Fetches one notification scoped to the recipient. Returns nil if missing.

list_for_user(user_uuid, opts \\ [])

Returns {notifications, total_count} for the given user, newest first.

Options:

  • :page (default 1) / :per_page (default 25)
  • :status:unread (seen_at nil) | :all (default)

  • :include_dismissed — include dismissed rows (default false)

mark_all_seen(user_uuid)

Bulk-marks all unseen notifications as seen. Returns {count, nil}.

mark_seen(user_uuid, uuid)

Marks a single notification as seen. Idempotent — already-seen rows return {:ok, notification} unchanged.

maybe_create_from_activity(entry)

Inserts a notification for the activity's target user, if the rules allow it.

Returns one of:

  • {:ok, %Notification{}} — row created; broadcast on the per-user topic
  • {:ok, :skipped} — filtered out (no target, self-action, feature disabled)
  • {:error, changeset} — insert failed (logged, never raised)

prune(days)

Deletes notifications whose underlying activity is older than days.

recent_for_user(user_uuid, limit \\ 10)

Returns the N most-recent undismissed notifications for a user.

Drives the bell dropdown. Activity (and actor) are preloaded.

retention_days()

Retention period in days. Falls back to activity retention if unset.