Bonfire.API.MastoCompat.Mappers.Account (Bonfire v1.0.1-social-alpha.28)

View Source

Maps Bonfire User objects to Mastodon Account format.

This module is the single source of truth for transforming Bonfire users into Mastodon-compatible account objects. It handles:

  • Field extraction from both GraphQL responses (aliased names) and Ecto structs (schema names)
  • Stats computation (with options to skip or use preloaded values)
  • Building the final flat Mastodon account structure

Usage

# Basic account transformation
Mappers.Account.from_user(user)

# Skip expensive stats for list endpoints
Mappers.Account.from_user(user, skip_expensive_stats: true)

# Use preloaded stats (batch loading optimization)
Mappers.Account.from_user(user, follow_counts: %{followers: 10, following: 5}, status_count: 42)

Summary

Functions

Transform a Bonfire User into a Mastodon Account.

Same as from_user/2 but raises on invalid input.

Checks if an account object is valid (non-nil and has ID).

Functions

from_user(user, opts \\ [])

Transform a Bonfire User into a Mastodon Account.

Returns nil if the user is nil or invalid.

Options

  • :skip_expensive_stats - Skip computing expensive stats (statuses_count, followers_count, following_count, follow_requests_count). Use for list endpoints where these counts are not displayed. Default: false
  • :follow_counts - Pre-loaded follow counts map with :followers and :following keys
  • :status_count - Pre-loaded status count integer
  • :current_user - Current user context (for settings lookups)
  • :fallback_return - Value to return on error (default: nil)

Examples

iex> from_user(%{id: "123", character: %{username: "alice"}})
%{"id" => "123", "username" => "alice", ...}

iex> from_user(nil)
nil

from_user!(user, opts \\ [])

Same as from_user/2 but raises on invalid input.

valid?(account)

Checks if an account object is valid (non-nil and has ID).