PhoenixKitOG.ActivityLog (PhoenixKitOG v0.4.0)

Copy Markdown View Source

Thin wrapper around PhoenixKit.Activity.log/3 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. Core never raises — a missing activities table on a fresh host, or any other failure, is logged there and returned — so logging never breaks the mutation.

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

Inserts an activity row from INSIDE the caller's transaction, so the row exists exactly when the transaction commits. Returns the entry for publish/1, or nil when the row could not be written.

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.

Publishes entries written by insert_in_transaction/3, after the commit.

Functions

insert_in_transaction(action, opts, fields)

@spec insert_in_transaction(String.t(), keyword(), map()) :: struct() | nil

Inserts an activity row from INSIDE the caller's transaction, so the row exists exactly when the transaction commits. Returns the entry for publish/1, or nil when the row could not be written.

PhoenixKit.Activity.log/3 broadcasts as it inserts, which inside a transaction announces a change that may still roll back — hence the split. The insert runs in a savepoint: a failed audit row must not abort the transaction it describes.

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.

publish(entries)

@spec publish([struct() | nil]) :: :ok

Publishes entries written by insert_in_transaction/3, after the commit.