PhoenixKit.Activity (phoenix_kit v1.7.102)

Copy Markdown View Source

Activity feed for tracking business-level actions across the platform.

Provides a simple API for logging and querying activities. Any module can call Activity.log/1 to record an action. The admin dashboard shows a real-time activity stream.

Usage

PhoenixKit.Activity.log(%{
  action: "post.created",
  actor_uuid: user.uuid,
  resource_type: "post",
  resource_uuid: post.uuid,
  metadata: %{"title" => post.title}
})

Action naming convention

Use dotted format: resource.verb — e.g., "post.created", "comment.liked", "user.registered", "password.changed", "role.assigned".

Summary

Functions

Returns a CSS badge class based on the action verb.

Counts activities matching the given filters.

Gets a single activity entry by UUID with preloaded associations.

Gets a single activity entry by UUID. Raises if not found.

Lists activities with filtering and pagination.

Returns distinct action types that have been logged.

Lists activities for a specific user (as actor).

Returns distinct modes that have been logged.

Returns distinct modules that have been logged.

Returns distinct resource types that have been logged.

Logs an activity.

Logs a user change with automatic from/to diff extraction from a changeset.

Returns a CSS badge class based on the mode.

Deletes activities older than the given number of days.

PubSub topic for activity events.

Returns the N most recent activities.

Resolves resource info for entries where resource_type is "user".

Returns the configured retention period in days.

Functions

action_badge_color(action)

Returns a CSS badge class based on the action verb.

count(opts \\ [])

Counts activities matching the given filters.

get_entry(uuid)

Gets a single activity entry by UUID with preloaded associations.

get_entry!(uuid)

Gets a single activity entry by UUID. Raises if not found.

list(opts \\ [])

Lists activities with filtering and pagination.

Options

  • :action — filter by action string (exact match or prefix with "post.*")
  • :actor_uuid — filter by who performed the action
  • :resource_type — filter by resource type
  • :target_uuid — filter by who was affected
  • :since — filter activities after this datetime
  • :until — filter activities before this datetime
  • :page — page number (default: 1)
  • :per_page — items per page (default: 50)
  • :preload — associations to preload (default: [:actor])

list_action_types()

Returns distinct action types that have been logged.

list_for_user(user_uuid, opts \\ [])

Lists activities for a specific user (as actor).

list_modes()

Returns distinct modes that have been logged.

list_modules()

Returns distinct modules that have been logged.

list_resource_types()

Returns distinct resource types that have been logged.

log(attrs)

Logs an activity.

Required fields

  • :action — dotted action string (e.g., "post.created")

Optional fields

  • :actor_uuid — who performed the action
  • :resource_type — type of resource acted on
  • :resource_uuid — UUID of the resource
  • :target_uuid — who was affected (e.g., follow target)
  • :metadata — map of additional context

Returns {:ok, entry} or {:error, changeset}. Failures are logged but never crash.

log_user_change(action, user, changeset, opts \\ [])

Logs a user change with automatic from/to diff extraction from a changeset.

Extracts changed fields from the changeset and builds field_from / field_to metadata pairs. Skips logging if nothing actually changed.

Options

  • :actor_uuid — who performed the action (default: the user's own UUID)
  • :target_uuid — who was affected (default: nil)
  • :mode — "auto" or "manual" (default: "auto")
  • :actor_role — "user" or "admin" (default: "user")
  • :extra_metadata — additional metadata to merge in (default: %{})
  • :skip_fields — fields to exclude from diff (default: [:custom_fields])

mode_badge_color(arg1)

Returns a CSS badge class based on the mode.

prune(days)

Deletes activities older than the given number of days.

pubsub_topic()

PubSub topic for activity events.

recent(limit \\ 20)

Returns the N most recent activities.

resolve_resource_users(entries)

Resolves resource info for entries where resource_type is "user".

Returns a map of resource_uuid => %{email: ..., first_name: ..., last_name: ...}. Batch-queries to avoid N+1.

retention_days()

Returns the configured retention period in days.