PhoenixKitOG.ActivityLog (PhoenixKitOG v0.1.1)

Copy Markdown View Source

Thin wrapper around PhoenixKit.Activity.log/1 for the OG plugin.

Every entry is stamped with module: "phoenix_kit_og" so the admin activity feed can filter to just this plugin's events. Callers pass the pipe-friendly {:ok, struct} shape so the return value chains cleanly through context functions.

The wrapper guards against three known drop-cases:

  • PhoenixKit.Activity isn't loaded — module not compiled yet (rare, but possible during recompile cascades).
  • The phoenix_kit_activity table doesn't exist — a very fresh host that hasn't run migrations past V72 (activity was introduced there). Rescuing Postgrex.Error :undefined_table keeps a fresh install usable before migrations catch up.
  • Any other exception — logged as a warning, never re-raised.

Metadata is PII-safe by convention: names, statuses, counts, UUIDs are OK; email / phone / free-text / anything a user could paste in is not.

Summary

Functions

Pipe step for context functions returning {:ok, struct}. Logs the action and returns the value unchanged. {:error, _} short-circuits to a no-op so callers can put this at the tail of the pipe.

Log without the pipe wrapper — for transactions, toggles, and other paths where the caller doesn't have a {:ok, struct} in hand.

Functions

log(ok, action, opts, fields_fn)

@spec log(
  {:ok, struct()} | {:error, term()},
  String.t(),
  keyword(),
  (struct() -> map())
) :: {:ok, struct()} | {:error, term()}

Pipe step for context functions returning {:ok, struct}. Logs the action and returns the value unchanged. {:error, _} short-circuits to a no-op so callers can put this at the tail of the pipe.

Example

%Template{}
|> Template.changeset(attrs)
|> Repo.insert()
|> ActivityLog.log("template.created", opts, &template_activity_fields/1)

maybe_log(action, opts, fields)

@spec maybe_log(String.t(), keyword(), map()) :: :ok

Log without the pipe wrapper — for transactions, toggles, and other paths where the caller doesn't have a {:ok, struct} in hand.